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 keys
V - the type of map values
All Implemented Interfaces:
Map<K,V>

public class IncrementalMap<K,V> extends AbstractMap<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
  • Method Details

    • empty

      public static <K, V> IncrementalMap<K,V> empty()
      Returns an empty map.
      Type Parameters:
      K - the type of map keys
      V - the type of map values
      Returns:
      a map containing no entries
    • computeChange

      public IncrementalMap.Change<K,V,IncrementalMap<K,V>> computeChange(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)
      Attempts to compute a mapping for the specified key and its current mapped value, or null if there is no current mapping.
      Parameters:
      key - the key which wich the specified value is to be associated
      remappingFunction - 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 - if key or remappingFunction is null
      See Also:
    • putting

      public IncrementalMap<K,V> putting(K key, @Opt V value)
      Returns a variant of this map where the given key is mapped to the given value.
      Parameters:
      key - the key
      value - the new value to map the key to
      Returns:
      a new map that has all entries of this map, except that key is mapped to value if nonnull, or unmapped otherwise
      Throws:
      NullPointerException - if key or value is null
    • removing

      public IncrementalMap<K,V> removing(K key)
      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 key is unmapped
      Throws:
      NullPointerException - if key is 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, or null if there is no current mapping.
      Parameters:
      key - the key which wich the specified value is to be associated
      remappingFunction - 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 - if key or remappingFunction is 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 associated
      mappingFunction - 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 - if key or mappingFunction is 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 associated
      remappingFunction - 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 - if key or remappingFunction is null
      See Also:
    • merging

      public IncrementalMap<K,V> merging(K key, V newValue, BiFunction<? super V,? super @Opt V,? extends V> remappingFunction)
    • entrySet

      public Set<Map.Entry<K,V>> entrySet()
      Specified by:
      entrySet in interface Map<K,V>
      Specified by:
      entrySet in class AbstractMap<K,V>
    • get

      public V get(Object key)
      Specified by:
      get in interface Map<K,V>
      Overrides:
      get in class AbstractMap<K,V>
    • containsKey

      public boolean containsKey(Object key)
      Specified by:
      containsKey in interface Map<K,V>
      Overrides:
      containsKey in class AbstractMap<K,V>