My problem: I want to specify a 1 to 1 connection between two enums. basically:
enum MyEnum{
ENUM_VALUE_1,
ENUM_VALUE_2,
ENUM_VALUE_3,
}
enum
I put the "why" aside and try to answer your question:
Create a static block in MyOtherEnum
that performs the check:
static{
// This set stores all the used values
EnumSet usedValues = EnumSet.noneOf(MyEnum.class);
for(MyOtherEnum e : values()){
// Value already present in the set?
if(usedValues.contains(e.pair)) throw ...; // Duplicate
usedValues.add(e);
}
}
But I have to agree with the comments: Instead of writing this check, you can also simply look at your code :).