避免返回通配符类型 [英] Avoiding Returning Wildcard Types

查看:136
本文介绍了避免返回通配符类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通配符类集合的类,它是一个单例类,如下所示:

  public ObliviousClass {

private static final ObliviousClass INSTANCE = new ObliviousClass();

私人地图< Key,Type<>> map = new HashMap< Key,Type<>>();

public void putType(Key key,Type<> type){
map.put(type);
}

//返回单例
public static ObliviousClass getInstance(){
return INSTANCE;
}

}

我希望能够在客户端代码中为这个集合添加不同的参数化类型:

  void clientMethod(){
ObliviousClass oc = ObliviousClass。的getInstance();

键入< Integer> intType = ...
键入< String> stringType = ...

oc.putType(new Key(0),intType);
oc.putType(new Key(1),stringType);
}

据我所知,一切正常。但是,客户还需要能够通过提供 Key 来获得 Type<?> 。因此,类似以下的方法将被添加到 ObliviousClass

  public Type<?> getType(Key key){
return map.get(key);
}

但是,在我的方便副本



我了解这个问题,因为客户端必须将返回的的类型<?> 。但我真的不想让 ObliviousClass 一个泛型类型, ObliviousClass< T> ,因为那时我的客户端代码... b / b>

是否有更好的设计用于我正在尝试做的事情?
- 我目前的解决方案是为客户提供一种静态方法;以下内容:


  public static< T> void getType(ObliviousClass instance,Key key,Type< T> dest){
dest =(Type< T>)instance.getType(key);
}

我四处搜寻,但无法找到完全清除的答案我的困惑。

以下是一种类型安全的方法,可将多个给定类型的实例存储在地图中。关键是您需要在检索值时提供 Class 实例来执行运行时类型检查,因为静态类型信息已被擦除。

  class ObliviousClass {

private final Map< Key,Object> map = new HashMap< Key,Object>();
$ b $ public Object put(Key key,Object value)
{
return map.put(key,value);
}

public< T> T get(Key key,Class< ;? extends T> type)
{
return type.cast(map.get(key));
}

}

用法如下所示:

  oc.put(k1,42); 
oc.put(k2,Hello!);
...
Integer i = oc.get(k1,Integer.class);
String s = oc.get(k2,String.class);
Integer x = oc.get(k2,Integer.class); / *抛出ClassCastException * /


I have a class with a collection of Wildcard Types that is a singleton, something like:

public ObliviousClass{

    private static final ObliviousClass INSTANCE = new ObliviousClass();

    private Map<Key, Type<?>> map = new HashMap<Key, Type<?>>();

    public void putType(Key key, Type<?> type){
        map.put(type);
    }

    // returns the singleton
    public static ObliviousClass getInstance(){
        return INSTANCE;
    }

}

I'd like to be able to add different Parameterized types to this collection in client code:

void clientMethod(){
    ObliviousClass oc = ObliviousClass.getInstance();

    Type<Integer> intType = ...
    Type<String> stringType = ...

    oc.putType(new Key(0), intType);
    oc.putType(new Key(1), stringType);
}

Up to this point, as I understand it, everything is ok. But a client also needs to be able to get a Type<?> provided the Key. So a method something like the following would be added to ObliviousClass:

public Type<?> getType(Key key){
    return map.get(key);
}

But in my handy copy of Effective Java, I read:

Do not use wildcard types as return types.

I understand the issue, as the client would have to cast the returned Type<?>. But I really do not want to make ObliviousClass a generic type, ObliviousClass<T>, because then my client code above would not work...

Is there a better design for what I am trying to do? -My current solution is to provide a static method for the client; something along the lines of:

public static <T> void getType(ObliviousClass instance, Key key, Type<T> dest){
    dest = (Type<T>)instance.getType(key);
}

I searched around, but wasn't able to find an answer that totally cleared my confusion.

解决方案

Here's a type-safe way to store multiple instances of a given type in a map. The key is that you need to provide a Class instance when retrieving values in order to perform runtime type-checking, because static type information has been erased.

class ObliviousClass {

  private final Map<Key, Object> map = new HashMap<Key, Object>();

  public Object put(Key key, Object value)
  {
    return map.put(key, value);
  }

  public <T> T get(Key key, Class<? extends T> type)
  {
    return type.cast(map.get(key)); 
  }

}

Usage would look like this:

oc.put(k1, 42);
oc.put(k2, "Hello!");
...
Integer i = oc.get(k1, Integer.class);
String s = oc.get(k2, String.class);
Integer x = oc.get(k2, Integer.class); /* Throws ClassCastException */

这篇关于避免返回通配符类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