|
||||||||||
| 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
|
+--org.apache.commons.lang.enum.ValuedEnum
Abstract superclass for type-safe enums with integer values.
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 JavaVersion extends ValuedEnum {
//standard enums for version of JVM
public static final int JAVA1_0_VALUE = 100;
public static final int JAVA1_1_VALUE = 110;
public static final int JAVA1_2_VALUE = 120;
public static final int JAVA1_3_VALUE = 130;
public static final JavaVersionEnum JAVA1_0 = new JavaVersionEnum( "Java 1.0", JAVA1_0_VALUE );
public static final JavaVersionEnum JAVA1_1 = new JavaVersionEnum( "Java 1.1", JAVA1_1_VALUE );
public static final JavaVersionEnum JAVA1_2 = new JavaVersionEnum( "Java 1.2", JAVA1_2_VALUE );
public static final JavaVersionEnum JAVA1_3 = new JavaVersionEnum( "Java 1.3", JAVA1_3_VALUE );
private JavaVersionEnum(String name, int value) {
super( name, value );
}
public static JavaVersionEnum getEnum(String javaVersion) {
return (JavaVersionEnum) getEnum(JavaVersionEnum.class, javaVersion);
}
public static JavaVersionEnum getEnum(int javaVersion) {
return (JavaVersionEnum) getEnum(JavaVersionEnum.class, javaVersion);
}
public static Map getEnumMap() {
return getEnumMap(JavaVersionEnum.class);
}
public static List getEnumList() {
return getEnumList(JavaVersionEnum.class);
}
public static Iterator iterator() {
return iterator(JavaVersionEnum.class);
}
}
The above class could then be used as follows:
public void doSomething(JavaVersion ver) {
switch (ver.getValue()) {
case JAVA1_0_VALUE:
// ...
break;
case JAVA1_1_VALUE:
// ...
break;
//...
}
}
As shown, each enum has a name and a value. These can be accessed using
getName and getValue.
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 |
| Nested classes inherited from class org.apache.commons.lang.enum.Enum |
|
| Field Summary | |
private int |
iValue
The value contained in enum. |
| Fields inherited from class org.apache.commons.lang.enum.Enum |
|
| Constructor Summary | |
protected |
ValuedEnum(String name,
int value)
Constructor for enum item. |
| Method Summary | |
int |
compareTo(Object other)
Tests for order. |
protected static Enum |
getEnum(Class enumClass,
int value)
Gets an Enum object by class and value. |
int |
getValue()
Get value of enum item. |
String |
toString()
Human readable description of this Enum item. |
| Methods inherited from class org.apache.commons.lang.enum.Enum |
equals, getEnum, getEnumList, getEnumMap, getName, hashCode, iterator, readResolve |
| Methods inherited from class java.lang.Object |
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
| Field Detail |
private final int iValue
| Constructor Detail |
protected ValuedEnum(String name,
int value)
name - the name of enum item.value - the value of enum item.| Method Detail |
protected static Enum getEnum(Class enumClass,
int value)
enumClass - the class of the Enum to getvalue - the value of the Enum to get
IllegalArgumentException - if the enum class is nullpublic final int getValue()
public int compareTo(Object other)
compareTo in interface ComparablecompareTo in class Enumother - 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 Enumtype[name=value], for example:
JavaVersion[Java 1.0=100]. 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 | |||||||||