org.eclipse.internal.xtend.util
Class WeakInterningHashSet<E>

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractSet<E>
          extended by org.eclipse.internal.xtend.util.WeakInterningHashSet<E>
All Implemented Interfaces:
java.io.Serializable, java.lang.Iterable<E>, java.util.Collection<E>, java.util.Set<E>
Direct Known Subclasses:
QualifiedNameWithDelimiter.QualifiedNameCache, QualifiedNameWithDelimiter.StringCache

public class WeakInterningHashSet<E>
extends java.util.AbstractSet<E>
implements java.io.Serializable

Copied from org.eclipse.emf.common.util, since it is only available in EMF >= 2.9. TODO: Remove once Target Platform allows usage of newer EMF version. An implementation of an AbstractSet 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.

See Also:
Serialized Form

Nested Class Summary
protected static class WeakInterningHashSet.Entry<E>
          A weak reference holder that caches the hash code of the referent and is chained in the entries to handle collisions.
 
Field Summary
protected static int CAPACITY_MASK
          The mask for the lower five bits of the modCount representing the capacity index in PRIME_CAPACITIES.
protected  WeakInterningHashSet.Entry<E>[] entries
          The table of linked entries.
protected static int MOD_COUNT_INCREMENT
          The regular increment for the modCount representing a structural change to the set.
protected  int modCount
          The mod count is used to encode several things.
protected static int NULL_BIT_INCREMENT
          The increment for modCount representing the addition/removal of the null value to/from the set.
protected static WeakInterningHashSet.Entry<java.lang.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().
protected static 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.
protected  java.lang.ref.ReferenceQueue<E> queue
          The queue used for polling garbage collected references.
protected static long serialVersionUID
           
protected  int size
          The current size of the set.
 
Constructor Summary
WeakInterningHashSet()
           
 
Method Summary
 boolean add(E object)
          
protected  void addEntry(WeakInterningHashSet.Entry<E> entry)
          Adds a new entry to the set.
protected  void cleanup()
          Polls the queue and removes any garbage collected entries.
 boolean contains(java.lang.Object object)
          
protected  WeakInterningHashSet.Entry<E> createEntry(E object, int hashCode)
          Creates a new entry for the given referent and the given hash code.
protected  void dump()
           
protected  void ensureCapacity()
          Ensures that 3/4 of current capacity is larger than the current size.
protected  boolean equals(java.lang.Object object, java.lang.Object otherObject)
          Returns true if the two objects are to be considered equal.
 E get(E object)
           
protected  WeakInterningHashSet.Entry<E> getEntry(int hashCode)
          Gets the first entry in the table with exactly the given hash code.
 void grow(int minimumCapacity)
          Ensures that the set has at least the specifies capacity.
protected  int hashCode(java.lang.Object object)
          Returns the hash code of the object.
protected  int index(int hashCode)
          Returns the index in the entries for the given hash code.
 E intern(E object)
           
 java.util.Iterator<E> iterator()
          
protected  WeakInterningHashSet.Entry<E>[] newEntries(int capacity)
          Creates a new array of entries with the specified capacity.
protected  WeakInterningHashSet.Entry<E> nullEntry()
          Returns the singleton entry representing the null value.
protected  void putEntry(WeakInterningHashSet.Entry<E> entry)
          Puts an new entry into the table.
