|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--com.primix.tapestry.util.Enum
Defines properties of enumerated types. Enumerated types are special classes that take the place of C enums. The class typically defines a number of public static constants for the elements of the type, and makes its constructors private.
The end result is that you can use simple equality checking (the == operator) for the type. Since it is still a first-class object, it may also be extended with operations.
The problem is serialization. If you serialize such an object and
then deserialize it, you get a different object. Forunately, as of
JDK 1.2, we can override this and use readResolve()
When an Enum is constructed, it is recorded into an identity table using a unique enumerationId (the enumerationId must be unique for all instances of the same class). When the Enum is de-serialized, it uses the enumerationId to locate the existing singleton instance.
It would be nice if this class was Externalizable
, not Serializable
,
but that requires public no-arguments constructors, which would spoil things.
Constructor Summary | |
protected |
Enum(java.lang.String enumerationId)
Registers the new Enum. |
Method Summary | |
java.lang.String |
getEnumerationId()
Returns a String that identifies the object. |
protected java.lang.Object |
getSingleton()
Invoked from subclass' readResolve() method. |
java.lang.String |
toString()
Returns the class name (with the package stripped off), and the serializationId, seperated by a period. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
protected Enum(java.lang.String enumerationId)
Method Detail |
public java.lang.String getEnumerationId()
public java.lang.String toString()
toString
in class java.lang.Object
protected java.lang.Object getSingleton()
readResolve()
method.
This is a bit of serialization black magic ... the method
readResolve()
MUST BE implemented by the actual class, it can't
be inherited. Subclasses should simply invoke this method:
private Object readResolve() { return getSingleton(); }
Since
readResolve()
returns a java.lang.Object
there's no reason to cast the singleton to Enum
.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |