枚举类:
/**
* 作业类型
*/
public enum WorkingTypeEnum {
// 作业类型(1=人工区,2=AGV区,0=不限制)
ARTIFICIAL_AREA(1, "人工区"),
AVG_AREA(2, "AGV区"),
UNLIMITED(0, "不限制"),
;
private Integer code;
private String name;
WorkingTypeEnum(Integer code, String name) {
this.code = code;
this.name = name;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static String getNameByCode(Integer code) {
return Arrays.stream(WorkingTypeEnum.values()).filter(enumObj -> NumUtils.eq(code, enumObj.getCode()))
.findFirst().map(WorkingTypeEnum::getName).orElse("");
}
public static Integer getCodeByName(String name) {
return Arrays.stream(WorkingTypeEnum.values()).filter(enumObj -> StringUtils.equals(name, enumObj.getName()))
.findFirst().map(WorkingTypeEnum::getCode).orElse(null);
}
}
工具类:
**
* 数字工具类
*/
public class NumUtils {
private static final Logger log = LoggerFactory.getLogger(NumUtils.class);
public static Boolean eq(Object i, Object j) {
if (i == null || j == null) return false;
return StringUtils.equals(i.toString(), j.toString());
}
public static Boolean anyEq(Object i, Object... j) {
return Arrays.stream(j).anyMatch(o -> eq(i, o));
}
public static Boolean anyEq(Object i, List j) {
return j.stream().anyMatch(o -> eq(i, o));
}
public static Boolean allEq(Object i, Object... j) {
return Arrays.stream(j).allMatch(o -> eq(i, o));
}
public static Boolean notEq(Object i, Object j) {
return !eq(i, j);
}
public static Integer parseInt(String uid) {
try {
return Integer.parseInt(uid);
} catch (NumberFormatException e) {
log.error(e.getMessage(), e);
}
return null;
}
}
参考2:根据code获取名称
public enum StockProportionTypeEnum {
SAFETY_STOCK(1, "实物库存"),
VIRTUAL_INVENTORY(2, "模拟库存"),
QUOTA_INVENTORY(3, "配合库存"),
;
private Integer code;
private String message;
StockProportionTypeEnum(Integer code, String message) {
this.code = code;
this.message = message;
}
public Integer getCode() {
return this.code;
}
public String getMessage() {
return this.message;
}
public static String getMessageByCode(Integer code) {
for (StockProportionTypeEnum stockProportionTypeEnum : values()) {
if (stockProportionTypeEnum.getCode().equals(code)) {
return stockProportionTypeEnum.getMessage();
}
}
return null;
}
}