protected  void rehash(WeakInterningHashSet.Entry<E>[] newEntries)
          Rehashes the existing {#entries} into the new entries.
 boolean remove(java.lang.Object object)
          
protected  void removeEntry(WeakInterningHashSet.Entry<E> entry)
          Remove an existing entry from the set.
 int size()
           
 
Methods inherited from class java.util.AbstractSet
equals, hashCode, removeAll
 
Methods inherited from class java.util.AbstractCollection
addAll, clear, containsAll, isEmpty, retainAll, toArray, toArray, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Set
addAll, clear, containsAll, isEmpty, retainAll, toArray, toArray
 

Field Detail

serialVersionUID

protected static final long serialVersionUID
See Also:
Constant Field Values

NULL_ENTRY

protected static final WeakInterningHashSet.Entry<java.lang.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 < Integer.MAX_VALUE.


MOD_COUNT_INCREMENT

protected static final int MOD_COUNT_INCREMENT
The regular increment for the modCount representing a structural change to the set. It is designed to preserve the lower 6 bits of the field.

See Also:
Constant Field Values

NULL_BIT_INCREMENT

protected static final int NULL_BIT_INCREMENT
The increment for modCount representing the addition/removal of the null value to/from the set. It's intended to preserve the lower 5 bits of the field.

See Also:
Constant Field Values

CAPACITY_MASK

protected static final int CAPACITY_MASK
The mask for the lower five bits of the modCount representing the capacity index in PRIME_CAPACITIES.

See Also:
Constant Field Values

size

protected int size
The current size of the set.


modCount

protected transient int modCount
The mod count is used to encode several things. The lower five bits represent the capacity the entries. The sixth low bit represents whether or not the set contains the null value. The remaining bits are the actual modification count.


entries

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


queue

protected transient java.lang.ref.ReferenceQueue<E> queue
The queue used for polling garbage collected references.

Constructor Detail

WeakInterningHashSet

public WeakInterningHashSet()
Method Detail

size

public int size()
Specified by:
size in interface java.util.Collection<E>
Specified by:
size in interface java.util.Set<E>
Specified by:
size in class java.util.AbstractCollection<E>

grow

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


hashCode

protected int hashCode(java.lang.Object 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. The default implementation simply uses the object's hash code.


equals

protected boolean equals(java.lang.Object object,
                         java.lang.Object otherObject)
Returns true if the two objects are to be considered equal. The first object will always be the one passed in as an argument to add, contains, get, intern(Object), remove(Object). A derived class might specialize this to allow a different type of object to be used as a key than is actually stored in the set. The default implementation simply tests for Java Object.equals(Object) equality.


newEntries

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


ensureCapacity

protected void ensureCapacity()
Ensures that 3/4 of current capacity is larger than the current size. If not, it newEntries(int) reallocates the entries to the next prime capacity, i.e., it approximate doubles the capacity, and rehash(Entry[])s the set.


rehash

protected void rehash(WeakInterningHashSet.Entry<E>[] newEntries)
Rehashes the existing {#entries} into the new entries.


cleanup

protected void cleanup()
Polls the queue and removes any garbage collected entries.


add

public boolean add(E object)

Specified by:
add in interface java.util.Collection<E>
Specified by:
add in interface java.util.Set<E>
Overrides:
add in class java.util.AbstractCollection<E>

remove

public boolean remove(java.lang.Object object)

Specified by:
remove in interface java.util.Collection<E>
Specified by:
remove in interface java.util.Set<E>
Overrides:
remove in class java.util.AbstractCollection<E>

intern

public E intern(E object)

get

public E get(E object)

contains

public boolean contains(java.lang.Object object)

Specified by:
contains in interface java.util.Collection<E>
Specified by:
contains in interface java.util.Set<E>
Overrides:
contains in class java.util.AbstractCollection<E>

iterator

public java.util.Iterator<E> iterator()

Specified by:
iterator in interface java.lang.Iterable<E>
Specified by:
iterator in interface java.util.Collection<E>
Specified by:
iterator in interface java.util.Set<E>
Specified by:
iterator in class java.util.AbstractCollection<E>

index

protected int index(int hashCode)
Returns the index in the entries for the given hash code.


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.


createEntry

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


putEntry

protected void putEntry(WeakInterningHashSet.Entry<E> entry)
Puts an new entry into the table. It does not increase the size of the set and does not increment the modCount. It should be used only while rehashing or during deserialization. Use addEntry(Entry) to properly add new entries to the set.


addEntry

protected void addEntry(WeakInterningHashSet.Entry<E> entry)
Adds a new entry to the set. It ensures the capacity is sufficient, increases the size of the set, and increments the modCount.


removeEntry

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


dump

protected void dump()