我该如何声明具有不平凡关系的多个泛型类型的成员? [英] How do I declare a member with multiple generic types that have non-trivial relationships?

查看:79
本文介绍了我该如何声明具有不平凡关系的多个泛型类型的成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我想写在我的java代码中的内容:

  private< A extends Action< R>, R扩展结果>的MyType< A,R>会员; 

然而这是无效的语法。所以我最终写道:

  private MyType <?扩展Action< ;?扩展结果> ;,?扩展结果>会员; 

但是这忽略了这两个类派生自 Result 是一样的。我的类方法都强制执行这种关系,所以我可以确定MyType强制执行它,但在某些情况下,我仍然需要不安全地类型化 member



更多详情



下面是我想要做的精确版本,虽然它更加诙谐:



我希望我能做到:

  private< A extends Action< R>,R扩展结果> 
地图< Class< A>,ActionHandler< A,R> >处理程序;

取而代之的是:

 私人地图<类< ?扩展Action< ;?扩展结果> > ;, 
ActionHandler<扩展Action< ;?扩展结果> ;,
?扩展结果> >处理程序;

我的方法强制执行所需的关系,如下所示:

  public< A extends Action< R>,R extends Result> void addHandler(
ActionHandler< A,R>处理程序){
handlers.put(handler.getActionType(),handler);
}

我想要以下方法:

  public< A extends Action< R>,R extends Result> ActionHandler< A,R> 
findHandler(A action){
return handlers.get(action.getClass());
}

但这不起作用:我必须添加一个转换和一个 @SuppressWarnings(unchecked)



我试过的东西



  public class MyMap< A extends Action< R>, R扩展结果>扩展
HashMap< Class< A>,ActionHandler< A,R> > {...}

MyMap< ?,? >处理程序;

但它不起作用,我仍然需要投射。

解决方案

有没有干净的方式来做到这一点。您可以做的最好的是您已经完成的任务 - 隐藏执行类型安全的方法后面的 Map ,并在这些方法中隐藏必要的强制转换。



有效的Java 第2版,第29项:考虑类型安全的异构容器(Josh Bloch在其中简略描述了你所做的简单版本,一个映射,其中键是类,值是键类的实例):


接下来要注意的是收藏夹 Map的值类型仅仅是 Object 。换句话说, Map 不保证键和值之间的类型关系。事实上,Java的类型系统并不足以表达这一点。但我们知道这是真的,我们利用它来获取最爱。


鉴于您可以' t无论如何都得到任何有用的值类型强制执行,最麻烦的实现可能是这样的:

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

public< R extends Result,A extends Action< R>>
void addHandler(Class< A> actionClass,ActionHandler< R,A>处理程序){
map.put(actionClass,handler);
}

@SuppressWarnings(unchecked)
public< R extends Result,A extends Action< R>> ActionHandler< R,A>
findHandler(A action){
return(ActionHandler< R,A>)map.get(action.getClass());


有一点需要注意 Map - 基于解决方案:如果 action 不是完全用作键的类, map.get() 不会起作用 - 例如,如果它是一个子类,或者如果键类是一个接口,并且 action 本身就是实现。您可能会更好:

  @SuppressWarnings(unchecked)
public< R extends Result,A扩展Action< R>> ActionHandler< R,?超级A> (Map.Entry< Class<>,Object> entry:map.entrySet())
($ action){
if(entry.getKey ().isAssignableFrom(action.getClass())){
return(ActionHandler< R,?super A>)entry.getValue();
}
}
返回null;
}

注意:原本上面第二个例子返回 ActionHandler< R,A> ,这实际上并不正确 - 它应该是?super A 。除了 A 之外不知道它可以处理什么 - 它可以是 A 的任何超类,一直向上到 Object 。)在这种特殊情况下,它可能足够安全,但如果你考虑类似于 List ,我们可以遇到很多麻烦:您可以安全地将 A 放入 List< ;? super A> 中,但如果你假设你只会得到 A s,你将得到 ClassCastExceptions 。)

Here is what I would like to write in my java code:

private <A extends Action<R>, R extends Result> MyType<A,R> member;

This is invalid syntax however. So I end up writing:

private MyType<? extends Action<? extends Result>, ? extends Result> member;

But this disregard the fact that both classes derived from Result are the same. My class methods all enforce this relationship, so I can be sure that MyType enforces it, but I still have to unsafely typecast member in some instances.

More details

Here is the precise version of what I want to do, although it is much more criptic:

I wish I could do:

private <A extends Action<R>, R extends Result> 
    Map< Class<A>, ActionHandler<A,R> > handlers;

Instead I have to do:

private Map< Class< ? extends Action<? extends Result> >, 
             ActionHandler<? extends Action<? extends Result>, 
                           ? extends Result> > handlers;

My methods enforce the desired relationship and look like that:

public <A extends Action<R>, R extends Result> void addHandler( 
    ActionHandler<A, R> handler ) {
  handlers.put( handler.getActionType(), handler );
}

I would like the following method:

public <A extends Action<R>, R extends Result> ActionHandler<A, R> 
    findHandler( A action ) {
  return handlers.get( action.getClass() );
}

But this doesn't work: I have to add a cast and a @SuppressWarnings("unchecked").

Things I tried

I tried creating a new class just for that purpose :

public class MyMap <A extends Action<R>, R extends Result> extends 
    HashMap< Class<A>, ActionHandler<A,R> > { ... }

MyMap< ?, ? > handlers;

But it didn't work, I still need a cast.

解决方案

There's no clean way to do this. The best you can do is what you've already done -- hide the Map behind methods that enforce type safety, and hide the necessary casts in those methods.

From Effective Java 2nd edition, Item 29: "Consider typesafe heterogeneous containers" (in which Josh Bloch sketches out a simpler version of what you're doing, a "map" where the keys are classes and the values are instances of the key class):

The next thing to notice is that the value type of the favorites Map is simply Object. In other words, the Map does not guarantee the type relationship between keys and values. In fact, Java's type system is not powerful enough to express this. But we know that it's true, and we take advantage of it when it comes time to retrieve a favorite.

Given that you can't get any useful type enforcement for the values anyway, the least messy implementation is probably something like this:

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

    public <R extends Result, A extends Action<R>>
        void addHandler(Class<A> actionClass, ActionHandler<R, A> handler) {
        map.put(actionClass, handler);
    }

    @SuppressWarnings("unchecked")
    public <R extends Result, A extends Action<R>> ActionHandler<R, A>
        findHandler(A action) {
            return (ActionHandler<R, A>) map.get(action.getClass());
    }
}

One thing to note about a Map-based solution: if action isn't exactly the class used as the key, map.get() isn't going to work -- for instance, if it's a subclass, or if the key class is an interface and the action itself is the implementation. You might be better off with:

    @SuppressWarnings("unchecked")
    public <R extends Result, A extends Action<R>> ActionHandler<R, ? super A>
        findHandler( A action ) {
            for ( Map.Entry<Class<?>, Object> entry : map.entrySet() )
            {
                if (entry.getKey().isAssignableFrom(action.getClass())) {
                    return (ActionHandler<R, ? super A>) entry.getValue();
                }
            }
            return null;
    }

(Note: Originally the second example above returned ActionHandler<R, A>, which isn't actually correct -- it should be ? super A. (We don't know what it can handle besides A -- it could be any superclass of A, all the way up to Object.) In this particular case it's probably safe enough, but if you consider something like a List, we could be in a lot of trouble: you can safely put an A into a List<? super A>, but if you assume you're only going to get As out of it, you're going to get ClassCastExceptions.)

这篇关于我该如何声明具有不平凡关系的多个泛型类型的成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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