net.innig.util
Class EnumeratedType

java.lang.Object
  extended by net.innig.util.EnumeratedType
All Implemented Interfaces:
java.io.Serializable
Direct Known Subclasses:
GraphType, OrderedType

public abstract class EnumeratedType
extends java.lang.Object
implements java.io.Serializable

An alternative to using hand-assigned integer constants for enumerated types. Subclass to create your own enumerated type (a.k.a. "typesafe enum"):

public final class ColorType extends EnumeratedType
    {
    static public RED   = new ColorType("red");
    static public GREEN = new ColorType("green");
    static public BLUE  = new ColorType("blue");
    private ColorType( String type ) { super( type ); }
    }

There are three important features of an enumerated type class:

Because they have private constructors, you can use object identity (==) to differentiate enumerated types:

void kermitCheck(ColorType color)
    {
    if(color == ColorType.GREEN)
        System.out.println("It's not easy!");
    }

Voila! Compile-time type safety! See Effective Java #21 for a superb exposition of enumerated types and their advantages, including examples with more conventional brace formatting.

This implementation of EnumeratedType keeps an internal pool of the types of a different class. For the class above, the call

EnumeratedType.allTypes(ColorType.class)
would return the set [ColorType.RED, ColorType.GREEN, ColorType.BLUE]. This internal pooling allows you to look up enumerated types by name:
EnumeratedType.resolveFromName(ColorType.class, "blue")
would return ColorType.BLUE.

EnumeratedType serializes correctly, preserving that all-important object identity on the receiving side -- you can use == on enumerated type objects sent over the wire.

Maturity: The serialization/resolve/allTypes support is relatively new. It's succeeded in unit tests, but not real-world tests. The basic enumerated type pattern is thoroughly established and well-worn.
Plans: Perhaps add JDO support.

Version:
[Development version]
Author:
Kendall Helmstetter Gelner, Paul Cantrell
See Also:
Serialized Form

Method Summary
static java.util.Set allTypeNames(java.lang.Class enumTypeClass)
          Returns the set of valid type names for the given enumerated type class.
static java.util.Set allTypes(java.lang.Class enumTypeClass)
          Returns the set of valid types for the given enumerated type class.
 boolean equals(java.lang.Object that)
          Enumerated types always use object identity.
 java.lang.String getName()
          Returns the name of this enumerated type.
 int hashCode()
           
static EnumeratedType resolveFromName(java.lang.Class enumTypeClass, java.lang.String name)
          Returns the enumerated type object of a given class with a given name, or null if there is no type object with that name.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Method Detail

allTypes

public static java.util.Set allTypes(java.lang.Class enumTypeClass)
Returns the set of valid types for the given enumerated type class. This does not include subclasses.

Throws:
java.lang.IllegalArgumentException - if the given class is not a subclass of EnumeratedType.

allTypeNames

public static java.util.Set allTypeNames(java.lang.Class enumTypeClass)
Returns the set of valid type names for the given enumerated type class. This does not include subclasses.

Throws:
java.lang.IllegalArgumentException - if the given class is not a subclass of EnumeratedType.

resolveFromName

public static EnumeratedType resolveFromName(java.lang.Class enumTypeClass,
                                             java.lang.String name)
Returns the enumerated type object of a given class with a given name, or null if there is no type object with that name.

Throws:
java.lang.IllegalArgumentException - if the given class is not a subclass of EnumeratedType.

getName

public final java.lang.String getName()
Returns the name of this enumerated type.


toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

equals

public final boolean equals(java.lang.Object that)
Enumerated types always use object identity.

Overrides:
equals in class java.lang.Object

hashCode

public final int hashCode()
Overrides:
hashCode in class java.lang.Object