An abstract implementation of ListenableFuture, intended for advanced users only. More common ways to create a ListenableFuture include instantiating a SettableFuture, submitting a task to a ListeningExecutorService, and deriving a Future from an existing one,
| 59 | |
| 60 | |
| 61 | @GwtCompatible(emulated = true) |
| 62 | public abstract class AbstractFuture<V> implements ListenableFuture<V> { |
| 63 | // NOTE: Whenever both tests are cheap and functional, it's faster to use &, | instead of &&, || |
| 64 | private static final boolean GENERATE_CANCELLATION_CAUSES = Boolean.parseBoolean(System.getProperty("guava.concurrent.generate_cancellation_cause", "false")); |
| 65 | |
| 66 | /** |
| 67 | * A less abstract subclass of AbstractFuture. This can be used to optimize setFuture by ensuring |
| 68 | * that {@link #get} calls exactly the implementation of {@link AbstractFuture#get}. |
| 69 | */ |
| 70 | |
| 71 | abstract static class TrustedFuture<V> extends AbstractFuture<V> { |
| 72 | // N.B. cancel is not overridden to be final, because many future utilities need to override |
| 73 | // cancel in order to propagate cancellation to other futures. |
| 74 | // TODO(lukes): with maybePropagateCancellation this is no longer really true. Track down the |
| 75 | // final few cases and eliminate their overrides of cancel() |
| 76 | @CanIgnoreReturnValue |
| 77 | @Override |
| 78 | public final V get() throws InterruptedException, ExecutionException { |
| 79 | return super.get(); |
| 80 | } |
| 81 | |
| 82 | @CanIgnoreReturnValue |
| 83 | @Override |
| 84 | public final V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { |
| 85 | return super.get(timeout, unit); |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public final boolean isDone() { |
| 90 | return super.isDone(); |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public final boolean isCancelled() { |
| 95 | return super.isCancelled(); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public final void addListener(Runnable listener, Executor executor) { |
| 100 | super.addListener(listener, executor); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | // Logger to log exceptions caught when running listeners. |
| 105 | |
| 106 | private static final Logger log = Logger.getLogger(AbstractFuture.class.getName()); |
| 107 | |
| 108 | // A heuristic for timed gets. If the remaining timeout is less than this, spin instead of |
| 109 | // blocking. This value is what AbstractQueuedSynchronizer uses. |
| 110 | private static final long SPIN_THRESHOLD_NANOS = 1000L; |
| 111 | private static final AtomicHelper ATOMIC_HELPER; |
| 112 | |
| 113 | static { |
| 114 | AtomicHelper helper; |
| 115 | try { |
| 116 | helper = new UnsafeAtomicHelper(); |
| 117 | } catch (Throwable unsafeFailure) { |
| 118 | // catch absolutely everything and fall through to our 'SafeAtomicHelper' |
nothing calls this directly
no test coverage detected