Class ImmutableAbstractMap<K,V>

java.lang.Object
org.djutils.immutablecollections.ImmutableAbstractMap<K,V>
Type Parameters:
K - the key type of content of this Map
V - the value type of content of this Map
All Implemented Interfaces:
Serializable, ImmutableMap<K,V>
Direct Known Subclasses:
ImmutableHashMap, ImmutableLinkedHashMap, ImmutableTreeMap

public abstract class ImmutableAbstractMap<K,V> extends Object implements ImmutableMap<K,V>
An abstract base class for an immutable wrapper for a Map.

Copyright (c) 2016-2024 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See for project information https://djutils.org. The DJUTILS project is distributed under a three-clause BSD-style license, which can be found at https://djutils.org/docs/license.html.

Author:
Alexander Verbraeck, Peter Knoppers
See Also:
  • Field Details

    • copyOrWrap

      protected final Immutable copyOrWrap
      COPY stores a safe, internal copy of the collection; WRAP stores a pointer to the original collection.
    • cachedValues

      protected ImmutableCollection<V> cachedValues
      the cached values.
  • Constructor Details

    • ImmutableAbstractMap

      protected ImmutableAbstractMap(Map<K,V> map, Immutable copyOrWrap)
      Construct an abstract immutable map. Make sure that the argument is a safe copy of the map of the right type! Copying does not take place in the Abstract class!
      Parameters:
      map - Map<K,V>; a safe copy of the map to use for the immutable map
      copyOrWrap - Immutable; indicate whether the immutable is a copy or a wrap
  • Method Details

    • getUnderlyingMap

      protected Map<K,V> getUnderlyingMap()
      Return the raw underlying map.
      Returns:
      Map<K, V>; the raw underlying map
    • size

      public final int size()
      Description copied from interface: ImmutableMap
      Returns the number of elements in this immutable collection. If this immutable collection contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.
      Specified by:
      size in interface ImmutableMap<K,V>
      Returns:
      the number of elements in this immutable collection
    • isEmpty

      public final boolean isEmpty()
      Description copied from interface: ImmutableMap
      Returns true if this immutable collection contains no elements.
      Specified by:
      isEmpty in interface ImmutableMap<K,V>
      Returns:
      true if this immutable collection contains no elements
    • containsKey

      public final boolean containsKey(Object key)
      Description copied from interface: ImmutableMap
      Returns true if this map contains a mapping for the specified key. More formally, returns true if and only if this map contains a mapping for a key k such that (key==null ? k==null : key.equals(k)). (There can be at most one such mapping.)
      Specified by:
      containsKey in interface ImmutableMap<K,V>
      Parameters:
      key - Object; key whose presence in this map is to be tested
      Returns:
      true if this map contains a mapping for the specified key
    • containsValue

      public final boolean containsValue(Object value)
      Description copied from interface: ImmutableMap
      Returns true if this map maps one or more keys to the specified value. More formally, returns true if and only if this map contains at least one mapping to a value v such that (value==null ? v==null : value.equals(v)). This operation will probably require time linear in the map size for most implementations of the Map interface.
      Specified by:
      containsValue in interface ImmutableMap<K,V>
      Parameters:
      value - Object; value whose presence in this map is to be tested
      Returns:
      true if this map maps one or more keys to the specified value
    • get

      public final V get(Object key)
      Description copied from interface: ImmutableMap
      Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

      More formally, if this map contains a mapping from a key k to a value v such that (key==null ? k==null : key.equals(k)), then this method returns v; otherwise it returns null. (There can be at most one such mapping.)

      If this map permits null values, then a return value of null does not necessarily indicate that the map contains no mapping for the key; it's also possible that the map explicitly maps the key to null. The containsKey operation may be used to distinguish these two cases.

      Specified by:
      get in interface ImmutableMap<K,V>
      Parameters:
      key - Object; the key whose associated value is to be returned
      Returns:
      the value to which the specified key is mapped, or null if this map contains no mapping for the key
    • values

      public ImmutableCollection<V> values()
      Description copied from interface: ImmutableMap
      Returns a ImmutableCollection view of the values contained in this map.
      Specified by:
      values in interface ImmutableMap<K,V>
      Returns:
      an immutable collection view of the values contained in this map
    • isWrap

      public final boolean isWrap()
      Description copied from interface: ImmutableMap
      Return whether the internal storage is a wrapped pointer to the original map. If true, this means that anyone holding a pointer to this data structure can still change it. The users of the ImmutableMap itself can, however, not make any changes.
      Specified by:
      isWrap in interface ImmutableMap<K,V>
      Returns:
      boolean; whether the internal storage is a wrapped pointer to the original map
    • hashCode

      public int hashCode()
      Description copied from interface: ImmutableMap
      Force to redefine hashCode for the implementations of immutable collection classes.
      Specified by:
      hashCode in interface ImmutableMap<K,V>
      Overrides:
      hashCode in class Object
      Returns:
      the calculated hashCode
    • equals

      public boolean equals(Object obj)
      Description copied from interface: ImmutableMap
      Force to redefine equals for the implementations of immutable collection classes.
      Specified by:
      equals in interface ImmutableMap<K,V>
      Overrides:
      equals in class Object
      Parameters:
      obj - Object; the object to compare this collection with
      Returns:
      whether the objects are equal