0

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;
    }
}
Misthi
  • 33
  • 10

1 Answers1

0

You cannot set the ordinal value of an Enum. The ordinal value is set based upon its position during Enum declaration. In this case, the ordinals might look like

0 MULTIPLE

1 ZERO

2 ONE

See this previous post for more details. Also your spring error is occurring outside of this class and so, I have no idea what would be triggering it

duppydodah
  • 165
  • 1
  • 3
  • 17