How to make sure several Java Enum types implemented a same function

115 Views Asked by At

I am using these code in Mybatis

public interface MyCustomTestInterface<T> {
    <T> getValue();
    /* cannot override getInstance()*/
    /*static Object getInstance(T);*/
}

public enum MyTestInt implements MyCustomTestInterface<Integer> {
    TEST_INT(5);

    private Integer value;

    @Override
    public Integer getValue() {
        return this.val;
    }

    public static MyTestInt getInstance(Integer value) {
        // find MyTestInt using value
    }
}

public enum MyTestStr implements MyCustomTestInterface<String> {
    TEST_STRING("TEST");

    private String value;

    @Override
    public String getValue() {
        return this.val;
    }
    public static MyTestStr getInstance(String value) {
        // find MyTestStr using value
    }
}

@MappedTypes(value = {MyTestInt.class, MyTestStr.class}) 
public class MyCustomEnumTypeHandler<E extends Enum<E> & MyCustomTestInterface<T>/*syntax error*/> extends BaseTypeHandler<E> {
    private Method getInstance;

    public MyCustomEnumTypeHandler(Class<E> type) throws NoSuchMethodException {
        // do something 
        this.getInstance = type.getDeclaredMethod("getInstance", T/*syntax error*/);
    }    

    // get a Enum type using Integer OR String(from database to java Enum object)
    @Override
    public E getNullableResult(ResultSet rs, String columnName) throws SQLException {
        // do something
        Object value = rs.getObject(columnName);
        return (E)getInstance.invoke(null, (T)value/*syntax error*/);
    }
}

My questions:
1. How to let Enum types implemented MyCustomTestInterface must have a getInstance() function?
2. How to make MyCustomEnumTypeHandler declaration correct, to fix several "syntax error", I want a common enum type hander

Thanks in advance

0

There are 0 best solutions below