I have seen some beginners write strange things about enumerations, so I would like to share these small enumeration functions that are often coded.
Example:
LevelTypeEnum
Pay attention to the function inside, it is a commonly used way of writing.
package com.example.dotest.echart;
import lombok.Getter;
import java.util.Arrays;
import java.util.Objects;
import java.util.stream.Stream;
@Getter
public enum LevelTypeEnum {
/**
* 青铜
*/
BRONZE(1,"青铜"),
/**
* 白银
*/
SILVER(2,"白银"),
/**
* 黄金
*/
GOLD(3,"黄金"),
;
private Integer type;
private String name;
LevelTypeEnum(Integer type, String name) {
this.type = type;
this.name = name;
}
/**
* 根据类型拿枚举
* @param type
* @return
*/
public static LevelTypeEnum byType(int type){
return Stream.of(values()).filter(obj-> Objects.equals(obj.getType(),type)).findFirst().orElse(null);
}
/**
* 根据类型拿名称
* @param type
* @return
*/
public static String name(int type){
LevelTypeEnum typeEnum = byType(type);
return null == typeEnum ? "": typeEnum.getName();
}
/**
* 判断是否存在
* @param type
* @return
*/
public static boolean checkIsExist(int type){
return Arrays.stream(values()).anyMatch(typeEnum -> typeEnum.getType() == type);
}
}