我有一个场景,其中有一个具有未来对象的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();
}
}
}