Class WeakInterningHashSet<E>

java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractSet<E>
org.eclipse.emf.common.util.WeakInterningHashSet<E>
All Implemented Interfaces:
Serializable, Iterable<E>, Collection<E>, Set<E>, InterningSet<E>
Direct Known Subclasses:
Pool

public class WeakInterningHashSet<E> extends AbstractSet<E> implements InterningSet<E>, Serializable
An implementation of an interning set that keeps weak references to its element. This structure is particularly well suited for maintaining a cache of instances, e.g., a string pool. All the caveats about the behavior of the garbage collector that apply for a weak hash map apply for this implementation as well.
Since:
2.9
See Also:
  • Field Details

    • serialVersionUID

      protected static final long serialVersionUID
      See Also:
    • NULL_ENTRY

      protected static final WeakInterningHashSet.Entry<Object> NULL_ENTRY
      A special entry used by the iterator to represent the need to yield the null value for the subsequent call to next().
    • PRIME_CAPACITIES

      protected static final int[] PRIME_CAPACITIES
      The capacity used for the entries of the set will always be a prime number to help ensure uniform distribution of the hash codes. Each of these prime numbers is the smallest prime larger than 2^n, except for the last, which is the largest prime invalid input: '<' Integer.MAX_VALUE.
    • size

      protected int size
      The current size of the set.
    • capacityIndex

      protected transient int capacityIndex
      The current index within PRIME_CAPACITIES for the length of the entries.
    • containsNull

      protected transient boolean containsNull
      Whether or not this set contains the null value.
    • threshold

      protected transient int threshold
      The next size at which an rehash should occur.
    • modCount

      protected transient int modCount
      The modification count for fail fast iteration with ConcurrentModificationException.
    • entries

      protected transient WeakInterningHashSet.Entry<E>[] entries
      The table of linked entries.
    • internalQueue

      protected transient ReferenceQueue<E> internalQueue
      A queue used when creating internal entries and cleaning garbage collected references. The set must have must have either an internal queue or an external queue. All calls to cleanup() are guarded by whether this value is null.
    • externalQueue

      protected final transient ReferenceQueue<Object> externalQueue
      A queue used when creating external entries and subsequently for cleaning garbage collected references. Cleaning garbage collected references is the responsibility of this external queue provider. All calls to cleanup() are guarded by whether internalQueue value is null, so no cleanup takes place when there is an external reference queue.
  • Constructor Details

    • WeakInterningHashSet

      public WeakInterningHashSet()
      Creates an instance with a minimum capacity.
    • WeakInterningHashSet

      public WeakInterningHashSet(int minimumCapacity)
      Creates an instance with at least the given capacity.
  • Method Details

    • size

      public int size()
      Specified by:
      size in interface Collection<E>
      Specified by:
      size in interface Set<E>
      Specified by:
      size in class AbstractCollection<E>
    • grow

      public void grow(int minimumCapacity)
      Ensures that the set has at least the specified capacity. Higher capacity ensures fewer collisions hence faster lookup. This does nothing if the specified capacity is smaller than the current capacity.
    • hashCode

      protected int hashCode(E object)
      Returns the hash code of the object. This will never be called with null. A derived class may specialize this to compute an alternative hash code and should generally specialize asInstance(Object) and equals(Object, Object) The default implementation simply uses Object.hashCode().
    • equals

      protected boolean equals(E object, E otherObject)
      Returns whether the two objects are considered equal. This will never be called with null. The first argument will be an argument passed to one of the mutating methods of the set and the second will be a value already in the set. A derived class may specialize this to check for structural equality and should generally specialize asInstance(Object) and hashCode(Object). The default implementation simply uses the Object.equals(Object).
    • asInstance

      protected E asInstance(Object object)
      Returns the result of casting the object to E, or null if the object is not an instance of E. This method is called by remove(Object) and contains(Object) which use the returned null return false because an object that isn't an instance of E can't be (shouldn't be in the set) and so can't be removed. The default implementation cannot do a checked cast so it always returns the argument via an unchecked cast. This method should be specialized if hashCode(Object) or equals(Object, Object) are specialized.
    • newEntries

      protected WeakInterningHashSet.Entry<E>[] newEntries(int capacity)
      Creates a new array of entries with the specified capacity.
    • ensureCapacity

      protected boolean ensureCapacity()
      Ensures that 3/4 of current capacity is larger than the current size, i.e., that the size invalid input: '<'= threshold. If not, it reallocates the entries to the next prime capacity, i.e., it approximate doubles the capacity, and then rehashes the set. The return value indicates whether or note the entries where rehashed.
    • cleanup

      protected void cleanup()
      Polls the internalQueue and removes any garbage collected entries.
    • remove

      public boolean remove(Object object)
      Specified by:
      remove in interface Collection<E>
      Specified by:
      remove in interface Set<E>
      Overrides:
      remove in class AbstractCollection<E>
    • clear

      public void clear()
      Specialized to be more efficient.
      Specified by:
      clear in interface Collection<E>
      Specified by:
      clear in interface Set<E>
      Overrides:
      clear in class AbstractCollection<E>
    • add

      public boolean add(E object)
      Specified by:
      add in interface Collection<E>
      Specified by:
      add in interface Set<E>
      Overrides:
      add in class AbstractCollection<E>
    • intern

      public E intern(E object)
      Returns either the instance already contained in the set that's equal to the given object, or adds the object to the set and returns it.
      Specified by:
      intern in interface InterningSet<E>
      Parameters:
      object - the object to intern.
      Returns:
      the existing instance already contained in the set equal to the given object, or the object itself (or perhaps in some implementations, an object equal to the object itself).
    • get

      public E get(E object)
      Returns either the instance already contained in the set that's equal to the given object, or null if the object is not in the set.
      Specified by:
      get in interface InterningSet<E>
      Parameters:
      object - the object to intern.
      Returns:
      the existing instance already contained in the set equal to the given object, or null if the object is not in the set.
    • contains

      public boolean contains(Object object)
      Specified by:
      contains in interface Collection<E>
      Specified by:
      contains in interface Set<E>
      Overrides:
      contains in class AbstractCollection<E>
    • iterator

      public Iterator<E> iterator()
      Specified by:
      iterator in interface Collection<E>
      Specified by:
      iterator in interface Iterable<E>
      Specified by:
      iterator in interface Set<E>
      Specified by:
      iterator in class AbstractCollection<E>
    • index

      protected static int index(int hashCode, int capacity)
      Returns the index in the entries for the given hash code and capacity.
    • getEntry

      protected WeakInterningHashSet.Entry<E> getEntry(int hashCode)
      Gets the first entry in the table with exactly the given hash code. It's very useful to call WeakInterningHashSet.Entry.getNextEntry() to yield the next entry with exactly this same hash code.
    • nullEntry

      protected WeakInterningHashSet.Entry<E> nullEntry()
      Returns the singleton entry representing the null value's entry during iteration.
    • newEntry

      protected final WeakInterningHashSet.Entry<E> newEntry(E object, int hashCode)
      Creates a new entry for the given referent and the given hash code. Depending on whether there's an internalQueue or externalQueue it calls newInternalEntry(Object, int) or newExternalEntry(Object, int) respectively.
    • newInternalEntry

      protected WeakInterningHashSet.Entry<E> newInternalEntry(E object, int hashCode)
      Creates a new entry for the given referent and the given hash code and the internalQueue.
    • newExternalEntry

      protected WeakInterningHashSet.Entry<E> newExternalEntry(E object, int hashCode)
      Creates a new entry for the given referent and the given hash code managed by the externalQueue.
    • putEntry

      protected void putEntry(int index, WeakInterningHashSet.Entry<E> entry)
      Puts the entry into the entries linking up the chain for collision handling.
    • addEntry

      protected void addEntry(int index, WeakInterningHashSet.Entry<E> entry)
      Adds a new entry to the set at the given given index in the entries. It ensures the capacity is sufficient, increases the size of the set, increments the modCount, and puts the entry into the set.
    • removeEntry

      protected void removeEntry(WeakInterningHashSet.Entry<E> entry)
      Remove an existing entry from the set. If successful, it decreases the size of the set and increments the modCount.
    • removeEntry

      protected boolean removeEntry(int index, WeakInterningHashSet.Entry<E> entry)
      Finds the entry at the given index the table and prune if from the collision chain. Returns whether or not the entry was actually removed.
    • dump

      public void dump()
      Dumps information about the current state of the set.