Java map with values limited by key's type parameter

Viewed 39491

Is there a way in Java to have a map where the type parameter of a value is tied to the type parameter of a key? What I want to write is something like the following:

public class Foo {
    // This declaration won't compile - what should it be?
    private static Map<Class<T>, T> defaultValues;

    // These two methods are just fine
    public static <T> void setDefaultValue(Class<T> clazz, T value) {
        defaultValues.put(clazz, value);
    }

    public static <T> T getDefaultValue(Class<T> clazz) {
        return defaultValues.get(clazz);
    }
}

That is, I can store any default value against a Class object, provided the value's type matches that of the Class object. I don't see why this shouldn't be allowed since I can ensure when setting/getting values that the types are correct.

EDIT: Thanks to cletus for his answer. I don't actually need the type parameters on the map itself since I can ensure consistency in the methods which get/set values, even if it means using some slightly ugly casts.

6 Answers

You're not trying to implement Joshua Bloch's typesafe hetereogeneous container pattern are you? Basically:

public class Favorites {
  private Map<Class<?>, Object> favorites =
    new HashMap<Class<?>, Object>();

  public <T> void setFavorite(Class<T> klass, T thing) {
    favorites.put(klass, thing);
  }

  public <T> T getFavorite(Class<T> klass) {
    return klass.cast(favorites.get(klass));
  }

  public static void main(String[] args) {
    Favorites f = new Favorites();
    f.setFavorite(String.class, "Java");
    f.setFavorite(Integer.class, 0xcafebabe);
    String s = f.getFavorite(String.class);
    int i = f.getFavorite(Integer.class);
  }
}

From Effective Java (2nd edition) and this presentation.

No, you can't do it directly. You'll need to write a wrapper class around Map<Class, Object> to enforce that Object will be instanceof Class.

It's possible to create a class which stores a map of type safe key to a value, and cast when necessary. The cast in get method is safe, as after using new Key<CharSequence>(), it's not possible to safely cast it to Key<String> or Key<Object>, so the type system enforces the correct usage of a class.

The Key class needs to be final, as otherwise an user could override equals and cause type-unsafety if two elements with different types were to be equal. Alternatively, it's possible to override equals to be final if you want to use inheritance despite the issues with it.

public final class TypeMap {
    private final Map<Key<?>, Object> m = new HashMap<>();

    public <T> T get(Key<? extends T> key) {
        // Safe, as it's not possible to safely change the Key generic type,
        // hash map cannot be accessed by an user, and this class being final
        // to prevent serialization attacks.
        @SuppressWarnings("unchecked")
        T value = (T) m.get(key);
        return value;
    }

    public <T> void put(Key<? super T> key, T value) {
        m.put(key, value);
    }

    public static final class Key<T> {
    }
}

You can use below 2 classes, Map class: GenericMap, Map-Key class: GenericKey

For example:

// Create a key includine Type definition
public static final GenericKey<HttpServletRequest> REQUEST = new GenericKey<>(HttpServletRequest.class, "HttpRequestKey");

public void example(HttpServletRequest requestToSave)
{
    GenericMap map = new GenericMap();

    // Saving value
    map.put(REQUEST, requestToSave);

    // Getting value
    HttpServletRequest request = map.get(REQUEST);
}

Advantages

  • It forces the user to put and get correct types by compilation error
  • It's doing casing for you inside
  • Generic Key helps to avoid write the class type each time you calling put(..) or get
  • No typo mistakes, like if key is 'String' type

GenericMap

public class GenericMap
{  
    private Map<String, Object> storageMap;

    protected GenericMap()
    {
        storageMap = new HashMap<String, Object>();
    }

    public <T> T get(GenericKey<T> key)
    {
        Object value = storageMap.get(key.getKey());
        if (value == null)
        {
            return null;
        }

        return key.getClassType().cast(value);
    }

    /**
     * @param key    GenericKey object with generic type - T (it can be any type)
     * @param object value to put in the map, the type of 'object' mast be - T
     */
    public <T> void put(GenericKey<T> key, T object)
    {
        T castedObject = key.getClassType().cast(object);
        storageMap.put(key.getKey(), castedObject);
    }

    @Override
    public String toString()
    {
        return storageMap.toString();
    }
}

GenericKey

public class GenericKey<T>
{
    private Class<T> classType;
    private String key;

    @SuppressWarnings("unused")
    private GenericKey()
    {
    }

    public GenericKey(Class<T> iClassType, String iKey)
    {
        this.classType = iClassType;
        this.key = iKey;
    }

    public Class<T> getClassType()
    {
        return classType;
    }

    public String getKey()
    {
        return key;
    }

    @Override
    public String toString()
    {
        return "[classType=" + classType + ", key=" + key + "]";
    }
}

T as a type must be defined generically in the class instance. The following example works:

public class Test<T> {

    private Map<Class<T>, T> defaultValues;

    public void setDefaultValue(Class<T> clazz, T value) {
        defaultValues.put(clazz, value);
    }

    public T getDefaultValue(Class<T> clazz) {
        return defaultValues.get(clazz);
    }

}

Alternatively, you can use Paul Tomblin's answer, and wrap the Map with your own object which will enforce this type of generics.

Related