I have an interface like this:
public interface BatchSynchronisedPool<R extends Runnable> {
void execute(R runnable, Object batchIdentifier);
public <T> Future<T> submit(Callable<T> callable, Object batchIdentifier);
}
I want to infer an upper bound for Callable, but still want to be able to keep the <T> argument on the method:
public interface BatchSynchronisedPool<R extends Runnable, C extends Callable> {
void execute(R runnable, Object batchIdentifier);
public <T> Future<T> submit(C<T> callable, Object batchIdentifier);
}
Obviously that doesn’t work, because the type of C I specify may only take a specific range of T arguments. However, now you have the general just of what I am trying to do, is there a possible solution, or am I stuck always having to submit a callable? (Or remove generics altogether and perform an unsafe cast)
I’m not 100% certain, but don’t think that you can do what you’re trying to do here. Since
Cis not generic, you can’t useC<T>. Lots of code below, but tl;dr take option 3. All that really changes in the end is how manyBatchSynchronisedPoolobjects you’ll need to create, which is not really a considerable overhead…1. Keep the
<T>generic type parameter on the method, submit aCallable<T>and perform a runtime check of the types, like your original solution, in the class that implements this interface.2. Keep the
<T>generic type parameter on the method, submit aCand perform a cast, like you’ve suggested, in the class that implements this interface.3. Create a new
BatchSynchronisedPoolfor each typeTthat you’re trying to submit by specifyingTas a generic type parameter for the class. Then each time you want to callsubmiton a different type, you’d need to generate a new instance ofBatchSynchronisedPool.