com.primix.tapestry.util
Class Enum

java.lang.Object
  |
  +--com.primix.tapestry.util.Enum
All Implemented Interfaces:
java.io.Serializable
Direct Known Subclasses:
AssetType, BeanLifecycle, BindingType, FormEventType, InsertTextMode, Race, ServiceLinkEventType, Sex, SortMode, TokenType, ValidationConstraint, View

public class Enum
extends java.lang.Object
implements java.io.Serializable

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.

Version:
$Id: Enum.java,v 1.7 2001/05/02 14:15:16 hship Exp $
Author:
Howard Ship
See Also:
Serialized Form

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

Enum

protected Enum(java.lang.String enumerationId)
Registers the new Enum. The serializationId must be non-null and unique among all instances of Enum for the same class.
Method Detail

getEnumerationId

public java.lang.String getEnumerationId()
Returns a String that identifies the object. The combination of class name and enumerationId should be unique. The serializationId should be a fixed, constant value set in the constructor.

toString

public java.lang.String toString()
Returns the class name (with the package stripped off), and the serializationId, seperated by a period. For example, "Suit.CLUBS" for org.example.Suit with a serializationId of "CLUBS".
Overrides:
toString in class java.lang.Object

getSingleton

protected java.lang.Object getSingleton()
Invoked from subclass' 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.