Class BasicEMap<K,V>

java.lang.Object
org.eclipse.emf.common.util.BasicEMap<K,V>
All Implemented Interfaces:
Serializable, Cloneable, Iterable<Map.Entry<K,V>>, Collection<Map.Entry<K,V>>, List<Map.Entry<K,V>>, SequencedCollection<Map.Entry<K,V>>, EList<Map.Entry<K,V>>, EMap<K,V>

public class BasicEMap<K,V> extends Object implements EMap<K,V>, Cloneable, Serializable
A highly extensible map implementation.
See Also:
  • Field Details

    • delegateEList

      protected transient EList<BasicEMap.Entry<K,V>> delegateEList
      The underlying list of entries.
    • size

      protected transient int size
      The size of the map.
    • entryData

      protected transient BasicEList<BasicEMap.Entry<K,V>>[] entryData
      The array of entry lists into which the hash codes are indexed.
    • modCount

      protected transient int modCount
      The modification indicator used to ensure iterator integrity.
    • view

      protected transient BasicEMap.View<K,V> view
      The various alternative views of the map.
  • Constructor Details

    • BasicEMap

      public BasicEMap()
      Creates an empty instance.
    • BasicEMap

      public BasicEMap(int initialCapacity)
      Creates an empty instance with the given capacity.
      Parameters:
      initialCapacity - the initial capacity of the map before it must grow.
      Throws:
      IllegalArgumentException - if the initialCapacity is negative.
    • BasicEMap

      public BasicEMap(Map<? extends K,? extends V> map)
      Creates an instance that is a copy of the map.
      Parameters:
      map - the initial contents of the map.
  • Method Details

    • initializeDelegateEList

      protected void initializeDelegateEList()
      Initializes the delegateEList. This implementation illustrates the precise pattern that is used to delegate a list implementation's callback methods to the map implementation.
    • newEntryData

      protected BasicEList<BasicEMap.Entry<K,V>>[] newEntryData(int capacity)
      Returns new allocated entry data storage. Clients may override this to create typed storage, but it's not likely. The cost of type checking via a typed array is negligible.
      Parameters:
      capacity - the capacity of storage needed.
      Returns:
      new entry data storage.
    • ensureEntryDataExists

      protected void ensureEntryDataExists()
      Ensures that the entry data is created and is populated with contents of the delegate list.
    • newList

      protected BasicEList<BasicEMap.Entry<K,V>> newList()
      Returns a new allocated list of entries. Clients may override this to create typed storage. The cost of type checking via a typed array is negligible. The type must be kept in synch with newEntry.
      Returns:
      a new list of entries.
      See Also:
    • newEntry

      protected BasicEMap.Entry<K,V> newEntry(int hash, K key, V value)
      Returns a new entry. The key is validated and the value is validated. Clients may override this to create typed storage. The type must be kept in synch with newEntry.
      Parameters:
      hash - the cached hash code of the key.
      key - the key.
      value - the value.
      Returns:
      a new entry.
      See Also:
    • putEntry

      protected V putEntry(BasicEMap.Entry<K,V> entry, V value)
      Sets the value of the entry, and returns the former value. The value is validated.
      Parameters:
      entry - the entry.
      value - the value.
      Returns:
      the former value, or null.
    • useEqualsForKey

      protected boolean useEqualsForKey()
      Returns whether equals rather than == should be used to compare keys. The default is to return true but clients can optimize performance by returning false. The performance difference is highly significant.
      Returns:
      whether equals rather than == should be used to compare keys.
    • useEqualsForValue

      protected boolean useEqualsForValue()
      Returns whether equals rather than == should be used to compare values. The default is to return true but clients can optimize performance by returning false. The performance difference is highly significant.
      Returns:
      whether equals rather than == should be used to compare values.
    • resolve

      protected V resolve(K key, V value)
      Resolves the value associated with the key and returns the result. This implementation simply returns the value; clients can use this to transform objects as they are fetched.
      Parameters:
      key - the key of an entry.
      value - the value of an entry.
      Returns:
      the resolved value.
    • validateKey

      protected void validateKey(K key)
      Validates a new key. This implementation does nothing, but clients may throw runtime exceptions in order to handle constraint violations.
      Parameters:
      key - the new key.
      Throws:
      IllegalArgumentException - if a constraint prevents the object from being added.
    • validateValue

      protected void validateValue(V value)
      Validates a new key. This implementation does nothing, but clients may throw runtime exceptions in order to handle constraint violations.
      Parameters:
      value - the new value.
      Throws:
      IllegalArgumentException - if a constraint prevents the object from being added.
    • didAdd

      protected void didAdd(BasicEMap.Entry<K,V> entry)
      Called to indicate that the entry has been added. This implementation does nothing; clients can use this to monitor additions to the map.
      Parameters:
      entry - the added entry.
    • didModify

      protected void didModify(BasicEMap.Entry<K,V> entry, V oldValue)
      Called to indicate that the entry has an updated value. This implementation does nothing; clients can use this to monitor value changes in the map.
      Parameters:
      entry - the new entry.
    • didRemove

      protected void didRemove(BasicEMap.Entry<K,V> entry)
      Called to indicate that the entry has been removed. This implementation does nothing; clients can use this to monitor removals from the map.
      Parameters:
      entry - the removed entry.
    • didClear

      protected void didClear(BasicEList<BasicEMap.Entry<K,V>>[] oldEntryData)
      Called to indicate that the map has been cleared. This implementation does calls didRemove for each entry; clients can use this to monitor clearing of the map.
      Parameters:
      oldEntryData - the removed entries.
    • size

      public int size()
      Returns the number of entries in the map.
      Specified by:
      size in interface Collection<K>
      Specified by:
      size in interface List<K>
      Returns:
      the number of entries in the map.
    • isEmpty

      public boolean isEmpty()
      Returns whether the map has zero size.
      Specified by:
      isEmpty in interface Collection<K>
      Specified by:
      isEmpty in interface List<K>
      Returns:
      whether the map has zero size.
    • indexOfKey

      public int indexOfKey(Object key)
      Description copied from interface: EMap
      Returns the index in the list of the entry with the given key, or -1, if there is no such entry.
      Specified by:
      indexOfKey in interface EMap<K,V>
      Parameters:
      key - a key.
      Returns:
      the index of the entry with the given key.
    • containsKey

      public boolean containsKey(Object key)
      Description copied from interface: EMap
      Returns whether the key is associated with a value.
      Specified by:
      containsKey in interface EMap<K,V>
      Parameters:
      key - a key associated with a value.
      Returns:
      whether the key is associated with a value.
    • containsValue

      public boolean containsValue(Object value)
      Description copied from interface: EMap
      Returns whether the value is associated with a key.
      Specified by:
      containsValue in interface EMap<K,V>
      Parameters:
      value - a value associated with a key.
      Returns:
      whether the value is associated with a key.
    • get

      public V get(Object key)
      Description copied from interface: EMap
      Returns the value associated with the key. The key, the value, or both may be null.
      Specified by:
      get in interface EMap<K,V>
      Parameters:
      key - the key of the value.
      Returns:
      the value associated with the key.
    • put

      public V put(K key, V value)
      Description copied from interface: EMap
      Associates the key with the value and returns the value previously associated with the key, or null. The key, the value, or both may be null. Either the existing entry is updated, or a new entry is added to the end of the list.
      Specified by:
      put in interface EMap<K,V>
      Parameters:
      key - the key of the value.
      value - the value associated with the key.
      Returns:
      the value formerly associated with the key, or null.
    • doPut

      protected void doPut(BasicEMap.Entry<K,V> entry)
      Adds the new entry to the map.
      Parameters:
      entry - the new entry.
    • removeKey

      public V removeKey(Object key)
      Description copied from interface: EMap
      Disassociates the key from its value, and returns the value formerly associated with the key. An entry is removed from the list, if the key is found.
      Specified by:
      removeKey in interface EMap<K,V>
      Parameters:
      key - the key of a value.
      Returns:
      the value formerly associated with the key.
    • doRemove

      protected void doRemove(BasicEMap.Entry<K,V> entry)
      Removes the entry from the map.
      Parameters:
      entry - an entry in the map.
    • removeEntry

      protected V removeEntry(int index, int entryIndex)
      Removes the fully indexed entry from the map and returns it's value.
      Parameters:
      index - the index in the entry data
      entryIndex - the index in the list of entries.
      Returns:
      the value of the entry.
    • putAll

      public void putAll(Map<? extends K,? extends V> map)
      Description copied from interface: EMap
      Puts each Map.Entry of the given map into this one.
      Specified by:
      putAll in interface EMap<K,V>
      Parameters:
      map - the map of entries.
      See Also:
    • putAll

      public void putAll(EMap<? extends K,? extends V> map)
      Description copied from interface: EMap
      Puts each Map.Entry of the given map into this one.
      Specified by:
      putAll in interface EMap<K,V>
      Parameters:
      map - the map of entries.
      See Also:
    • doClear

      protected void doClear()
      Clears the map.
    • doMove

      protected void doMove(BasicEMap.Entry<K,V> entry)
      Increments the modification count.
    • clone

      public Object clone()
      Returns a shallow copy of this map.
      Overrides:
      clone in class Object
      Returns:
      a shallow copy of this map.
    • map

      public Map<K,V> map()
      Description copied from interface: EMap
      Returns a map view.
      Specified by:
      map in interface EMap<K,V>
      Returns:
      a map view.
    • keySet

      public Set<K> keySet()
      Description copied from interface: EMap
      Returns a set view of the keys of the entries.
      Specified by:
      keySet in interface EMap<K,V>
      Returns:
      a set view of the keys of the entries.
    • values

      public Collection<V> values()
      Description copied from interface: EMap
      Returns a collection view the values of the entries.
      Specified by:
      values in interface EMap<K,V>
      Returns:
      a collection view the values of the entries.
    • entrySet

      public Set<Map.Entry<K,V>> entrySet()
      Description copied from interface: EMap
      Returns a set view of the entries.
      Specified by:
      entrySet in interface EMap<K,V>
      Returns:
      a set view of the entries.
    • hashOf

      protected int hashOf(Object key)
      Called to return the hash code of the key.
      Parameters:
      key - the key.
      Returns:
      the hash code of the object.
    • indexOf

      protected int indexOf(int hash)
      Called to return the entry data index corresponding to the hash code.
      Parameters:
      hash - the hash code.
      Returns:
      the index corresponding to the hash code.
    • entryForKey

      protected BasicEMap.Entry<K,V> entryForKey(int index, int hash, Object key)
      Called to return the entry given the index, the hash, and the key.
      Parameters:
      index - the entry data index of the key.
      hash - the hash code of the key.
      key - the key.
      Returns:
      the entry.
    • entryIndexForKey

      protected int entryIndexForKey(int index, int hash, Object key)
      Called to return the entry list index given the index, the hash, and the key.
      Parameters:
      index - the entry data index of the key.
      hash - the hash code of the key.
      key - the key.
      Returns:
      the entry list index.
    • grow

      protected boolean grow(int minimumCapacity)
      Grows the capacity of the map to ensure that no additional growth is needed until the size exceeds the specified minimum capacity.
    • contains

      public boolean contains(Object object)
      Delegates to delegateEList.
      Specified by:
      contains in interface Collection<K>
      Specified by:
      contains in interface List<K>
    • containsAll

      public boolean containsAll(Collection<?> collection)
      Delegates to delegateEList.
      Specified by:
      containsAll in interface Collection<K>
      Specified by:
      containsAll in interface List<K>
    • indexOf

      public int indexOf(Object object)
      Delegates to delegateEList.
      Specified by:
      indexOf in interface List<K>
    • lastIndexOf

      public int lastIndexOf(Object object)
      Delegates to delegateEList.
      Specified by:
      lastIndexOf in interface List<K>
    • toArray

      public Object[] toArray()
      Delegates to delegateEList.
      Specified by:
      toArray in interface Collection<K>
      Specified by:
      toArray in interface List<K>
    • toArray

      public <T> T[] toArray(T[] array)
      Delegates to delegateEList.
      Specified by:
      toArray in interface Collection<K>
      Specified by:
      toArray in interface List<K>
    • get

      public BasicEMap.Entry<K,V> get(int index)
      Delegates to delegateEList.
      Specified by:
      get in interface List<K>
    • set

      public Map.Entry<K,V> set(int index, Map.Entry<K,V> object)
      Delegates to delegateEList.
      Specified by:
      set in interface List<K>
    • add

      public boolean add(Map.Entry<K,V> object)
      Delegates to delegateEList.
      Specified by:
      add in interface Collection<K>
      Specified by:
      add in interface List<K>
    • add

      public void add(int index, Map.Entry<K,V> object)
      Delegates to delegateEList.
      Specified by:
      add in interface List<K>
    • addAll

      public boolean addAll(Collection<? extends Map.Entry<K,V>> collection)
      Delegates to delegateEList.
      Specified by:
      addAll in interface Collection<K>
      Specified by:
      addAll in interface List<K>
    • addAll

      public boolean addAll(int index, Collection<? extends Map.Entry<K,V>> collection)
      Delegates to delegateEList.
      Specified by:
      addAll in interface List<K>
    • remove

      public boolean remove(Object object)
      Delegates to delegateEList.
      Specified by:
      remove in interface Collection<K>
      Specified by:
      remove in interface List<K>
    • removeAll

      public boolean removeAll(Collection<?> collection)
      Delegates to delegateEList.
      Specified by:
      removeAll in interface Collection<K>
      Specified by:
      removeAll in interface List<K>
    • remove

      public Map.Entry<K,V> remove(int index)
      Delegates to delegateEList.
      Specified by:
      remove in interface List<K>
    • retainAll

      public boolean retainAll(Collection<?> collection)
      Delegates to delegateEList.
      Specified by:
      retainAll in interface Collection<K>
      Specified by:
      retainAll in interface List<K>
    • clear

      public void clear()
      Delegates to delegateEList.
      Specified by:
      clear in interface Collection<K>
      Specified by:
      clear in interface List<K>
    • move

      public void move(int index, Map.Entry<K,V> object)
      Delegates to delegateEList.
      Specified by:
      move in interface EList<K>
      Parameters:
      index - the position of the object after the move.
      object - the object to move.
    • move

      public Map.Entry<K,V> move(int targetIndex, int sourceIndex)
      Delegates to delegateEList.
      Specified by:
      move in interface EList<K>
      Parameters:
      targetIndex - the position of the object after the move.
      sourceIndex - the position of the object before the move.
      Returns:
      the moved object.
    • iterator

      public Iterator<Map.Entry<K,V>> iterator()
      Delegates to delegateEList.
      Specified by:
      iterator in interface Collection<K>
      Specified by:
      iterator in interface Iterable<K>
      Specified by:
      iterator in interface List<K>
    • listIterator

      public ListIterator<Map.Entry<K,V>> listIterator()
      Delegates to delegateEList.
      Specified by:
      listIterator in interface List<K>
    • listIterator

      public ListIterator<Map.Entry<K,V>> listIterator(int index)
      Delegates to delegateEList.
      Specified by:
      listIterator in interface List<K>
    • subList

      public List<Map.Entry<K,V>> subList(int start, int end)
      Delegates to delegateEList.
      Specified by:
      subList in interface List<K>
    • hashCode

      public int hashCode()
      Specified by:
      hashCode in interface Collection<K>
      Specified by:
      hashCode in interface List<K>
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object object)
      Specified by:
      equals in interface Collection<K>
      Specified by:
      equals in interface List<K>
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Delegates to delegateEList.
      Overrides:
      toString in class Object