JavaSE 5.0 has added support for enumerations. I will not elaborate on the use of enumerations. In short, constants can be defined in enumerations for use by other parts of the program.
The built-in enumeration type in Java only provides the definition of the enumeration name, implying 1, 2,. . Wait for the corresponding actual value.
Using the ValuedEnum type under the org.apache.commons.lang.enums package, we can provide more powerful functions by extending this class. As the name suggests, ValuedEnum----is a named enumeration. We can define the name of the enumeration at will. This class provides many methods, such as getEnumMap, getEnumList, interator and other methods, to access the constants of the enumeration class. . Here's an example of it:
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.enums.ValuedEnum;
public final class WeekDayEnum extends ValuedEnum{
/**
*
*/
public static final long serialVersionUID = 732377557389126868L;
private static final int MONDAY=1;
private static final int TUESDAY=2;
private static final int WEDSDAY=3;
private static final int THURSDAY = 4;
private static final int FRIDAY=5;
private static final int SATDAY=6;
private static final int SUNDAY=7;
public static final WeekDayEnum WEEKDAY_MONDAY_ENUM = new WeekDayEnum("Monday",MONDAY);
public static final WeekDayEnum WEEKDAY_TUESDAY_ENUM = new WeekDayEnum("Tuesday",TUESDAY);
public static final WeekDayEnum WEEKDAY_WEDSDAY_ENUM = new WeekDayEnum("Wednesday",WEDSDAY);
public static final WeekDayEnum WEEKDAY_THURSDAY_ENUM = new WeekDayEnum("Thursday",THURSDAY);
public static final WeekDayEnum WEEKDAY_FRIDAY_ENUM = new WeekDayEnum("Friday",FRIDAY);
public static final WeekDayEnum WEEKDAY_SATDAY_ENUM = new WeekDayEnum("Saturday",SATDAY);
public static final WeekDayEnum WEEKDAY_SUNDAY_ENUM = new WeekDayEnum("Sunday",SUNDAY);
protected WeekDayEnum(String name, int value) {
super(name, value);
}
public static WeekDayEnum getEnum(String type){
return (WeekDayEnum)getEnum(WeekDayEnum.class, type);
}
public static WeekDayEnum getEnum(int type){
return (WeekDayEnum)getEnum(WeekDayEnum.class, type);
}
public static Map getEnumMap(){
return getEnumMap(WeekDayEnum.class);
}
public static List getEnumList(){
return getEnumList(WeekDayEnum.class);
}
public static Iterator iterator(){
return iterator();
}
We can call the methods in the driver class at will to obtain the results we want:
For example: WeekDayEnum.WEEKDAY_MONDAY_ENUM accesses the value of the enumeration.
We can also WeekDayEnum.getEnumList returns a collection of constants defined by the enumeration class
And return its Map key-value pair collection through WeekDayEnum.getEnumMap, etc.