|
||||||||||
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.geotools.util.WeakHashSet<E>
org.geotools.util.CanonicalSet<E>
E
- The type of elements in the set.public class CanonicalSet<E>
A canonical set of objects, used to optimize memory use.
The operation of this set is similar in spirit to the 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.
Constructor Summary | |
---|---|
|
CanonicalSet()
Deprecated. Use newInstance(java.lang.Class instead. |
protected |
CanonicalSet(java.lang.Class<E> type)
Constructs a CanonicalSet for elements of the specified type. |
Method Summary | ||
---|---|---|
|
get(T object)
Returns an object equals to the specified object, if present. |
|
static
|
newInstance(java.lang.Class<E> type)
Constructs a CanonicalSet for elements of the specified type. |
|
|
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. |
Methods inherited from class org.geotools.util.WeakHashSet |
---|
add, clear, contains, getElementType, iterator, remove, size, toArray |
Methods inherited from class java.util.AbstractSet |
---|
equals, hashCode, removeAll |
Methods inherited from class java.util.AbstractCollection |
---|
addAll, containsAll, isEmpty, retainAll, toArray, toString |
Methods inherited from class java.lang.Object |
---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
Methods inherited from interface java.util.Collection |
---|
addAll, containsAll, equals, hashCode, isEmpty, removeAll, retainAll, toArray |
Methods inherited from interface java.util.Set |
---|
addAll, containsAll, isEmpty, retainAll, toArray |
Constructor Detail |
---|
public CanonicalSet()
newInstance(java.lang.Class)
instead.
CanonicalSet
.
protected CanonicalSet(java.lang.Class<E> type)
CanonicalSet
for elements of the specified type.
type
- The type of elements in the set.Method Detail |
---|
public static <E> CanonicalSet<E> newInstance(java.lang.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.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |