今天工作中遇到需要获取上一个和下一个枚举值的需求,记录一下
package com.wugongzi.day0121;
/**
* add by wugongzi 2021/1/21
*/
public enum FamilyLevel {
LEVEL_1(1, 3000L, "入门"),
LEVEL_2(2, 27000L, "入门"),
LEVEL_3(3, 108000L, "入门"),
LEVEL_4(4, 300000L, "入门"),
LEVEL_5(5, 675000L, "了解"),
LEVEL_6(6, 1323000L, "了解"),
LEVEL_7(7, 2352000L, "了解"),
LEVEL_8(8, 3888000L, "了解"),
LEVEL_9(9, 6075000L, "掌握"),
LEVEL_10(10, 9075000L, "掌握"),
LEVEL_11(11, 13068000L, "掌握"),
LEVEL_12(12, 18252000L, "掌握"),
LEVEL_13(13, 24843000L, "精通"),
LEVEL_14(14, 33075000L, "精通"),
LEVEL_15(15, 43200000L, "精通"),
LEVEL_16(16, 131340000L, "精通"),
;
private Integer code;
private Long count;
private String description;
private static FamilyLevel[] vals = values();
FamilyLevel(Integer code, Long count, String description) {
this.code = code;
this.count = count;
this.description = description;
}
public Integer getCode() {
return code;
}
public Long getCount() {
return count;
}
public String getDescription() {
return description;
}
// 是否有上一个
public boolean hasPrevious() {
if (this.ordinal() <= 0) {
return false;
}
return true;
}
// 是否有下一个
public boolean hasNext() {
if(this.ordinal() >= vals.length - 1) {
return false;
}
return true;
}
// 上一个
public FamilyLevel previous() {
return vals[(this.ordinal() - 1) % vals.length];
}
//下一个
public FamilyLevel next() {
return vals[(this.ordinal() + 1) % vals.length];
}
}