代码之家  ›  专栏  ›  技术社区  ›  utkarsh31

用通用实例变量创建一个单实例类?

  •  2
  • utkarsh31  · 技术社区  · 6 年前

    我有一个场景,其中有一个具有未来对象的bean(下面的类A)。我有另一个类(下面的类B),它是一个单例类,它将hashmap作为类型的实例变量,并实现了一个接口(下面的testinterface),它再次实现了可调用的。

    根据目前的场景,用户只能传递类型为object的future,但根据我的新需求,我希望将来的对象是generic类型。我已经修改了代码,代码似乎可以工作,但是有很多警告,我不确定我的更改是否正确。在某些情况下,我确信代码会失败。我面临的主要问题是在单例类中初始化泛型类型的哈希映射。有人能帮我吗?

    下面的代码是现有代码的示例。

    接口测试接口:

    interface TestInterface extends Callable<Void>{
        void doSomething(Future<Object> future, String id); 
    }
    

    甲类

    class A{
     private Future<Object> future;
     private CustomInteraface a;
    
     public A(Future<Object> future, CustomInteraface a){
        //do init
     }
     //getters and setters
    }
    

    乙类

    Class B implements TestInterface{
    
        private HashMap<String, A> map = new HashMap();
        private static B monitor = new B();
    
    
        public Void call(){
            HashMap.Entry<String, A> pair = (HashMap.Entry<String, A>) it.next();
            A a = (A) pair.getValue();
            Future<Object> future = a.getFuture();
            // Do something
        }
    
        public void doSomething(Future<Object> future, String id){
            if(map.contains(id)){
                //Do something
            }
            else{
                A a = new A(future, null);
                map.put();
            }
        }
    
    }
    

    我对性别的改变

    接口测试接口:

    interface TestInterface extends Callable<Void>{
        <T> void doSomething(Future<T> future, String id);  
    }
    

    甲类

    class A<T>{
     private Future<T> future;
     private CustomInteraface a;
    
     public A(Future<T> future, CustomInteraface a){
        //do init
     }
     //getters and setters
    }
    

    乙类

    Class B implements TestInterface{
    
        private HashMap<String, A> map = new HashMap();
        private static B monitor = new B();
    
    
        public Void call(){
            HashMap.Entry<String, A> pair = (HashMap.Entry<String, A>) it.next();
            A a = (A) pair.getValue();
            //Code will definitely fail here as I'm trying to cast a future object of generic type to Object class
            Future<Object> future = a.getFuture();
            // Do something
        }
    
        public void doSomething(Future<T> future, String id){
            if(map.contains(id)){
                //Do something
            }
            else{
                A<T> a = new A<T>(future, null);
                map.put();
            }
        }
    
    }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Andy Turner    6 年前

    如果你有可能 A<T> 对于映射中的异类类型,需要使用通配符将映射声明为:

    private HashMap<String, A<?>> map = new HashMap();
    

    然后,您将从地图中获得一个值,从而:

        // The cast was only necessary because A by itself is a raw type.
        HashMap.Entry<String, A<?>> pair = it.next();
        A<?> a = pair.getValue();
        Future<?> future = a.getFuture();
        // Note that future.get() yields an Object
    

    把它放在地图上,比如:

    public void doSomething(Future<?> future, String id){
        ...
            A<?> a = new A<>(future, null);
            map.put(id, future);
        ...
    }
    

    如果你需要 T 未来的返回类型 doSomething ,可以在方法上声明类型变量:

    public <T> void doSomething(Future<T> future, String id){