I am trying to create a enum with values 1,-1,0 but while mapping the data unknown Ordinal value for -1 is coming. Below is the piece of code for the enum . How the enum can be use to retrieve the ordinal value for -1
@RequiredArgsConstructor
public enum Number {
MULTIPLE(0, -1, "MULTIPLE"),
ZERO(1, 0, "ZERO"),
ONE(2, 1, "ONE");
private static final Map<Integer, Number> DOCUMENT_CARDINALITY_ID = Arrays.stream(values())
.collect(Collectors.toMap(Number :: intValue, Function.identity()));
private static final Map<String, Number> DOCUMENT_CARDINALITY_BY_CODE = Arrays.stream(values())
.collect(Collectors.toMap(Number :: toString, Function.identity()));
private final int value;
private final int id;
private final String code;
public static Number of(int id) {
isTrue(DOCUMENT_CARDINALITY_ID.containsKey(id), "the id " + id + " is not valid!");
return DOCUMENT_CARDINALITY_ID.get(id);
}
public static Number of(String code) {
notNull(code, "the code is null!");
isTrue(DOCUMENT_CARDINALITY_BY_CODE.containsKey(code), "the code " + code + " is not valid!");
return DOCUMENT_CARDINALITY_BY_CODE.get(code);
}
public int intValue() {
if (id == -1) {
return 0;
} else if (id == 0) {
return 1;
} else {
return 2;
}
}
@Override
public String toString() {
return code;
}
}