enum使用一例

/**
 * User: renjunjie
 * Date: 13-6-25 下午4:43
 * Function:
 */
public class AppContants {
    //政府机构
    //public static final Integer ORG_GOVERN = 1;
    //节能公司
    //public static final Integer ORG_ENERGY = 2;
    //耗能单位
    //public static final Integer ORG_UNIT = 3;

    public static enum ORG_TYPE { ORG_GOVERN("1"), ORG_ENERGY("2"), ORG_UNIT("3");

        private String value;
        private ORG_TYPE(String value){
            this.value = value;
        }
        private String getValue(){
            return value;
        }
        public static ORG_TYPE getByName(String name){
            for(ORG_TYPE prop : values()){
                if(prop.getValue().equals(name)){
                    return prop;
                }
            }
            throw new IllegalArgumentException(name + " is not a valid PropName");
        }
    };

    
    public static final String ZR_NAME = "zr";

    public static final Integer NODE_TYPE_DEVICE = 1;
    public static final Integer NODE_TYPE_GOVERN = 2;
    public static final Integer NODE_TYPE_ENERGY = 3;
    public static final Integer NODE_TYPE_UNIT = 4;

    /**
     * 通过组织机构类型获取树形机构节点类型
     * @param orgType
     * @return
     * @throws com.rixing.energysaving.exception.ServiceException
     */
    public static int getNodeTypeOfOrgType(String orgType) throws ServiceException {
        int result = -1;

        ORG_TYPE t = ORG_TYPE.getByName(orgType);
        switch (t) {
            case ORG_GOVERN :
                result = AppContants.NODE_TYPE_GOVERN;
                break;
            case ORG_ENERGY:
                result = AppContants.NODE_TYPE_ENERGY;
                break;
            case ORG_UNIT :
                result = AppContants.NODE_TYPE_UNIT;
                break;
        }
        if(result == -1) {
            throw new ServiceException("组织机构类型orgtype错误");
        }


        return result;
    }


}

 java switch使用enum好像只能在一个类里面

猜你喜欢

转载自blueram.iteye.com/blog/1925123