Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Couldn't you have all your methods return a <code>ListenableFuture</code> wrapper like:</p> <pre><code>import com.google.common.util.concurrent.ForwardingListenableFuture; import com.google.common.util.concurrent.ListenableFuture; import javax.annotation.Nonnull; import java.util.concurrent.Executor; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; public class ListenableFutureWithFixedExecutor&lt;V&gt; extends ForwardingListenableFuture&lt;V&gt; { @Nonnull private final ListenableFuture&lt;V&gt; delegate; @Nonnull private final Executor executor; public ListenableFutureWithFixedExecutor(@Nonnull ListenableFuture&lt;V&gt; delegate, @Nonnull Executor executor) { this.delegate = checkNotNull(delegate); this.executor = checkNotNull(executor); } @Override protected ListenableFuture&lt;V&gt; delegate() { return delegate; } @Override public void addListener(Runnable listener, Executor executor) { checkArgument(this.executor.equals(executor), "Listeners can only be executed using %s", executor); super.addListener(listener, executor); //To change body of overridden methods use File | Settings | File Templates. } } </code></pre> <p>Another possibility: instead of throwing an <code>IllegalArgumentException</code> when the client calls <code>addListener()</code> with an incorrect callback, you could simply log a warning, and call <code>super.addListener()</code> with the correct executor.</p> <p>You could even add a method <code>&lt;V&gt; void addCallback(FutureCallback&lt;? super V&gt; callback)</code> that adds the given callback to the Future, using the fixed executor. This would let you use your Future more fluently :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload