> START_SNIPPET
import java.util.*;
> public class GenericClass<P extends Enum> {
public class GenericClass<P extends Enum<P>> {
> private Map<P,String> properties;
>
> public GenericClass() {
> properties = new EnumMap<P,String>( new HashMap<P,String>() );
> // compile error
> }
}
> END_SNIPPET
>
> Eclipse points out the following compile error directly on the first
> 'P' in the marked line of code: "Bound mismatch: The type P is not a
> valid substitute for the bounded parameter <K extends Enum<K>> of the
> type EnumMap<K,V>"
In your first line you have declared P to be
<P extends Enum>
Hence the compiler complains. To make it happy you have to declare
<P extends Enum<P>>
> Does anyone know what I am doing wrong, ...

Signature
Thomas
Mick - 23 Jun 2006 13:15 GMT
> In your first line you have declared P to be
> <P extends Enum>
> Hence the compiler complains. To make it happy you have to declare
> <P extends Enum<P>>
That's exactly what I was looking for; thank you.
Mick - 23 Jun 2006 22:58 GMT
Apparently this solution does not work. You cannot create a new
EnumMap based off an empty HashMap regardless of the types used.
Roland de Ruiter - 24 Jun 2006 12:16 GMT
> Apparently this solution does not work. You cannot create a new
> EnumMap based off an empty HashMap regardless of the types used.
You could pass the class denoting the key type as parameter to your
constructor (like EnumMap<K extends Enum<K>, V> has a contructor with
Class<K> as parameter):
public class GenericClass<P extends Enum<P>>
{
private Map<P,String> properties;
public GenericClass(Class<P> keyType)
{
properties = new EnumMap<P,String>( keyType );
}
}
Usage example:
GenericClass<Thread.State> foo =
new GenericClass<Thread.State>(Thread.State.class);

Signature
Regards,
Roland
Mick - 25 Jun 2006 02:46 GMT
> You could pass the class denoting the key type as parameter to your
> constructor (like EnumMap<K extends Enum<K>, V> has a contructor with
> Class<K> as parameter):
> --
> Regards,
>
> Roland
That makes a lot of sense, and it made me realize why EnumMap has that
seemingly silly constructor in the first place. I had moved on happily
with a plain-old HashMap, but I'll make a note to go revisit that code.
Thank you!