策略枚举的用法六:内容归类

178 阅读3分钟

场景

在开发过程中,我们有时候会遇到一些业务性质的枚举值,这时候还需要对性质进行分类判断,传统的做法,就是写if,然后用 ||来连接判断。这时候,如果新加入一个性质的话,就会修改到比较多的地方,而且不是很直观。

1.枚举

package com.cah.project.test.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 功能描述: 对公信贷流程-业务性质枚举 <br/>
 */
@Getter
@AllArgsConstructor
public enum BizCharEnum {

    DEFAULT("0", "非业务性质"),
    S1("1", "一般业务") {
        @Override
        public boolean isNewBizChar() {
            return true;
        }
    },
    S2("2", "组合贷款"){
        @Override
        public boolean isNewBizChar() {
            return true;
        }
    },
    S3("3", "循环额度"),
    S4("4", "担保变更"){
        @Override
        public boolean isChangeBizChar() {
            return true;
        }
    },
    S5("5", "期限调整"){
        @Override
        public boolean isChangeBizChar() {
            return true;
        }
    },
    S6("6", "变更借款人"){
        @Override
        public boolean isChangeBizChar() {
            return true;
        }
    },
    S7("7", "再融资"){
        @Override
        public boolean isNewBizChar() {
            return true;
        }
    },
    S8("8", "回收再贷"),
    S9("9", "展期"){
        @Override
        public boolean isChangeBizChar() {
            return true;
        }
    },
    S10("10", "表外业务修改"){
        @Override
        public boolean isChangeBizChar() {
            return true;
        }
    },
    S11("11", "贸易融资额度合同"),
    S12("12", "框架性额度合同变更"){
        @Override
        public boolean isChangeBizChar() {
            return true;
        }
    },
    S13("13", "单位委托贷款"){
        @Override
        public boolean isChangeBizChar() {
            return true;
        }
    },

    ;

    private final String code;
    private final String msg;

    /**
     * 功能描述: 判断是否为新增类业务性质 <br/>
     *
     * @return "boolean" 是-true;否-false
     */
    public boolean isNewBizChar() {
        return false;
    }

    /**
     * 功能描述: 判断是否为变更类业务性质 <br/>
     *
     * @return "boolean" 是-true;否-false
     */
    public boolean isChangeBizChar() {
        return false;
    }

    /**
     * 功能描述: 判断是否为新增类业务性质 <br/>
     *
     * @return "boolean" 是-true;否-false
     */
    public static boolean isNewBizChar(String code) {
        return S1.code.equals(code) || S2.getCode().equals(code) || S3.getCode().equals(code);
    }

    /**
     * 功能描述: 判断是否为变更类业务性质 <br/>
     *
     * @return "boolean" 是-true;否-false
     */
    public static boolean isChangeBizChar(String code) {
        return S4.code.equals(code) || S5.getCode().equals(code) || S6.getCode().equals(code)
                || S9.getCode().equals(code) || S10.getCode().equals(code) || S12.getCode().equals(code)
                || S13.getCode().equals(code);
    }

    /**
     * 功能描述: 判断是否为新增类业务性质 <br/>
     *
     * @return "boolean" 是-true;否-false
     */
    public static boolean isNewBizChar2(String code) {
        return indexOf(code).isNewBizChar();
    }

    /**
     * 功能描述: 判断是否为变更类业务性质 <br/>
     *
     * @return "boolean" 是-true;否-false
     */
    public static boolean isChangeBizChar2(String code) {
        return indexOf(code).isChangeBizChar();
    }

    /** 如果枚举的内容有很多,可以这样缓存(注意:如果这里出现错误,启动不会报错,但是运行时会出现NoClassException。) */
    private final static Map<String, BizCharEnum> map = Stream.of(values()).collect(Collectors.toMap(BizCharEnum::getCode, e->e));

    /** 内容不是很多,直接使用for循环即可 */
    public static BizCharEnum indexOf(String code) {
        return map.getOrDefault(code, DEFAULT);
    }

    /** for循环写法 */
    public static BizCharEnum indexOf2(String code) {
        for(BizCharEnum e : values()) {
            if(e.getCode().equals(code)) {
                return e;
            }
        }
        return DEFAULT;
    }

}

这里三种写法,实际上是两种。isNewBizChar2 是对 public boolean isNewBizChar 方法的进一步包装,根据否需要再次使用这个返回的枚举做其他处理决定。 public static boolean isNewBizChar 这个方法就是传统的写法,不属于本次策略枚举考虑的范围内,只是写出来对比一下。(大部分还是会选择这种写法的)

注意闭坑: 在 静态变量map中,如果报错了,会导致整个枚举加载不了。这是spring启动过程中,不会报错!不会报错!不会报错!

2. 测试

package com.cah.project.test;

import com.cah.project.test.enums.BizCharEnum;

public class BizCharEnumTest {

    public static void main(String[] args) {
        String bizChar = "1";
        System.out.println("写法1:" + BizCharEnum.indexOf(bizChar).isNewBizChar());
        System.out.println("写法1:" + BizCharEnum.indexOf(bizChar).isChangeBizChar());
        System.out.println("---------------------------------------------------------");
        System.out.println("写法2:" + BizCharEnum.isNewBizChar(bizChar));
        System.out.println("写法2:" + BizCharEnum.isChangeBizChar(bizChar));
        System.out.println("---------------------------------------------------------");
        System.out.println("写法3:" + BizCharEnum.isNewBizChar2(bizChar));
        System.out.println("写法3:" + BizCharEnum.isChangeBizChar2(bizChar));
    }
}

3.输出

写法1:true
写法1:false
---------------------------------------------------------
写法2:true
写法2:false
---------------------------------------------------------
写法3:true
写法3:false

总结

要实现功能的方式有很多种,选择一种复合自身实际情况的即可。 强行说一下优点吧,提高代码量、水代码