Class OrderedListMap<K,V>

java.lang.Object
org.javimmutable.collections.listmap.OrderedListMap<K,V>
All Implemented Interfaces:
Serializable, Iterable<IMapEntry<K,IList<V>>>, ICollection<IMapEntry<K,IList<V>>>, IListMap<K,V>, InvariantCheckable, IStreamable<IMapEntry<K,IList<V>>>, Mapped<K,IList<V>>, SplitableIterable<IMapEntry<K,IList<V>>>

@Immutable public class OrderedListMap<K,V> extends Object implements Serializable
IListMap implementation that allows keys to be traversed in the same order as they were inserted into the collection.
See Also:
  • Field Details

    • emptyList

      protected final IList<V> emptyList
    • contents

      protected final IMap<K,IList<V>> contents
  • Method Details

    • of

      public static <K, V> OrderedListMap<K,V> of()
    • checkInvariants

      public void checkInvariants()
      Description copied from interface: InvariantCheckable
      Checks invariants of implementing class.
      Specified by:
      checkInvariants in interface InvariantCheckable
    • create

      protected IListMap<K,V> create(IMap<K,IList<V>> map)
      Implemented by derived classes to create a new instance of the appropriate class.
    • getList

      @Nonnull public IList<V> getList(@Nonnull K key)
      Description copied from interface: IListMap
      Return the list associated with key or an empty list if no list is associated.
      Specified by:
      getList in interface IListMap<K,V>
      Parameters:
      key - identifies the value to retrieve
      Returns:
      list associated with key or an empty list if no value is associated
    • assign

      @Nonnull public IListMap<K,V> assign(@Nonnull K key, @Nonnull IList<V> value)
      Description copied from interface: IListMap
      Sets the list associated with a specific key. Key and value must be non-null. If the key already has a list in the map the old list is discarded and the new list is stored in its place. Returns a new IListMap reflecting any changes. The original map is always left unchanged.
      Specified by:
      assign in interface IListMap<K,V>
      Parameters:
      key - non-null key
      value - list of possibly null values to use for this key
      Returns:
      new map reflecting the change
    • insert

      @Nonnull public IListMap<K,V> insert(@Nonnull K key, @Nullable V value)
      Description copied from interface: IListMap
      Add value to the list for the specified key. Note that this can create duplicate values in the list.
      Specified by:
      insert in interface IListMap<K,V>
    • delete

      @Nonnull public IListMap<K,V> delete(@Nonnull K key)
      Description copied from interface: IListMap
      Deletes the entry for the specified key (if any). Returns a new map if the value was deleted or the current map if the key was not contained in the map.
      Specified by:
      delete in interface IListMap<K,V>
      Parameters:
      key - non-null key
      Returns:
      same or different map depending on whether key was removed
    • size

      public int size()
      Specified by:
      size in interface ICollection<K>
      Returns:
      number of values in the collection
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface ICollection<K>
      Returns:
      true only if collection contains no values
    • keys

      @Nonnull public IStreamable<K> keys()
      Description copied from interface: IListMap
      Creates an IStreamable to access all of the Map's keys.
      Specified by:
      keys in interface IListMap<K,V>
    • values

      @Nonnull public IStreamable<V> values(@Nonnull K key)
      Description copied from interface: IListMap
      Creates an IStreamable to access all of the specified key's list. If no list exists for key an empty IStreamable is returned.
      Specified by:
      values in interface IListMap<K,V>
      Returns:
      a (possibly empty) IStreamable for traversing the values associated with key
    • entries

      @Nonnull public IStreamable<IMapEntry<K,IList<V>>> entries()
      Description copied from interface: IListMap
      Creates an IStreamable to access all of the Map's entries.
      Specified by:
      entries in interface IListMap<K,V>
    • insert

      @Nonnull public IListMap<K,V> insert(@Nonnull IMapEntry<K,IList<V>> e)
      Description copied from interface: IListMap
      Add key/value entry to the map, replacing any existing entry with same key.
      Specified by:
      insert in interface ICollection<K>
      Specified by:
      insert in interface IListMap<K,V>
    • insertAll

      @Nonnull public IListMap<K,V> insertAll(@Nonnull Iterator<? extends IMapEntry<K,IList<V>>> iterator)
      Description copied from interface: ICollection
      Add all values to the container in some manner appropriate to the implementation.
      Specified by:
      insertAll in interface ICollection<K>
      Specified by:
      insertAll in interface IListMap<K,V>
    • iterator

      @Nonnull public SplitableIterator<IMapEntry<K,IList<V>>> iterator()
      Description copied from interface: IStreamable
      Overridden here to require implementations to return a SplitableIterator rather than a basic Iterator. This is necessary to allow composition of new objects from methods like keys() and values().
      Specified by:
      iterator in interface IStreamable<K>
      Specified by:
      iterator in interface Iterable<K>
      Specified by:
      iterator in interface SplitableIterable<K>
    • getSpliteratorCharacteristics

      public int getSpliteratorCharacteristics()
      Specified by:
      getSpliteratorCharacteristics in interface IStreamable<K>
      Returns:
      characteristics value used when creating Spliterators
    • get

      @Nullable public IList<V> get(K key)
      Description copied from interface: Mapped
      Return the value associated with key or null if no value is associated. Note that if null is an acceptable value to the container then this method will be ambiguous and find() should be used instead.
      Specified by:
      get in interface Mapped<K,V>
      Parameters:
      key - identifies the value to retrieve
      Returns:
      value associated with key or null if no value is associated
    • getValueOr

      public IList<V> getValueOr(K key, IList<V> defaultValue)
      Description copied from interface: Mapped
      Return the value associated with key or defaultValue if no value is associated. Note that if defaultValue is an acceptable value to the container then this method will be ambiguous and find() should be used instead.
      Specified by:
      getValueOr in interface Mapped<K,V>
      Parameters:
      key - identifies the value to retrieve
      defaultValue - value to return if no entry exists for key
      Returns:
      value associated with key or defaultValue if no value is associated
    • find

      @Nonnull public Maybe<IList<V>> find(K key)
      Description copied from interface: Mapped
      Return a Holder containing the value associated wth the key or an empty Holder if no value is associated with the key.
      Specified by:
      find in interface Mapped<K,V>
      Parameters:
      key - identifies the value to retrieve
      Returns:
      possibly empty Holder containing any value associated with the key
    • deleteAll

      @Nonnull public IListMap<K,V> deleteAll()
      Specified by:
      deleteAll in interface ICollection<K>
      Specified by:
      deleteAll in interface IListMap<K,V>
      Returns:
      an equivalent collection with no values
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • checkListMapInvariants

      protected void checkListMapInvariants()
    • copyList

      protected IList<V> copyList(IList<V> original)
      Overridable by derived classes to create a compatible copy of the specified list. Default implementation simply returns the original.