Class ValuedEnum
- java.lang.Object
-
- org.apache.commons.lang.enums.Enum
-
- org.apache.commons.lang.enums.ValuedEnum
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Comparable
@Deprecated(since="2021-04-30") public abstract class ValuedEnum extends Enum
Deprecated.Commons Lang 2 is in maintenance mode. Commons Lang 3 should be used instead.Abstract superclass for type-safe enums with integer values suitable for use in
switch
statements.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 JavaVersionEnum 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); } }
NOTE:These are declared
final
, so compilers may inline the code. Ensure you recompile everything when using final.The above class could then be used as follows:
public void doSomething(JavaVersionEnum 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
andgetValue
.NOTE: Because the switch is ultimately sitting on top of an int, the example above is not type-safe. That is, there is nothing that checks that JAVA1_0_VALUE is a legal constant for JavaVersionEnum.
The
getEnum
anditerator
methods are recommended. Unfortunately, Java restrictions require these to be coded as shown in each subclass. An alternative choice is to use theEnumUtils
class.- Since:
- 2.1 (class existed in enum package from v1.0)
- See Also:
- Serialized Form
-
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description int
compareTo(java.lang.Object other)
Deprecated.Tests for order.int
getValue()
Deprecated.Get value of enum item.java.lang.String
toString()
Deprecated.Human readable description of thisEnum
item.-
Methods inherited from class org.apache.commons.lang.enums.Enum
equals, getEnumClass, getName, hashCode
-
-
-
-
Method Detail
-
getValue
public final int getValue()
Deprecated.Get value of enum item.
- Returns:
- the enum item's value.
-
compareTo
public int compareTo(java.lang.Object other)
Deprecated.Tests for order.
The default ordering is numeric by value, but this can be overridden by subclasses.
NOTE: From v2.2 the enums must be of the same type. If the parameter is in a different class loader than this instance, reflection is used to compare the values.
- Specified by:
compareTo
in interfacejava.lang.Comparable
- Overrides:
compareTo
in classEnum
- Parameters:
other
- the other object to compare to- Returns:
- -ve if this is less than the other object, +ve if greater than,
0
of equal - Throws:
java.lang.ClassCastException
- if other is not anEnum
java.lang.NullPointerException
- if other isnull
- See Also:
Comparable.compareTo(Object)
-
-