English 中文(简体)
专题:如何在地图中强制实行关键和价值之间的限制
原标题:Generics: how to enforce restrictions between keys and values in a Map

问题: 一、 功能目标接口,分为以下几类:

    public static interface FunctionObject<T>  {
        void process(T object);
    }

I need it generic because I d like to use T methods in the process implementations.
Then, in other generic class, I ve a Map where I have classes as keys and function objects as values:

    Map<Class<T>, FunctionObject<T>> map;

但是,我也希望该地图接受基调小组的次类类别和超型功能物体,因此我这样做:

    Map<Class<? extends T>, FunctionObject<? super T>> map; //not what I need

基本想法是能够使用以下地图:

    //if T were Number, this should be legal
    map.put(Class<Integer>, new FunctionObject<Integer>(){...});
    map.put(Class<Float>, new FunctionObject<Number>(){...});
    map.put(Class<Double>, new FunctionObject<Object>(){...});

由于我想要执行“功能目标”的类型是关键或超级类型,我真的要界定的是:

    Map<Class<E extends T>, FunctionObject<? super E>>> map;

我如何能够取得预期的效果? 唯一选择是哪一种类型的安全异质集装箱? 地图的通用型号是什么?

最佳回答

报废的集装箱似乎只做罚款:

public class MyMap<T>
{
    interface FunctionObject<X> {}

    private Map<Class<? extends T>, FunctionObject<Object>> map = new HashMap<>();

    @SuppressWarnings("unchecked")
    public <E extends T> void put(Class<E> c, FunctionObject<? super E> f)
    {
        map.put(c, (FunctionObject<Object>) f);
    }

    public <E extends T> FunctionObject<Object> get(Class<E> c)
    {
        return map.get(c);
    }

    public static void Main(String[] args)
    {
        MyMap<Number> map = new MyMap<>();

        map.put(Integer.class, new FunctionObject<Integer>() {});
        map.put(Float.class, new FunctionObject<Number>() {});
        map.put(Double.class, new FunctionObject<Object>() {});
    }
}

www.un.org/Depts/DGACM/index_french.htm 令人痛心的是,没有任何办法可以避免 down落为反对。

http://www.ohchr.org。

问题回答

您可以作总结,假设你只使用每个入境检查的地图。

The following add method avoids the need to double up on the type as well.

public class Main {
interface FunctionObject<T> { }

private final Map<Class, FunctionObject> map = new LinkedHashMap<Class, FunctionObject>();

public <T> void add(FunctionObject<T> functionObject) {
    Class<T> tClass = null;
    for (Type iType : functionObject.getClass().getGenericInterfaces()) {
        ParameterizedType pt = (ParameterizedType) iType;
        if (!pt.getRawType().equals(FunctionObject.class)) continue;
        Type t = pt.getActualTypeArguments()[0];
        tClass = (Class<T>) t;
        break;
    }
    map.put(tClass, functionObject);
}

public <T> void put(Class<T> tClass, FunctionObject<T> functionObject) {
    map.put(tClass, functionObject);
}

public <T> FunctionObject<T> get(Class<T> tClass) {
    return map.get(tClass);
}

public static void main(String... args) throws IOException {
    Main m = new Main();
    m.add(new FunctionObject<Integer>() {
    });
    FunctionObject<Integer> foi = m.get(Integer.class);
    System.out.println(foi.getClass().getGenericInterfaces()[0]);
}
}

印刷

Main.Main$FunctionObject<java.lang.Integer>

如果你想要解除警告,你可以使用<代码>@SuppressWarnings(“unchecked”)。

The point is; there is no way to describe the constraint you have in the field declaration, you can achieve the same result if you use accessor methods which do the check on a per entry basis. You can add runtime checks as well if you need to ensure raw types are correct.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签