Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Generics Question: type parameter E is not within its bound

Tags:

java

generics

i've got a question about generics. I have this method which does not compile at all. The compiler tells me: type parameter E is not within its bound. I've usually no problem in understanding compiler errors, but this one is quite tricky. Maybe my knowledge about generics need to improve. :-) Can anyone tell me whats wrong?

public static <E extends Enum & StringConvertableEnum<E>> Map<String, E> map(Class<E> enumClass) {
    Map<String, E> mapping = new HashMap<String, E>();

    EnumSet<E> set = EnumSet.allOf(enumClass);

    for(E enumConstant : set) {
        mapping.put(enumConstant.getStringValue(), enumConstant);
    }

    return mapping;
}

This is the definition of StringConvertableEnum:

public interface StringConvertableEnum<E extends Enum> {
    public E getEnumFromStringValue(String string);
    public String getStringValue();
}
like image 665
Kraushauslaus Avatar asked Jan 23 '11 17:01

Kraushauslaus


1 Answers

You need to change your declaration to E extends Enum<E>

Edit, sorry had to step away, the full declaration I mean is:

public static <E extends Enum<E> & StringConvertableEnum<E>> Map<String, E> map(Class<E> enumClass) {
like image 197
Yishai Avatar answered Sep 21 '22 09:09

Yishai