|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectnet.innig.util.EnumeratedType
public abstract class EnumeratedType
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. |
| 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 |
|---|
public static java.util.Set allTypes(java.lang.Class enumTypeClass)
java.lang.IllegalArgumentException - if the given class is not a subclass of EnumeratedType.public static java.util.Set allTypeNames(java.lang.Class enumTypeClass)
java.lang.IllegalArgumentException - if the given class is not a subclass of EnumeratedType.
public static EnumeratedType resolveFromName(java.lang.Class enumTypeClass,
java.lang.String name)
java.lang.IllegalArgumentException - if the given class is not a subclass of EnumeratedType.public final java.lang.String getName()
public java.lang.String toString()
toString in class java.lang.Objectpublic final boolean equals(java.lang.Object that)
equals in class java.lang.Objectpublic final int hashCode()
hashCode in class java.lang.Object
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||