如何把Enum.byCode()写的优雅

75 阅读1分钟
@Getter
@RequiredArgsConstructor
public enum ActiveEnum implements DictPairEnum {
    
    OPEN(1, "开启"),
    CLOSE(0, "关闭");
    
    private static final ImmutableMap<Integer, ActiveEnum> BY_CODE = Arrays.stream(ActiveEnum.values())
            .collect(ImmutableMap.toImmutableMap(ActiveEnum::getCode, Function.identity(), CollectMerge::byFirst));
    
    @EnumValue
    private final int code;
    
    private final String msg;
    
    public static ActiveEnum byCode(Integer code) {
        return BY_CODE.get(code);
    }
    
}