Package org.eclipse.emf.common.util
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static classprotected static classA specialized external entry managed by theexternal queuethat callsWeakInterningHashSet.SelfCleaningEntry.clear()to remove this entry from its set. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected intThe current index withinPRIME_CAPACITIESfor the length of theentries.protected booleanWhether or not this set contains the null value.protected WeakInterningHashSet.Entry<E>[]The table of linked entries.protected final ReferenceQueue<Object>A queue used whencreatingexternal entries and subsequently for cleaning garbage collected references.protected ReferenceQueue<E>protected intThe modification count for fail fast iteration withConcurrentModificationException.protected static final WeakInterningHashSet.Entry<Object>A special entry used by the iterator to represent the need to yield the null value for the subsequent call to next().protected static final int[]The capacity used for theentriesof the set will always be a prime number to help ensure uniform distribution of the hash codes.protected static final longprotected intThe current size of the set.protected intThe next size at which anrehashshould occur. -
Constructor Summary
ConstructorsConstructorDescriptionCreates an instance with a minimum capacity.WeakInterningHashSet(int minimumCapacity) Creates an instance with at least the given capacity. -
Method Summary
Modifier and TypeMethodDescriptionbooleanprotected voidaddEntry(int index, WeakInterningHashSet.Entry<E> entry) Adds a new entry to the set at the given given index in theentries.protected EasInstance(Object object) Returns the result of casting the object toE, ornullif the object is not an instance ofE.protected voidcleanup()Polls theinternalQueueandremovesany garbage collected entries.voidclear()Specialized to be more efficient.booleanvoiddump()Dumps information about the current state of the set.protected booleanprotected booleanReturns whether the two objects are considered equal.Returns either the instance already contained in the set that's equal to the given object, ornullif the object is not in the set.protected WeakInterningHashSet.Entry<E>getEntry(int hashCode) Gets the first entry in the table with exactly the given hash code.voidgrow(int minimumCapacity) Ensures that the set has at least the specified capacity.protected intReturns thehash codeof the object.protected static intindex(int hashCode, int capacity) Returns the index in theentriesfor the given hash code and capacity.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.iterator()protected WeakInterningHashSet.Entry<E>[]newEntries(int capacity) Creates a new array ofentrieswith the specified capacity.protected final WeakInterningHashSet.Entry<E>Creates a new entry for the given referent and the given hash code.protected WeakInterningHashSet.Entry<E>newExternalEntry(E object, int hashCode) Creates a new entry for the given referent and the given hash code managed by theexternalQueue.protected WeakInterningHashSet.Entry<E>newInternalEntry(E object, int hashCode) Creates a new entry for the given referent and the given hash code and theinternalQueue.protected WeakInterningHashSet.Entry<E>Returns thesingleton entryrepresenting thenullvalue's entry during iteration.protected voidputEntry(int index, WeakInterningHashSet.Entry<E> entry) booleanprotected booleanremoveEntry(int index, WeakInterningHashSet.Entry<E> entry) Finds the entry at the given index thetableand prune if from the collision chain.protected voidremoveEntry(WeakInterningHashSet.Entry<E> entry) Removean existing entry from the set.intsize()Methods inherited from class java.util.AbstractSet
equals, hashCode, removeAllMethods inherited from class java.util.AbstractCollection
addAll, containsAll, isEmpty, retainAll, toArray, toArray, toStringMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
-
Field Details
-
serialVersionUID
protected static final long serialVersionUID- See Also:
-
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_CAPACITIESThe capacity used for theentriesof 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. -
size
protected int sizeThe current size of the set. -
capacityIndex
protected transient int capacityIndexThe current index withinPRIME_CAPACITIESfor the length of theentries. -
containsNull
protected transient boolean containsNullWhether or not this set contains the null value. -
threshold
protected transient int thresholdThe next size at which anrehashshould occur. -
modCount
protected transient int modCountThe modification count for fail fast iteration withConcurrentModificationException. -
entries
The table of linked entries. -
internalQueue
-
externalQueue
A queue used whencreatingexternal entries and subsequently for cleaning garbage collected references.Cleaninggarbage collected references is the responsibility of this external queue provider. All calls tocleanup()are guarded by whetherinternalQueuevalue isnull, so no cleanup takes plac 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:
sizein interfaceCollection<E>- Specified by:
sizein interfaceSet<E>- Specified by:
sizein classAbstractCollection<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
Returns thehash codeof the object. This will never be called withnull. A derived class may specialize this to compute an alternative hash code and should generally specializeasInstance(Object)andequals(Object, Object)The default implementation simply usesObject.hashCode(). -
equals
Returns whether the two objects are considered equal. This will never be called withnull. 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 specializeasInstance(Object)andhashCode(Object). The default implementation simply uses theObject.equals(Object). -
asInstance
Returns the result of casting the object toE, ornullif the object is not an instance ofE. This method is called byremove(Object)andcontains(Object)which use the returnednullreturnfalsebecause an object that isn't an instance ofEcan'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 ifhashCode(Object)orequals(Object, Object)are specialized. -
newEntries
Creates a new array ofentrieswith the specified capacity. -
ensureCapacity
protected boolean ensureCapacity()Ensures that 3/4 of current capacity is larger than the current size, i.e., that thesize<=threshold. If not, itreallocatesthe entries to the nextprime capacity, i.e., it approximate doubles the capacity, and thenrehashesthe set. The return value indicates whether or note the entries where rehashed. -
cleanup
protected void cleanup()Polls theinternalQueueandremovesany garbage collected entries. -
remove
- Specified by:
removein interfaceCollection<E>- Specified by:
removein interfaceSet<E>- Overrides:
removein classAbstractCollection<E>
-
clear
public void clear()Specialized to be more efficient.- Specified by:
clearin interfaceCollection<E>- Specified by:
clearin interfaceSet<E>- Overrides:
clearin classAbstractCollection<E>
-
add
- Specified by:
addin interfaceCollection<E>- Specified by:
addin interfaceSet<E>- Overrides:
addin classAbstractCollection<E>
-
intern
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:
internin interfaceInterningSet<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
Returns either the instance already contained in the set that's equal to the given object, ornullif the object is not in the set.- Specified by:
getin interfaceInterningSet<E>- Parameters:
object- the object to intern.- Returns:
- the existing instance already contained in the set equal to the given object, or
nullif the object is not in the set.
-
contains
- Specified by:
containsin interfaceCollection<E>- Specified by:
containsin interfaceSet<E>- Overrides:
containsin classAbstractCollection<E>
-
iterator
-
index
protected static int index(int hashCode, int capacity) Returns the index in theentriesfor the given hash code and capacity. -
getEntry
Gets the first entry in the table with exactly the given hash code. It's very useful to callWeakInterningHashSet.Entry.getNextEntry()to yield the next entry with exactly this same hash code. -
nullEntry
Returns thesingleton entryrepresenting thenullvalue's entry during iteration. -
newEntry
Creates a new entry for the given referent and the given hash code. Depending on whether there's aninternalQueueorexternalQueueit callsnewInternalEntry(Object, int)ornewExternalEntry(Object, int)respectively. -
newInternalEntry
Creates a new entry for the given referent and the given hash code and theinternalQueue. -
newExternalEntry
Creates a new entry for the given referent and the given hash code managed by theexternalQueue. -
putEntry
-
addEntry
-
removeEntry
-
removeEntry
Finds the entry at the given index thetableand 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.
-