Implementations of Futures.immediate .
| 31 | |
| 32 | |
| 33 | @GwtCompatible(emulated = true) |
| 34 | abstract class ImmediateFuture<V> implements ListenableFuture<V> { |
| 35 | private static final Logger log = Logger.getLogger(ImmediateFuture.class.getName()); |
| 36 | |
| 37 | @Override |
| 38 | public void addListener(Runnable listener, Executor executor) { |
| 39 | checkNotNull(listener, "Runnable was null."); |
| 40 | checkNotNull(executor, "Executor was null."); |
| 41 | try { |
| 42 | executor.execute(listener); |
| 43 | } catch (RuntimeException e) { |
| 44 | // ListenableFuture's contract is that it will not throw unchecked exceptions, so log the bad |
| 45 | // runnable and/or executor and swallow it. |
| 46 | log.log( |
| 47 | Level.SEVERE, |
| 48 | "RuntimeException while executing runnable " + listener + " with executor " |
| 49 | + executor, |
| 50 | e); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public boolean cancel(boolean mayInterruptIfRunning) { |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public abstract V get() throws ExecutionException; |
| 61 | |
| 62 | @Override |
| 63 | public V get(long timeout, TimeUnit unit) throws ExecutionException { |
| 64 | checkNotNull(unit); |
| 65 | return get(); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public boolean isCancelled() { |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public boolean isDone() { |
| 75 | return true; |
| 76 | } |
| 77 | |
| 78 | |
| 79 | static class ImmediateSuccessfulFuture<V> extends ImmediateFuture<V> { |
| 80 | static final ImmediateSuccessfulFuture<Object> NULL = new ImmediateSuccessfulFuture<Object>(null); |
| 81 | |
| 82 | @Nullable |
| 83 | private final V value; |
| 84 | |
| 85 | ImmediateSuccessfulFuture(@Nullable V value) { |
| 86 | this.value = value; |
| 87 | } |
| 88 | |
| 89 | // TODO(lukes): Consider throwing InterruptedException when appropriate. |
| 90 |