Package org.apache.sis.util.collection
Class WeakHashSet<E>
Object
AbstractCollection<E>
AbstractSet<E>
WeakHashSet<E>
- Type Parameters:
- E- the type of elements in the set.
- All Implemented Interfaces:
- Iterable<E>,- Collection<E>,- Set<E>,- CheckedContainer<E>
A set of objects hold by weak references. An entry in a 
WeakHashSet will automatically
 be removed when it is no longer in ordinary use. More precisely, the presence of an entry will
 not prevent the entry from being discarded by the garbage collector, that is, made finalizable,
 finalized, and then reclaimed. When an entry has been discarded it is effectively removed from
 the set, so this class behaves somewhat differently than other Set implementations.
 If the elements stored in this set are arrays like int[], float[] or
 Object[], then the hash code computations and the comparisons are performed using
 the static hashCode(a) and equals(a1, a2) methods defined in the Arrays
 class.
Optimizing memory use in factory implementations
TheWeakHashSet class has a get(Object) method that is not part of the
 Set interface. This get method retrieves an entry from this set
 that is equal to the supplied object. The unique(Object) method combines a
 get followed by a add operation if the specified object was not in the set.
 This is similar in spirit to the String.intern() method. The following example shows
 a convenient way to use WeakHashSet as an internal pool of immutable objects:
 private final WeakHashSet<Foo> pool = new WeakHashSet<Foo>(Foo.class);
public Foo create(String definition) {
    Foo created = new Foo(definition);
    return pool.unique(created);
}WeakHashSet can be used inside a factory to prevent creating duplicate immutable objects.
 Thread safety
The sameWeakHashSet instance can be safely used by many threads without synchronization on the part of
 the caller. But if a sequence of two or more method calls need to appear atomic from other threads perspective,
 then the caller can synchronize on this.- Since:
- 0.3
- See Also:
Defined in the sis-utility module
- 
Constructor SummaryConstructorsConstructorDescriptionWeakHashSet(Class<E> type) Creates aWeakHashSetfor elements of the specified type.
- 
Method SummaryModifier and TypeMethodDescriptionbooleanAdds the specified element to this set if it is not already present.voidclear()Removes all of the elements from this set.booleanReturnstrueif this set contains the specified element.Returns an object equals to the specified object, if present.Returns the type of elements in this set.iterator()Returns an iterator over the elements contained in this collection.booleanRemoves a single instance of the specified element from this set, if it is present Null values are considered never present.intsize()Returns the count of element in this set.E[]toArray()Returns a view of this set as an array.<T extends E>
 Tunique(T element) Returns an object equals toelementif such an object already exist in thisWeakHashSet.Methods inherited from class AbstractSetequals, hashCode, removeAllMethods inherited from class AbstractCollectionaddAll, containsAll, isEmpty, retainAll, toArray, toStringMethods inherited from class Objectclone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface CollectionparallelStream, removeIf, stream, toArrayMethods inherited from interface SetaddAll, containsAll, isEmpty, retainAll, spliterator, toArray
- 
Constructor Details- 
WeakHashSetCreates aWeakHashSetfor elements of the specified type.- Parameters:
- type- the type of the element to be included in this set.
 
 
- 
- 
Method Details- 
getElementTypeReturns the type of elements in this set.- Specified by:
- getElementTypein interface- CheckedContainer<E>
- Returns:
- the element type.
 
- 
sizepublic int size()Returns the count of element in this set.- Specified by:
- sizein interface- Collection<E>
- Specified by:
- sizein interface- Set<E>
- Specified by:
- sizein class- AbstractCollection<E>
- Returns:
- number of elements in this set.
 
- 
addAdds the specified element to this set if it is not already present. If this set already contains the specified element, the call leaves this set unchanged and returnsfalse.- Specified by:
- addin interface- Collection<E>
- Specified by:
- addin interface- Set<E>
- Overrides:
- addin class- AbstractCollection<E>
- Parameters:
- element- element to be added to this set.
- Returns:
- trueif this set did not already contain the specified element.
- Throws:
- NullArgumentException- if the given object is- null.
 
- 
removeRemoves a single instance of the specified element from this set, if it is present Null values are considered never present.- Specified by:
- removein interface- Collection<E>
- Specified by:
- removein interface- Set<E>
- Overrides:
- removein class- AbstractCollection<E>
- Parameters:
- element- element to be removed from this set, if present. Can be- null.
- Returns:
- trueif the set contained the specified element.
 
- 
getReturns an object equals to the specified object, if present. If this set doesn't contain any object equals toelement, then this method returnsnull. Null values are considered never present.- Parameters:
- element- the element to get.
- Returns:
- an element equals to the given one if already presents in the set,
         or nullotherwise.
- See Also:
 
- 
containsReturnstrueif this set contains the specified element. Null values are considered never present.- Specified by:
- containsin interface- Collection<E>
- Specified by:
- containsin interface- Set<E>
- Overrides:
- containsin class- AbstractCollection<E>
- Parameters:
- element- object to be checked for containment in this set. Can be- null.
- Returns:
- trueif this set contains the specified element.
 
- 
uniqueReturns an object equals toelementif such an object already exist in thisWeakHashSet. Otherwise, addselementto thisWeakHashSet. This method is functionally equivalents to the following code:if (element != null) { T current = get(element); if (current != null) { return current; } else { add(element); } } return element;- Type Parameters:
- T- the type of the element to get. Can be- null.
- Parameters:
- element- the element to get or to add in the set if not already presents, or- nullif the given element was null.
- Returns:
- an element equals to the given one if already presents in the set,
         or the given objectotherwise.
 
- 
clearpublic void clear()Removes all of the elements from this set.- Specified by:
- clearin interface- Collection<E>
- Specified by:
- clearin interface- Set<E>
- Overrides:
- clearin class- AbstractCollection<E>
 
- 
toArrayReturns a view of this set as an array. Elements will be in an arbitrary order. Note that this array contains strong references. Consequently, no object reclamation will occur as long as a reference to this array is hold.- Specified by:
- toArrayin interface- Collection<E>
- Specified by:
- toArrayin interface- Set<E>
- Overrides:
- toArrayin class- AbstractCollection<E>
- Returns:
- all elements in this set.
 
- 
iteratorReturns an iterator over the elements contained in this collection. No element from this set will be garbage collected as long as a reference to the iterator is hold.
 
-