枚举定义
具体细节不做介绍,直接上代码:
public enum TypeEnum {
//Google插件
GOOGLE_PLUG("Google插件"),
// 在线工具
ONLINE_TOOLS("在线工具");
private final String operationDes;
TypeEnum(String operationDes) {
this.operationDes = operationDes;
}
// valide legal
public static boolean isLegalOperation(String operationType) {
TypeEnum typeEnum = TypeEnum.get(operationType);
return Arrays.asList(TypeEnum.values()).contains(typeEnum);
}
// get name
public static TypeEnum get(String operationName) {
return Stream.of(TypeEnum.values())
.filter(operationTypeName -> operationTypeName.name().equals(operationName))
.findAny().orElse(null);
}
// all
public static List<String> getAll() {
return Stream.of(TypeEnum.values()).map(Enum::name)
.collect(Collectors.toList());
}
}
调用方式
TypeEnum.get("GOOGLE_PLUG").getOperationDes()
TypeEnum.getAll().stream().forEach(System.out::println);
TypeEnum.isLegalOperation("aa")
运行结果
Google插件
GOOGLE_PLUG
ONLINE_TOOLS
false