Created
March 16, 2015 14:16
-
-
Save mattwigway/a94ae43ce7e7ffe57f26 to your computer and use it in GitHub Desktop.
MapDB inverse secondary keys
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Create a set of Tuple2<key in original map, value> - secondary keys, backwards. | |
*/ | |
public static <K, V, V2> void inverseSecondaryKeys (MapWithModificationListener<K, V> map, | |
final NavigableSet<Fun.Tuple2<K, V2>> set, final Function2<V2[], K, V> fun) { | |
if (set.isEmpty()) { | |
for (Map.Entry<K, V> e : map.entrySet()) { | |
for (V2 val : fun.run(e.getKey(), e.getValue())) { | |
set.add(new Tuple2<K, V2>(e.getKey(), val)); | |
} | |
} | |
} | |
map.modificationListenerAdd(new MapListener<K, V> () { | |
@Override | |
public void update(K key, V oldVal, V newVal) { | |
// clear this key | |
set.subSet(new Tuple2(key, null), new Tuple2(key, Fun.HI)).clear(); | |
for (V2 val : fun.run(key, newVal)) { | |
set.add(new Tuple2<K, V2>(key, val)); | |
} | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment