public abstract class CanonicalWatched
extends java.lang.Object
CanonicalWatched, i.e., a canonical watched,
is an object that is canonical,
i.e., such that no two equal elements can be instantiated.
The canonical watched are stored in a map.
In order to know when they may be removed from the storage,
they need to be watched,
i.e., any outside reference to the object
must be indicated through methods watch()
and unwatch().
The method that decides whether two canonical watched are equal
(and, therefore, one should be removed),
is equivalent(java.lang.Object).
Implementations of CanonicalWatched should not allow
for public constructor.
Instead, it is recommended to have a method called create
and whose skeleton is the following:
public static X create(Object[] params) {
final X x = new X(params);
final X canon = x.getCanonical();
if (canon == x) {
// this is the first instantiation of the object
// finish initialising (stuff that was not required for #equivalent(Object)
}
canon.watch();
return canon;
}
| Constructor and Description |
|---|
CanonicalWatched() |
| Modifier and Type | Method and Description |
|---|---|
protected void |
destroy()
Notifies this object that it is no longer referenced.
|
boolean |
equals(java.lang.Object obj)
Indicates whether this object equals the specified object.
|
protected abstract boolean |
equivalent(java.lang.Object obj)
Indicates whether the specified object
is equivalent to this object.
|
protected abstract <K extends CanonicalWatched> |
getBuffer()
Returns the buffer that is used to decide the canonical objects.
|
protected <K extends CanonicalWatched> |
getCanonical()
Returns the canonical version of this object.
|
abstract int |
hashCode() |
void |
print(java.io.PrintStream out)
Prints out the buffer.
|
<K extends CanonicalWatched> |
unwatch()
Notifies this object that an external component no longer uses this object.
|
static void |
unwatchAll(java.util.Collection<? extends CanonicalWatched> coll)
Unwatches all the elements in the specified collection
of canonical watched objects.
|
void |
verifyValidity()
Verifies that the number of watches of this object is over 0.
|
void |
watch()
Notifies this object that it is used by an external component
of the software.
|
public final void watch()
public final <K extends CanonicalWatched> void unwatch()
public final boolean equals(java.lang.Object obj)
equivalent(Object).equals in class java.lang.Objectobj - the object to compare to this.public abstract int hashCode()
hashCode in class java.lang.Objectprotected final <K extends CanonicalWatched> K getCanonical()
equivalent(Object)),
then that object is returned.
Otherwise, this object is returned.protected void destroy()
protected abstract boolean equivalent(java.lang.Object obj)
obj - the object compared to this object.protected abstract <K extends CanonicalWatched> java.util.Map<java.lang.Object,K> getBuffer()
public void print(java.io.PrintStream out)
out - the output where the buffer is printed.public static void unwatchAll(java.util.Collection<? extends CanonicalWatched> coll)
coll - the collection of objects that must be unwatched.public void verifyValidity()
java.lang.IllegalStateException - if the number of watches is 0.