我有以下类:
public class RemoteService {
private static class QueryEntry<T> {
public final IQueryInput<T> input;
public final FutureCallback<T> callback;
// constructor not shown here
}
private final List<QueryEntry<?>> mQueries;
public <T> Result doStuff(IQueryInput<T> input, FutureCallback<T> callback) {
// Simplified code here
if(mIsServiceAlive) {
return actuallyMakeQuery(input, callback);
} else {
// will make it happen later
mQueries.add(new QueryEntry(input, callback));
}
}
private <T> Result actuallyMakeQuery(IQueryInput<T> input, FutureCallback<T> callback) {
// ... magic
return result;
}
//.. other code
private void onServiceAlive() {
//This method is called when this service is connected to dependencies
//and is now live. Here we want to actually make the queries ...
for(final QueryEntry<?> entry : mQueries) {
// Compile error here because I can't reference entry<T>.input
// as T is now lost .... is there any where I can make this happen?
actuallyMakeQuery(entry.input, entry.callback);
}
mQueries.clear();
}
}
这里的问题很简单->我是否可以用mQueries
来表示actuallyMakeQuery
?我不这么认为,因为当我缓存所有的输入时,我失去了T
,因为mQueries
使用QueryEntry<?>
。我也不能改变doStuff
签名,因为它被定义为其他接口。我在这里能做什么?
您可以通过引入额外的间接级别来捕获通配符:
private <T> Result actuallyMakeQuery(IQueryInput<T> input, FutureCallback<T> callback) {
// ... magic
return new Result();
}
private <T> Result actuallyMakeQuery(QueryEntry<T> entry) {
// no wildcards anymore
return actuallyMakeQuery(entry.input, entry.callback);
}
还有,你省略了
中的<>
mQueries.add(new QueryEntry<>(input, callback));
不必要地将其转换为rawtypes。
你可以使用这个新方法:
private void onServiceAlive() {
// This method is called when this service is connected to dependencies
// and is now live. Here we want to actually make the queries ...
for (final QueryEntry<?> entry : mQueries) {
// no problem here anymore
actuallyMakeQuery(entry);
}
mQueries.clear();
}