I want to enforce enum implementation in Java but I found out that I couldn't do it. So I decided to define an abstract function that returns the enums of subclasses. But don't know how to do it.
Here is the code:
EnumInterface
public interface EnumInterface
{
public String getString();
}
ParentClass
public abstract class ParentClass {
public abstract Enum<?> getEnums();
}
ChildClass
public class ChildClass extends ParentClass{
public enum EnumImplementation implements EnumInterface
{
FOO("foo"),
BAR("bar");
String string;
EnumImplementation(String field)
{
this.string = string;
}
@Override
public String getString() {
return string;
}
}
@Override
public Enum<?> getEnums() {
return ?;
}
}
The code above doesn't work, I'm just trying to describe my problem. I also want to enforce the enum return type to EnumInterface
if possible. If you know how to enforce specific enum implementation that would be better as I don't even have to define the function in ParentClass
.
So, how do I return the enum so that I can just do this instanceOfParentClass.getEnums().FOO
?
What about this:
public <T extends Enum<T> & EnumInterface> T[] getEnums() {
return (T[])EnumImplementation.values();
}
Note the cast, which might result in a ClassCastException
. To prevent this you could pass the enum class or just return an array of EnumInterface
:
public <T extends Enum<T> & EnumInterface> T[] getEnums(Class<T> enumType)
public <T extends Enum<T> & EnumInterface> EnumInterface[] getEnums()
Note that this doesn't enable you to call the method like getEnums().FOO
, but you could pass the class and the name, eg
public <T extends Enum<T> & EnumInterface> T[] getEnum(Class<T> enumType, String enumName) {
return Enum.valueOf( enumType, enumName);
}
However, just as Tim B said, there might be a better option for what you're trying to achieve.
Try this.
EnumInterface
public interface EnumInterface {
public String getString();
}
ParentClass
public abstract class ParentClass<ENUM_TYPE extends Enum<ENUM_TYPE> & EnumInterface> {
public abstract ENUM_TYPE getEnums();
}
ChildClass
public class ChildClass extends ParentClass<ChildClass.EnumImplementation> {
public static enum EnumImplementation implements EnumInterface {
FOO("foo"),
BAR("bar");
String string;
EnumImplementation(String field) {
this.string = field;
}
@Override
public String getString() {
return string;
}
}
public EnumImplementation getEnums() {
return EnumImplementation.values()[0];
}
public static void test() {
Object result = new ChildClass().getEnums().FOO;
}
}
Note that getEnums()
returns an enum value. It's not possible to return the enum container itself, but you can call other enum values from any value (fe EnumImplementation.FOO.BAR.FOO
)
In order to map property names to database field you can use the enum valueOf method.
You need to do the mapping in your class as you cannot pass a reference to the static enumeration:
http://www.tryjava8.com/app/snippets/52b86150e4b0f5090255bc45
import java.util.*;
public class Main{
static enum TestE {
FOO,
BAR
}
static class TestC {
TestE getEnum(String name) {
return TestE.valueOf(name);
}
}
public static void main(String[] args){
System.out.println(TestE.FOO);
System.out.println(new TestC().getEnum("BAR"));
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.