Class AbstractMap<K,V>

java.lang.Object
org.apache.sis.internal.util.AbstractMap<K,V>
Type Parameters:
K - the type of keys maintained by the map.
V - the type of mapped values.
All Implemented Interfaces:
Map<K,V>
Direct Known Subclasses:
AuthorityCodes, CharacteristicMap, CharacteristicTypeMap, FilteredCodes, LinearTransformBuilder.ControlPoints, MergedProperties, Properties, StaxDataStore.Config

public abstract class AbstractMap<K,V> extends Object implements Map<K,V>
An alternative to java.util.AbstractMap using different implementation strategies. Instead of providing default method implementations on top of entrySet(), this base class uses more often the Map.get(Object) method with the assumption that the map cannot contain null values, or use a special-purpose entryIterator() which can reduce the amount of object creations.

This base class is for Apache SIS internal purpose only. Do not use! This class is less robust than the JDK one (e.g. does not accept null values), forces subclasses to implement more methods, uses a non-standard entryIterator(), and may change in any future SIS version.

This AbstractMap implementation makes the following assumptions. Do not use this class if any of those assumptions do not hold!

  • The map cannot contain null value.
  • The map cannot contain references to itself, directly or indirectly, in the keys or in the values.

Read-only subclasses need to implement the following methods:

Read/write subclasses can implement those additional methods:

Since:
0.5
Version:
0.8
  • Constructor Details

    • AbstractMap

      protected AbstractMap()
      For subclass constructors.
  • Method Details

    • size

      public int size()
      Returns the number of key-value mappings in this map. The default implementation count the number of values returned by entryIterator(). Subclasses should implement a more efficient method.
      Specified by:
      size in interface Map<K,V>
    • isEmpty

      public boolean isEmpty()
      Returns true if this map contains no element.
      Specified by:
      isEmpty in interface Map<K,V>
      Returns:
      true if this map contains no element.
    • containsKey

      public boolean containsKey(Object key)
      Returns true if this map contains a value for the given name. The default implementation assumes that the map cannot contain null values.
      Specified by:
      containsKey in interface Map<K,V>
      Parameters:
      key - the key for which to test the presence of a value.
      Returns:
      true if the map contains a non-null value for the given key.
    • containsValue

      public boolean containsValue(Object value)
      Returns true if this map contains the given value. The default implementation iterates over all values using the entryIterator().
      Specified by:
      containsValue in interface Map<K,V>
      Parameters:
      value - the value for which to test the presence.
      Returns:
      true if the map contains the given value.
    • getOrDefault

      public V getOrDefault(Object key, V defaultValue)
      Returns the value for the given key, or defaultValue if none. The default implementation assumes that the map cannot contain null values.
      Specified by:
      getOrDefault in interface Map<K,V>
      Parameters:
      key - the key for which to get the value.
      defaultValue - the value to return if this map does not have an entry for the given key.
      Returns:
      the value for the given key, or defaultValue if none.
    • message

      static String message(boolean add)
      The message to gives to the exception to be thrown in case of unsupported operation.
      Parameters:
      add - true if this method is invoked from addKey(Object) or addValue(Object).
    • clear

      public void clear() throws UnsupportedOperationException
      Removes all entries in this map. The default operation throws UnsupportedOperationException.
      Specified by:
      clear in interface Map<K,V>
      Throws:
      UnsupportedOperationException
    • remove

      public V remove(Object key) throws UnsupportedOperationException
      Removes the entry for the given key in this map. The default operation throws UnsupportedOperationException.
      Specified by:
      remove in interface Map<K,V>
      Parameters:
      key - the key of the entry to remove.
      Returns:
      the previous value, or null if none.
      Throws:
      UnsupportedOperationException
    • put

      public V put(K key, V value) throws UnsupportedOperationException
      Adds an entry for the given key in this map. The default operation throws UnsupportedOperationException.
      Specified by:
      put in interface Map<K,V>
      Parameters:
      key - the key of the entry to remove.
      value - the value to associate to the given key.
      Returns:
      the previous value, or null if none.
      Throws:
      UnsupportedOperationException
    • putAll

      public void putAll(Map<? extends K,? extends V> map) throws UnsupportedOperationException
      Puts all entries of the given map in this map.
      Specified by:
      putAll in interface Map<K,V>
      Parameters:
      map - the other map from which to copy the entries.
      Throws:
      UnsupportedOperationException
    • addKey

      protected boolean addKey(K key) throws UnsupportedOperationException
      Adds the given key in this map. Implementation of this method shall generate a corresponding value. The default operation throws UnsupportedOperationException.
      Parameters:
      key - the key to add.
      Returns:
      true if this map changed as a result of this operation.
      Throws:
      UnsupportedOperationException
    • addValue

      protected boolean addValue(V value) throws UnsupportedOperationException
      Adds the given value in this map. Implementation of this method shall generate a corresponding key. The default operation throws UnsupportedOperationException.
      Parameters:
      value - the value to add.
      Returns:
      true if this map changed as a result of this operation.
      Throws:
      UnsupportedOperationException
    • keySet

      public Set<K> keySet()
      Returns a view over the keys in this map. The returned set supports the Set.add(Object) operation if the enclosing map implements the addKey(Object) method.

      The default implementation does not cache the set on the assumption that it is very quick to create and usually not retained for a long time (we often want only its iterator). Caching the set would require a volatile field for thread safety, which also has cost.

      Specified by:
      keySet in interface Map<K,V>
      Returns:
      a view of the keys in this map.
    • values

      public Collection<V> values()
      Returns a view over the values in this map. The returned collection supports the Collection.add(Object) operation if the enclosing map implements the addValue(Object) method.

      The default implementation does not cache the collection on the assumption that it is very quick to create and usually not retained for a long time.

      Specified by:
      values in interface Map<K,V>
      Returns:
      a view of the values in this map.
    • entrySet

      public Set<Map.Entry<K,V>> entrySet()
      Returns a view over the entries in this map.

      The default implementation does not cache the set on the assumption that it is very quick to create and usually not retained for a long time.

      Specified by:
      entrySet in interface Map<K,V>
      Returns:
      a view of the entries in this map.
    • entryIterator

      protected abstract AbstractMap.EntryIterator<K,V> entryIterator()
      Returns an iterator over the entries in this map. It is okay (but not required) to return null if the map is empty.
      Tip: AbstractMap<K,V>.KeyIterator provides a convenient implementation for simple cases.
      Returns:
      an iterator over the entries in this map, or null.
    • equals

      public boolean equals(Object object)
      Compares this map with the given object for equality.
      Specified by:
      equals in interface Map<K,V>
      Overrides:
      equals in class Object
      Parameters:
      object - the other object to compare with this map.
      Returns:
      true if both objects are equal.
    • hashCode

      public int hashCode()
      Computes a hash code value for this map.
      Specified by:
      hashCode in interface Map<K,V>
      Overrides:
      hashCode in class Object
      Returns:
      a hash code value.
    • toString

      public String toString()
      Returns a string representation of this map. The default implementation is different than the java.util.AbstractMap one, as it uses a tabular format rather than formatting all entries on a single line.
      Overrides:
      toString in class Object
      Returns:
      a string representation of this map.