Package org.geotools.util
Class CanonicalSet<E>
- Object
- 
- AbstractCollection<E>
- 
- AbstractSet<E>
- 
- WeakHashSet<E>
- 
- CanonicalSet<E>
 
 
 
 
- 
- Type Parameters:
- E- The type of elements in the set.
 - All Implemented Interfaces:
- Iterable<E>,- Collection<E>,- Set<E>,- CheckedCollection<E>
 
 public class CanonicalSet<E> extends WeakHashSet<E> A canonical set of objects, used to optimize memory use. The operation of this set is similar in spirit to theString.intern()method. The following example shows a convenient way to useCanonicalSetas an internal pool of immutable objects.
 Thepublic Foo create(String definition) { Foo created = new Foo(definition); return (Foo) canonicalSet.unique(created); }CanonicalSethas aget(T)method that is not part of theSetinterface. Thisgetmethod retrieves an entry from this set that is equals to the supplied object. Theunique(T)method combines agetfollowed by aputoperation if the specified object was not in the set.The set of objects is held by weak references as explained in WeakHashSet. TheCanonicalSetclass is thread-safe.- Since:
- 2.4
- Author:
- Martin Desruisseaux (IRD), Jody Garnett
 
- 
- 
Constructor SummaryConstructors Modifier Constructor Description protectedCanonicalSet(Class<E> type)Constructs aCanonicalSetfor elements of the specified type.
 - 
Method SummaryAll Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description <T extends E>
 Tget(T object)Returns an object equals to the specified object, if present.static <E> CanonicalSet<E>newInstance(Class<E> type)Constructs aCanonicalSetfor elements of the specified type.<T extends E>
 Tunique(T object)Returns an object equals toobjectif such an object already exist in thisCanonicalSet.voiduniques(E... objects)Iteratively callunique(Object)for an array of objects.- 
Methods inherited from class WeakHashSetadd, clear, contains, getElementType, iterator, remove, size, toArray
 - 
Methods inherited from class AbstractSetequals, hashCode, removeAll
 - 
Methods inherited from class AbstractCollectionaddAll, containsAll, isEmpty, retainAll, toArray, toString
 - 
Methods inherited from interface CollectionparallelStream, removeIf, stream, toArray
 - 
Methods inherited from interface SetaddAll, containsAll, isEmpty, retainAll, spliterator, toArray
 
- 
 
- 
- 
- 
Method Detail- 
newInstancepublic static <E> CanonicalSet<E> newInstance(Class<E> type) Constructs aCanonicalSetfor elements of the specified type.- Type Parameters:
- E- The type of elements in the set.
- Parameters:
- type- The type of elements in the set.
- Returns:
- An initially empty set for elements of the given type.
- Since:
- 2.5
 
 - 
getpublic <T extends E> T get(T object) Returns an object equals to the specified object, if present. If this set doesn't contains any object equals toobject, then this method returnsnull.- Type Parameters:
- T- The type of the element to get.
- Parameters:
- object- The element to get.
- Returns:
- An element equals to the given one if already presents in the set, or nullotherwise.
- See Also:
- unique(Object)
 
 - 
uniquepublic <T extends E> T unique(T object) Returns an object equals toobjectif such an object already exist in thisCanonicalSet. Otherwise, addsobjectto thisCanonicalSet. This method is equivalents to the following code:if (object != null) { Object current = get(object); if (current != null) { return current; } else { add(object); } } return object;- Type Parameters:
- T- The type of the element to get.
- Parameters:
- object- The element to get or to add in the set if not already presents.
- Returns:
- An element equals to the given one if already presents in the set, or the given objectotherwise.
 
 - 
uniques@SafeVarargs public final void uniques(E... objects) Iteratively callunique(Object)for an array of objects. This method is equivalents to the following code:for (int i=0; i<objects.length; i++) { objects[i] = unique(objects[i]); }- Parameters:
- objects- On input, the objects to add to this set if not already present. On output, elements that are equal, but where every reference to an instance already presents in this set has been replaced by a reference to the existing instance.
 
 
- 
 
-