lundi 23 janvier 2017

Java Enums Enforce Uniqueness For A Field

I have an enum which in a simplified form looks like this.

public enum Codes{
    Code1("someCode1", "someState"),
    Code2("someCode2", "someState"),
    ...
    ...
    private final String m_code;
    private final String m_state;       
}

My objective is to enforce that when anyone else edits this enum to add a new value, say Code100, the m_code for Code100 should not be the same as m_code for any of the previous Code1-Code99. The only way, I could think of was to write a unit test for this enum which would do that check. Is there a better solution to this problem?

Ideally, I would have liked a compile time error for this situation, but I am not sure this can be done in Java?

Aucun commentaire:

Enregistrer un commentaire