Reputation: 306
The goal is to implement a generic Enum translator. I know Enum1 and Enum2 have the same values and in the same order.
The goal is to implement something like this:(this works)
private static Enum1 translateEnum(Enum2 originalEnum) {
return Enum1.values()[originalEnum.ordinal()];
}
But I have several enums, so I want to make a generic method: (this doesn't work)
private static < T extends Enum<T>,G extends Enum<G>> T translateEnum(G originalEnum) {
return T.values()[originalEnum.ordinal()];
}
My problem is that at T.values() the compiler tells me :
The method values() is undefined for the type T
Do you guys have an idea of how I could solve this issue or do you have any alternative idea to my problem?
Upvotes: 8
Views: 3719
Reputation: 25390
A common way to do this is to pass the target class object (or an object of the target class) as an argument to the function, then use methods from the class object to do the tricky part. For enum classes, the class object has a method which returns the equivalent of Enum.values(). You can use that to get the enum values from the target class:
public class Scratch {
enum lower { a, b, c };
enum upper { A, B, C };
static <T extends Enum<T>> T translate(Enum<?> aFrom, Class<T> aTo) {
return aTo.getEnumConstants()[aFrom.ordinal()];
}
public static void main(String[] args) {
for (lower ll : lower.values()) {
upper uu = translate(ll, upper.class);
System.out.printf("Upper of '%s' is '%s'\n", ll, uu);
}
for (upper uu : upper.values()) {
lower ll = translate(uu, lower.class);
System.out.printf("Lower of '%s' is '%s'\n", uu, ll);
}
}
}
Running this produces the following output:
Upper of 'a' is 'A'
Upper of 'b' is 'B'
Upper of 'c' is 'C'
Lower of 'A' is 'a'
Lower of 'B' is 'b'
Lower of 'C' is 'c'
Upvotes: 10