Enum in Java

1. Enum Class

public enum ContainerPropertyConstants {
    RETAILER("retailer"),
    LINED("isLined"),
    BAGGING("bagging"),
    MISSING("missing"),
    MOVABLE("movable"),
    RESERVABLE("reservable"),
    CONTAINED_DELIVERY_TOTE("hasContainerDeliveryTote"),
    DAMAGED("isDamaged"),
    DIRTY("isDirty"),
    TAINTED("isTainted"),
    CONTAINED_STOCK("hasContainedStock"),
    UNKNOWN("unknown");
    
    private String value;
    private ContainerPropertyConstants(String value) {
        this.value = value;
    }
    
    public String getValue() {
        return value;
    }

    @Override
    public String toString() {
        return this.getValue();
    }

    public static ContainerPropertyConstants getEnum(String propertyKey) {
        for(ContainerPropertyConstants v : values()) {
            if(v.getValue().equals(propertyKey.trim())) { 
                return v;
            }
        }
        return UNKNOWN;
    }
}

2. Use Enum Class

    private BinAttributes getBinAttributes(Map<PropertyKey, PropertyValue> properties, BinAttributes.Builder builder) {
        properties.forEach((k, v) -> {
            switch (ContainerPropertyConstants.getEnum(k.toString())) {
                case LINED:
                    break;
                case MOVABLE:
                    builder.setAccessibleToPhoenix(Boolean.valueOf(v.getPropertyValue()));
                    break;
                case RESERVABLE:
                    break;
                case CONTAINED_DELIVERY_TOTE:
                    break;
                case DAMAGED:
                    builder.setDamaged(Boolean.valueOf(v.getPropertyValue()));
                    break;
                case DIRTY:
                    builder.setContaminated( Boolean.valueOf(v.getPropertyValue()));
                    break;
                case TAINTED:
                    builder.setTainted(Boolean.valueOf(v.getPropertyValue()));
                    break;
                case CONTAINED_STOCK:
                    break;
                case UNKNOWN:
                    throw new IllegalArgumentException("Phoenix container property [" + k + "] not recognised.");
                default:
                    logger.info("Currently cannot handle container property [{}]", k);
           break; } }); return builder.build(); }
原文地址:https://www.cnblogs.com/codingforum/p/5106720.html