|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjava.util.AbstractCollection<E>
java.util.AbstractSet<E>
org.eclipse.internal.xtend.util.WeakInterningHashSet<E>
public class WeakInterningHashSet<E>
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.
| 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 |
|---|
protected static final long serialVersionUID
protected static final WeakInterningHashSet.Entry<java.lang.Object> NULL_ENTRY
protected static final int[] PRIME_CAPACITIES
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.
protected static final int MOD_COUNT_INCREMENT
modCount representing a structural change to the set. It is designed to preserve the lower 6 bits of the
field.
protected static final int NULL_BIT_INCREMENT
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.
protected static final int CAPACITY_MASK
modCount representing the capacity index in PRIME_CAPACITIES.
protected int size
protected transient int modCount
entries. The sixth low bit represents
whether or not the set contains the null value. The remaining bits are the actual modification count.
protected transient WeakInterningHashSet.Entry<E>[] entries
protected transient java.lang.ref.ReferenceQueue<E> queue
| Constructor Detail |
|---|
public WeakInterningHashSet()
| Method Detail |
|---|
public int size()
size in interface java.util.Collection<E>size in interface java.util.Set<E>size in class java.util.AbstractCollection<E>public void grow(int minimumCapacity)
protected int hashCode(java.lang.Object object)
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.
protected boolean equals(java.lang.Object object,
java.lang.Object otherObject)
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.
protected WeakInterningHashSet.Entry<E>[] newEntries(int capacity)
entries with the specified capacity.
protected void ensureCapacity()
newEntries(int) reallocates the entries to the next
prime capacity, i.e., it approximate doubles the capacity, and rehash(Entry[])s the set.
protected void rehash(WeakInterningHashSet.Entry<E>[] newEntries)
protected void cleanup()
queue and removes any garbage collected entries.
public boolean add(E object)
add in interface java.util.Collection<E>add in interface java.util.Set<E>add in class java.util.AbstractCollection<E>public boolean remove(java.lang.Object object)
remove in interface java.util.Collection<E>remove in interface java.util.Set<E>remove in class java.util.AbstractCollection<E>public E intern(E object)
public E get(E object)
public boolean contains(java.lang.Object object)
contains in interface java.util.Collection<E>contains in interface java.util.Set<E>contains in class java.util.AbstractCollection<E>public java.util.Iterator<E> iterator()
iterator in interface java.lang.Iterable<E>iterator in interface java.util.Collection<E>iterator in interface java.util.Set<E>iterator in class java.util.AbstractCollection<E>protected int index(int hashCode)
entries for the given hash code.
protected WeakInterningHashSet.Entry<E> getEntry(int hashCode)
WeakInterningHashSet.Entry.getNextEntry() to yield the next
entry with exactly this same hash code.
protected WeakInterningHashSet.Entry<E> nullEntry()
singleton entry representing the null value.
protected WeakInterningHashSet.Entry<E> createEntry(E object,
int hashCode)
protected void putEntry(WeakInterningHashSet.Entry<E> entry)
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.
protected void addEntry(WeakInterningHashSet.Entry<E> entry)
ensures the capacity is sufficient, increases the size of the set, and
increments the modCount.
protected void removeEntry(WeakInterningHashSet.Entry<E> entry)
size of the set and increments the modCount.
protected void dump()
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||