|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | 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.
| Nested Class Summary | |
private static class |
Enum.Entry
Enable the iterator to retain the source code order |
| Field Summary | |
private static Map |
cEnumClasses
Map, key of class name, value of Entry. |
private static Map |
EMPTY_MAP
An empty map, as JDK1.2 didn't have an empty map |
private String |
iName
The string representation of the Enum. |
| Constructor Summary | |
protected |
Enum(String name)
Constructor to add a new named item to the enumeration. |
| Method Summary | |
int |
compareTo(Object other)
Tests for order. |
boolean |
equals(Object other)
Tests for equality. |
protected static Enum |
getEnum(Class enumClass,
String name)
Gets an Enum object by class and name. |
protected static List |
getEnumList(Class enumClass)
Gets the List of Enum objects using the Enum class. |
protected static Map |
getEnumMap(Class enumClass)
Gets the Map of Enum objects by name using the Enum class. |
String |
getName()
Retrieve the name of this Enum item, set in the constructor. |
int |
hashCode()
Returns a suitable hashCode for the enumeration. |
protected static Iterator |
iterator(Class enumClass)
Gets an iterator over the Enum objects in an Enum class. |
protected Object |
readResolve()
Handle the deserialization of the class to ensure that multiple copies are not wastefully created, or illegal enum types created. |
String |
toString()
Human readable description of this Enum item. |
| Methods inherited from class java.lang.Object |
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
| Field Detail |
private static final Map EMPTY_MAP
private static final Map cEnumClasses
private final String iName
| Constructor Detail |
protected Enum(String name)
name - the name of the enum object
IllegalArgumentException - if the name is null or a blank string| Method Detail |
protected Object readResolve()
protected static Enum getEnum(Class enumClass,
String name)
enumClass - the class of the Enum to getname - the name of the Enum to get, may be null
IllegalArgumentException - if the enum class is nullprotected static Map getEnumMap(Class enumClass)
enumClass - the class of the Enum to get
IllegalArgumentException - if the enum class is null
IllegalArgumentException - if the enum class is not a subclass of Enumprotected static List getEnumList(Class enumClass)
enumClass - the class of the Enum to get
IllegalArgumentException - if the enum class is null
IllegalArgumentException - if the enum class is not a subclass of Enumprotected static Iterator iterator(Class enumClass)
enumClass - the class of the Enum to get
IllegalArgumentException - if the enum class is null
IllegalArgumentException - if the enum class is not a subclass of Enumpublic final String getName()
String name of this Enum itempublic final boolean equals(Object other)
equals in class Objectother - the other object to compare for equality
public final int hashCode()
hashCode in class Objectpublic int compareTo(Object other)
compareTo in interface Comparableother - the other object to compare to
ClassCastException - if other is not an Enum
NullPointerException - if other is nullComparable.compareTo(Object)public String toString()
toString in class 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: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||