warning on unchecked cast on a return statement

Viewed 660

I have a class that holds a set of Valueloaders that handle values of type T. This is how I have created the class. However this class generates a warning on unchecked cast on line 39 return (ValueLoader<T>) loader;

I would like to know if there is way to clean this warning. Here is my code.

public enum ValueLoaderRegistry {

REGISTRY;

private transient Map<Class<?>, ValueLoader<?>> map = new HashMap<Class<?>, ValueLoader<?>>();

private ValueLoaderRegistry() {
    initialize();
}

private void initialize() {
    map.put(Integer.class, new IntegerValueLoader());
    map.put(String.class, new StringValueLoader());
    map.put(Double.class, new DoubleValueLoader());
    map.put(Boolean.class, new BooleanValueLoader());
    map.put(Regions.class, new RegoinsValueLoader());

}



@SuppressWarnings("unchecked")
public <T> ValueLoader<T> getLoader(Class<T> key){
    //Suppress unchecked cast warning, as by design we are making sure that 
    //the map contains the objects with right cast. 
    ValueLoader<?> loader = map.get(key);
    return (ValueLoader<T>) loader;
}

}

1 Answers
Related