org.gjt.rec.util
Class Enum

java.lang.Object
  |
  +--org.gjt.rec.util.Enum
All Implemented Interfaces:
Comparable, Serializable
Direct Known Subclasses:
Booleans.FlagType, FilePath.PathElement.Type, MapCache.MapType, SettingsEvent.Type, Workforce.State

public abstract class Enum
extends Object
implements Comparable, Serializable

Base class implementing all Enum features.

This implementation of Enum relies on the only instances of an Enum being declared in the static initializer of their class, so that all instances are instantiated by the time the class is loaded. An example of this reliance can be seen in the lack of synchronization in the readResolve() method.

NOTE: This class requires JDK 1.2 / Java 2 to work properly. It is recommended that you compile it with the JDK 1.2 option -target 1.2 to prevent its accidental use in Java 1.1 environments.

A subclass need only have a private constructor passing this class the distinct id for each declared enum instance and the public static final declarations for the enums.

This class is not implemented to allow aliasing of multiple Enum instances to the same id value.

Author:
R Evan Easton, Neil Stockbridge
See Also:
Serialized Form

Field Summary
static String REVISION
           
 
Constructor Summary
protected Enum(String uniqueId)
          Construct an instance and put it in an instance dictionary that is separate for each subclass of Enum.
 
Method Summary
 int compareTo(Object obj)
           
 boolean equals(Object obj)
          Uses reference equality to compare objects because all instances are resolved to the declared instances even after deserialization.
 String getId()
           
 int hashCode()
          This method is overridden to improve the performance of hash maps.
protected static List instanceList(Class cls)
           
protected  Object readResolve()
          Resolves this instance to one of the unique instances in the instance dictionary for this class by using the id as deserialized into this object.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

REVISION

public static final String REVISION
See Also:
Constant Field Values
Constructor Detail

Enum

protected Enum(String uniqueId)
        throws DuplicateEnumIdException
Construct an instance and put it in an instance dictionary that is separate for each subclass of Enum.

Throws:
DuplicateEnumIdException - If another Enum of the same class has already been declared with the same id.
Method Detail

instanceList

protected static List instanceList(Class cls)

getId

public String getId()

compareTo

public int compareTo(Object obj)
Specified by:
compareTo in interface Comparable

readResolve

protected final Object readResolve()
                            throws NonExistentEnumException
Resolves this instance to one of the unique instances in the instance dictionary for this class by using the id as deserialized into this object.

Throws:
NonExistentEnumException - If the instance called (id) cannot be found in the instance dictionary for this object's class.

hashCode

public final int hashCode()
This method is overridden to improve the performance of hash maps. It uses a random number, chosen when this object was constructed. This should ensure uniform distribution of different instances over hash buckets. This approach is only allowed because the equals(Object) method uses reference equality.

Overrides:
hashCode in class Object

equals

public final boolean equals(Object obj)
Uses reference equality to compare objects because all instances are resolved to the declared instances even after deserialization.

Overrides:
equals in class Object

toString

public final String toString()
Overrides:
toString in class Object
Returns:
A string of the form: ., where classname is the name of the class, less the package name for brevity.