stream-enum判断

   InventoryDetailTypeEnum inventoryDetailTypeEnum = Arrays.stream(InventoryDetailTypeEnum.values())
            .filter(it -> it.getCode() == deductInventoryInfoDTO.getInventoryDetailType())
            .findFirst()
            .orElseThrow(() -> new MzException(PmsErrorEnum.INVENTORY_DETAIL_TYPE_ERROR));

比如这个enum:

/**
 * 库存操作明细类型
 * @author hejun
 */

public enum InventoryDetailTypeEnum {

    DEDUCT_INVENTORY((byte)1, "交易扣减库存"),
    RECOVERY_INVENTORY((byte)2, "交易回退库存"),
    PRE_ORDER_DEDUCT_INVENTORY((byte)3, "预订单扣减"),
    PRE_ORDER_RECOVERY_INVENTORY((byte)4, "预订单回退"),
    PRE_ORDER_TO_ORDER_DEDUCT_INVENTORY((byte)5, "预订单转正式订单"),
    ORDER_TO_PRE_ORDER_RECOVERY_INVENTORY((byte)6, "正式订单回退到预订单")
    ;

    InventoryDetailTypeEnum(byte code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    private byte code;
    private String msg;

    public byte getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }


    /**
     * 根据code获取MSG
     * @param code
     * @return
     */
    public static String getMsgByCode(byte code){

        for (InventoryDetailTypeEnum element : InventoryDetailTypeEnum.values()) {
            if(element.getCode() == code){
                return element.getMsg();
            }
        }
        return "";
    }


    public static boolean contains(Object value) {
        if(value == null){
            return false;
        }
        for (InventoryDetailTypeEnum element : InventoryDetailTypeEnum.values()) {
            if(element.getCode() == Byte.valueOf(value.toString())){
                return true;
            }
        }
        return false;
    }

    /**
     * 获取全部枚举值
     *
     * @return
     */
    public static Map<Number, String> getDataMap() {
        Map<Number, String> dataMap = new HashMap<Number, String>();
        for (InventoryDetailTypeEnum element : InventoryDetailTypeEnum.values()) {
            dataMap.put(element.getCode(), element.getMsg());
        }
        return dataMap;
    }
}

 

上一篇:浅析TypeScript中const和readonly的区别、枚举和常量枚举的区别以及关于typescript中枚举的相关知识


下一篇:枚举类enum