java - Static factory method with generics -
i have class enumconverter<t extends enum<?>> converts strings correct enum constant enum t (i can't use enum.valueof). construct instances of class in factory method this:
public static <t extends enum<?>> enumconverter<t> getinstance(class<t> enumclass) { return new enumconverter<t>(enumclass.getenumconstants()); } this works, want cache enumconverter instances make sure there 1 per enum, i.e. map, , problem how declare map. closest have come this:
private static final map<class<?>, enumconverter<? extends enum<?>>> but error if try return value map factory method:
type mismatch: cannot convert enumconverter<capture#1-of ? extends enum<?>> enumconverter<t>
any ideas?
as want store different sub types in map, compiler can't know actual subtype receiving. think have add cast:
public static <t extends enum<?>> enumconverter<t> getinstance(class<t> enumclass) { return (enumconverter<t>) cache.get(enumclass); }
Comments
Post a Comment