Enum数值到枚举的转换

import java.util.HashMap;
import java.util.Map;

/**
 * User: bin.zhang
 * Date: 2010-9-8
 * Time: 18:13:00
 */
public enum ResumeStatEnum {
    CORP_APPLYVIEW_CNT(0),
    CORP_SEARCHVIEW_CNT(1),
    BUDDY_VIEW_CNT(2);

    int value;
    ResumeStatEnum(int value) {
        this.value = value;
    }
    public int value() {
        return value;
    }

    static Map<Integer, ResumeStatEnum> hash = new HashMap<Integer, ResumeStatEnum>();
    private static void initEnumHash() {
        System.out.println("init");
        for (ResumeStatEnum rse : ResumeStatEnum.values()) {
            hash.put(rse.value(), rse);    
        }
    }
    public static ResumeStatEnum getEnumItem(int value) {
        if (hash.size() == 0) initEnumHash(); 
        return hash.get(value);
    }
}
 
/**
 * User: bin.zhang
 * Date: 2010-9-28
 * Time: 11:25:10
 */
public class EnumTest {
    public static void main(String[] args) {
        ResumeStatEnum rse = ResumeStatEnum.valueOf("CORP_SEARCHVIEW_CNT");
        System.out.println(rse.value());

        System.out.println(ResumeStatEnum.getEnumItem(0));
        System.out.println(ResumeStatEnum.getEnumItem(1));
        System.out.println(ResumeStatEnum.getEnumItem(2));
    }
}
 

你可能感兴趣的:(enum)