I have an enum which is implements an interface. Enum:
enum MyEnum implements MyInterface {
ONE, TWO, THREE;
@Override
public MyEnum getFirst() {
return ONE;
}
}
Interface:
interface MyInterface<T extends Enum<T>> {
T getFirst();
}
Also I have a generic class with bounds:
class MyClass <T extends Enum<T> & MyInterface> {
private T firstElement;
private Class<T> enumType;
MyClass (Class<T> enumType) {
this.enumType = enumType;
}
}
So the main idea is to pass any enum
(which is implements MyIterface
) into constructor and then work with its constants. And this works for me. But also I want to store this first element into firstElement
private field. I tried something like this:
firstElement = ((MyInterface)enumType).getFirst();
But still no success. I can't cast java.lang.Class<T>
to MyInterface
. Any ideas how to achieve this? Thanks in advance!
UPDATE: My problem is not about how to take the first enum constant. I know about .ordinal()
and .values()[0];
. I want to create reusable generic class and use it with any enums, marked by some interface.
Ok, let it not be getFirst()
method. Let it be getDefault()