I have the following class:
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();
}
}
Question here is quite straightforward -> is there anyway I can call actuallyMakeQuery
with mQueries
? I don't think so because when I cache all the inputs, I lost T
because mQueries
is using QueryEntry<?>
. I also cannot change doStuff
signature as it is defined as an interface else where. What can I do here?
CodePudding user response:
You can capture the wildcard by introducing an additional level of indirection:
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);
}
Also, you omitted the <>
in
mQueries.add(new QueryEntry<>(input, callback));
unnecessarily turning it into rawtypes.
You can then use that new method:
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();
}