E
- The type of elements in the set.public class CanonicalSet<E> extends WeakHashSet<E>
String.intern()
method. The following example shows a convenient way to use
CanonicalSet
as an internal pool of immutable objects.
Thepublic Foo create(String definition) { Foo created = new Foo(definition); return (Foo) canonicalSet.unique(created); }
CanonicalSet
has a get(T)
method that is not part of the Set
interface. This get
method retrieves an entry from this set that is equals to the
supplied object. The unique(T)
method combines a get
followed by a put
operation if the specified object was not in the set.
The set of objects is held by weak references as explained in WeakHashSet
. The CanonicalSet
class is thread-safe.
Modifier | Constructor and Description |
---|---|
|
CanonicalSet()
Deprecated.
Use
newInstance(java.lang.Class<E>) instead. |
protected |
CanonicalSet(Class<E> type)
Constructs a
CanonicalSet for elements of the specified type. |
Modifier and Type | Method and Description |
---|---|
<T extends E> |
get(T object)
Returns an object equals to the specified object, if present.
|
static <E> CanonicalSet<E> |
newInstance(Class<E> type)
Constructs a
CanonicalSet for elements of the specified type. |
<T extends E> |
unique(T object)
Returns an object equals to
object if such an object already exist in this CanonicalSet . |
void |
uniques(E[] objects)
Iteratively call
unique(Object) for an array of objects. |
add, clear, contains, getElementType, iterator, remove, size, toArray
equals, hashCode, removeAll
addAll, containsAll, isEmpty, retainAll, toArray, toString
addAll, containsAll, equals, hashCode, isEmpty, parallelStream, removeAll, removeIf, retainAll, spliterator, stream, toArray
addAll, containsAll, isEmpty, retainAll, spliterator, toArray
public CanonicalSet()
newInstance(java.lang.Class<E>)
instead.CanonicalSet
.public static <E> CanonicalSet<E> newInstance(Class<E> type)
CanonicalSet
for elements of the specified type.E
- The type of elements in the set.type
- The type of elements in the set.public <T extends E> T get(T object)
object
, then this method returns null
.T
- The type of the element to get.object
- The element to get.null
otherwise.unique(Object)
public <T extends E> T unique(T object)
object
if such an object already exist in this CanonicalSet
. Otherwise, adds object
to this CanonicalSet
. 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;
T
- The type of the element to get.object
- The element to get or to add in the set if not already presents.object
otherwise.public void uniques(E[] objects)
unique(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]); }
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.Copyright © 1996–2019 Geotools. All rights reserved.