Package eu.bandm.tools.util.java
Class IncrementalMap<K,V>
java.lang.Object
java.util.AbstractMap<K,V>
eu.bandm.tools.util.java.IncrementalMap<K,V>
- Type Parameters:
K- the type of map keysV- the type of map values
- All Implemented Interfaces:
Map<K,V>
An immutable map that supports efficient persistent updates.
Persistent Updates
This map implementation is immutable. The methods AbstractMap.put(K, V), Map.remove(java.lang.Object) and Map.compute(K, java.util.function.BiFunction<? super K, ? super V, ? extends V>) all throw UnsupportedOperationException.
For each of these methods, there is a corresponding method, such as putting(K, V), etc., that returns a new map where the update has been applied,
leaving the original unchanged. The implementation ensures that these
operations involve only a reasonable amount of copying.
- Since:
- 1.3
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classA report on an incremental map update.Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K, V> -
Method Summary
Modifier and TypeMethodDescriptioncomputeChange(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) Attempts to compute a mapping for the specified key and its current mapped value, ornullif there is no current mapping.Attempts to compute a mapping for the specified key and its current mapped value, ornullif there is no current mapping.computingIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) Attempts to compute a mapping for the specified key if there is no current mapped value.computingIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) Attempts to compute a mapping for the specified key and its current mapped value, if there is one.booleancontainsKey(Object key) static <K,V> IncrementalMap <K, V> empty()Returns an empty map.entrySet()Returns a variant of this map where the given key is mapped to the given value.Returns a variant of this map where the given key is unmapped.Methods inherited from class java.util.AbstractMap
clear, clone, containsValue, equals, hashCode, isEmpty, keySet, put, putAll, remove, size, toString, valuesMethods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Method Details
-
empty
Returns an empty map.- Type Parameters:
K- the type of map keysV- the type of map values- Returns:
- a map containing no entries
-
computeChange
public IncrementalMap.Change<K,V, computeChangeIncrementalMap<K, V>> (K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) Attempts to compute a mapping for the specified key and its current mapped value, ornullif there is no current mapping.- Parameters:
key- the key which wich the specified value is to be associatedremappingFunction- the remapping function to compute a value- Returns:
- a report of the incremental map update to a new map that has all entries of this map, but extended or overwritten with the newly computed one
- Throws:
NullPointerException- ifkeyorremappingFunctionis null- See Also:
-
putting
Returns a variant of this map where the given key is mapped to the given value.- Parameters:
key- the keyvalue- the new value to map the key to- Returns:
- a new map that has all entries of this map, except that
keyis mapped tovalueif nonnull, or unmapped otherwise - Throws:
NullPointerException- ifkeyorvalueis null
-
removing
Returns a variant of this map where the given key is unmapped.- Parameters:
key- the key- Returns:
- a new map that has all entries of this map, except that
keyis unmapped - Throws:
NullPointerException- ifkeyis null
-
computing
public IncrementalMap<K,V> computing(K key, BiFunction<? super K, ? super @Opt V, ? extends V> remappingFunction) Attempts to compute a mapping for the specified key and its current mapped value, ornullif there is no current mapping.- Parameters:
key- the key which wich the specified value is to be associatedremappingFunction- the remapping function to compute a value- Returns:
- a new map that has all entries of this map, but extended or overwritten with the newly computed one
- Throws:
NullPointerException- ifkeyorremappingFunctionis null- See Also:
-
computingIfAbsent
public IncrementalMap<K,V> computingIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) Attempts to compute a mapping for the specified key if there is no current mapped value.- Parameters:
key- the key which wich the specified value is to be associatedmappingFunction- the mapping function to compute a value- Returns:
- a new map that has all entries of this map, but extended or overwritten with the newly computed one
- Throws:
NullPointerException- ifkeyormappingFunctionis null- See Also:
-
computingIfPresent
public IncrementalMap<K,V> computingIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) Attempts to compute a mapping for the specified key and its current mapped value, if there is one.- Parameters:
key- the key which wich the specified value is to be associatedremappingFunction- the remapping function to compute a value- Returns:
- a new map that has all entries of this map, but extended or overwritten with the newly computed one
- Throws:
NullPointerException- ifkeyorremappingFunctionis null- See Also:
-
merging
public IncrementalMap<K,V> merging(K key, V newValue, BiFunction<? super V, ? super @Opt V, ? extends V> remappingFunction) -
entrySet
-
get
-
containsKey
- Specified by:
containsKeyin interfaceMap<K,V> - Overrides:
containsKeyin classAbstractMap<K,V>
-