Java枚举工具类ValuedEnum用法
2009-11-30 21:08:59 来源:WEB开发网核心提示:javaSE 5.0中新增了对枚举的支持,关于枚举的用处我就不详细说明了,Java枚举工具类ValuedEnum用法,简而言之,在枚举中可以定义常量,下面是它的一个例子:import java.util.Iterator;import java.util.List;import java.util.Map;import
javaSE 5.0中新增了对枚举的支持,关于枚举的用处我就不详细说明了,简而言之,在枚举中可以定义常量,供程序其它部分使用。
Java中内置的枚举类型只提供了定义枚举名字,隐含了1,2,。。等对应的实际值。
利用 org.apache.commons.lang.enums包下ValuedEnum类型,我们可以通过扩展(extends)此类,提供更加强大的功能。故名思义,ValuedEnum----就是被命名的枚举,我们可以随意定义枚举的名字,此类提供了许多方法,如getEnumMap,getEnumList,interator等方法,对枚举类的常量进行访问。下面是它的一个例子:
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);
public static final WeekDayEnum WEEKDAY_TUESDAY_ENUM = new WeekDayEnum("星期二",TUESDAY);
public static final WeekDayEnum WEEKDAY_WEDSDAY_ENUM = new WeekDayEnum("星期三",WEDSDAY);
public static final WeekDayEnum WEEKDAY_THURSDAY_ENUM = new WeekDayEnum("星期四",THURSDAY);
public static final WeekDayEnum WEEKDAY_FRIDAY_ENUM = new WeekDayEnum("星期五",FRIDAY);
public static final WeekDayEnum WEEKDAY_SATDAY_ENUM = new WeekDayEnum("星期六",SATDAY);
public static final WeekDayEnum WEEKDAY_SUNDAY_ENUM = new WeekDayEnum("星期日",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();
}
驱动类中我们可以随意调用其中的方法,以获得我们想要的结果:
例如:WeekDayEnum.WEEKDAY_MONDAY_ENUM即访问了枚举的值。
我们还可以WeekDayEnum.getEnumList返回枚举类所定义的常量的集合
以及通过WeekDayEnum.getEnumMap返回其Map键值对集合等。
Java中内置的枚举类型只提供了定义枚举名字,隐含了1,2,。。等对应的实际值。
利用 org.apache.commons.lang.enums包下ValuedEnum类型,我们可以通过扩展(extends)此类,提供更加强大的功能。故名思义,ValuedEnum----就是被命名的枚举,我们可以随意定义枚举的名字,此类提供了许多方法,如getEnumMap,getEnumList,interator等方法,对枚举类的常量进行访问。下面是它的一个例子:
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);
public static final WeekDayEnum WEEKDAY_TUESDAY_ENUM = new WeekDayEnum("星期二",TUESDAY);
public static final WeekDayEnum WEEKDAY_WEDSDAY_ENUM = new WeekDayEnum("星期三",WEDSDAY);
public static final WeekDayEnum WEEKDAY_THURSDAY_ENUM = new WeekDayEnum("星期四",THURSDAY);
public static final WeekDayEnum WEEKDAY_FRIDAY_ENUM = new WeekDayEnum("星期五",FRIDAY);
public static final WeekDayEnum WEEKDAY_SATDAY_ENUM = new WeekDayEnum("星期六",SATDAY);
public static final WeekDayEnum WEEKDAY_SUNDAY_ENUM = new WeekDayEnum("星期日",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();
}
驱动类中我们可以随意调用其中的方法,以获得我们想要的结果:
例如:WeekDayEnum.WEEKDAY_MONDAY_ENUM即访问了枚举的值。
我们还可以WeekDayEnum.getEnumList返回枚举类所定义的常量的集合
以及通过WeekDayEnum.getEnumMap返回其Map键值对集合等。
赞助商链接