|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Object | +--org.apache.commons.lang.enum.Enum
Abstract superclass for type-safe enums.
One feature of the C programming language lacking in Java is enumerations. The C implementation based on ints was poor and open to abuse. The original Java recommendation and most of the JDK also uses int constants. It has been recognised however that a more robust type-safe class-based solution can be designed. This class follows the basic Java type-safe enumeration pattern.
NOTE:Due to the way in which Java ClassLoaders work, comparing Enum objects should always be done using the equals() method, not ==. The equals() method will try == first so in most cases the effect is the same.
To use this class, it must be subclassed. For example:
public final class ColorEnum extends Enum {
public static final ColorEnum RED = new ColorEnum("Red");
public static final ColorEnum GREEN = new ColorEnum("Green");
public static final ColorEnum BLUE = new ColorEnum("Blue");
private ColorEnum(String color) {
super(color);
}
public static ColorEnum getEnum(String color) {
return (ColorEnum) getEnum(ColorEnum.class, color);
}
public static Map getEnumMap() {
return getEnumMap(ColorEnum.class);
}
public static List getEnumList() {
return getEnumList(ColorEnum.class);
}
public static Iterator iterator() {
return iterator(ColorEnum.class);
}
}
As shown, each enum has a name. This can be accessed using getName.
The getEnum and iterator methods are recommended.
Unfortunately, Java restrictions require these to be coded as shown in each subclass.
An alternative choice is to use the EnumUtils class.
NOTE: This class originated in the Jakarta Avalon project.
| Inner Class Summary | |
private static class |
Enum.Entry
Enable the iterator to retain the source code order |
| Field Summary | |
private static java.util.Map |
cEnumClasses
Map, key of class name, value of Entry. |
private static java.util.Map |
EMPTY_MAP
An empty map, as JDK1.2 didn't have an empty map |
private java.lang.String |
iName
The string representation of the Enum. |
| Constructor Summary | |
protected |
Enum(java.lang.String name)
Constructor to add a new named item to the enumeration. |
| Method Summary | |
int |
compareTo(java.lang.Object other)
Tests for order. |
boolean |
equals(java.lang.Object other)
Tests for equality. |
protected static Enum |
getEnum(java.lang.Class enumClass,
java.lang.String name)
Gets an Enum object by class and name. |
protected static java.util.List |
getEnumList(java.lang.Class enumClass)
Gets the List of Enum objects using the Enum class. |
protected static java.util.Map |
getEnumMap(java.lang.Class enumClass)
Gets the Map of Enum objects by name using the Enum class. |
java.lang.String |
getName()
Retrieve the name of this Enum item, set in the constructor. |
int |
hashCode()
Returns a suitable hashCode for the enumeration. |
protected static java.util.Iterator |
iterator(java.lang.Class enumClass)
Gets an iterator over the Enum objects in an Enum class. |
protected java.lang.Object |
readResolve()
Handle the deserialization of the class to ensure that multiple copies are not wastefully created, or illegal enum types created. |
java.lang.String |
toString()
Human readable description of this Enum item. |
| Methods inherited from class java.lang.Object |
|
| Field Detail |
private static final java.util.Map EMPTY_MAP
private static final java.util.Map cEnumClasses
private final java.lang.String iName
| Constructor Detail |
protected Enum(java.lang.String name)
name - the name of the enum objectjava.lang.IllegalArgumentException - if the name is null or a blank string| Method Detail |
protected java.lang.Object readResolve()
protected static Enum getEnum(java.lang.Class enumClass,
java.lang.String name)
enumClass - the class of the Enum to getname - the name of the Enum to get, may be nulljava.lang.IllegalArgumentException - if the enum class is nullprotected static java.util.Map getEnumMap(java.lang.Class enumClass)
enumClass - the class of the Enum to getjava.lang.IllegalArgumentException - if the enum class is nulljava.lang.IllegalArgumentException - if the enum class is not a subclass of Enumprotected static java.util.List getEnumList(java.lang.Class enumClass)
enumClass - the class of the Enum to getjava.lang.IllegalArgumentException - if the enum class is nulljava.lang.IllegalArgumentException - if the enum class is not a subclass of Enumprotected static java.util.Iterator iterator(java.lang.Class enumClass)
enumClass - the class of the Enum to getjava.lang.IllegalArgumentException - if the enum class is nulljava.lang.IllegalArgumentException - if the enum class is not a subclass of Enumpublic final java.lang.String getName()
String name of this Enum itempublic final boolean equals(java.lang.Object other)
equals in class java.lang.Objectother - the other object to compare for equalitypublic final int hashCode()
hashCode in class java.lang.Objectpublic int compareTo(java.lang.Object other)
compareTo in interface java.lang.Comparableother - the other object to compare toClassCastException - if other is not an EnumNullPointerException - if other is nullComparable.compareTo(Object)public java.lang.String toString()
toString in class java.lang.Objecttype[name], for example:
Color[Red]. Note that the package name is stripped from
the type name.
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||