刘光辉
7 小时以前 06f314545ac3514d7ce28939920866ae8188d31c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package jnpf.tmsEntity.enums;
 
/**
 * TMS 业务字典枚举约定。
 * <p>
 * 库字段与前端字典 {@code enCode} 统一存 {@link #getCode()};
 * 业务比较请用 {@link #equalsCode(String)} / {@link #of(Class, String)},避免散落魔法字符串。
 * </p>
 */
public interface TmsDictEnum {
 
    /** 字典/库字段编码(与 base_dictionary_data.f_en_code 一致) */
    String getCode();
 
    /** 中文说明(便于日志与注释,非强制对外展示) */
    String getLabel();
 
    /** 是否与给定编码相等 */
    default boolean equalsCode(String code) {
        return getCode() != null && getCode().equals(code);
    }
 
    /** 是否与给定编码相等(忽略大小写,兼容历史脏数据) */
    default boolean equalsCodeIgnoreCase(String code) {
        return getCode() != null && code != null && getCode().equalsIgnoreCase(code.trim());
    }
 
    /** 按编码解析;空或不识别返回 null */
    static <E extends Enum<E> & TmsDictEnum> E of(Class<E> type, String code) {
        if (code == null || code.isEmpty() || type == null) {
            return null;
        }
        for (E e : type.getEnumConstants()) {
            if (e.equalsCode(code)) {
                return e;
            }
        }
        return null;
    }
 
    /** 编码是否命中任一枚举值 */
    @SafeVarargs
    static <E extends Enum<E> & TmsDictEnum> boolean equalsAny(String code, E... values) {
        if (code == null || values == null) {
            return false;
        }
        for (E e : values) {
            if (e != null && e.equalsCode(code)) {
                return true;
            }
        }
        return false;
    }
}