Abstract implementation of State that provides operations on futures to make concrete classes easier to create.
| 32 | * futures to make concrete classes easier to create. |
| 33 | */ |
| 34 | public abstract class AbstractState implements State { |
| 35 | static { |
| 36 | MesosNativeLibrary.load(); |
| 37 | } |
| 38 | |
| 39 | @Override |
| 40 | public Future<Variable> fetch(final String name) { |
| 41 | if (!MesosNativeLibrary.version().before(MESOS_2161_JIRA_FIX_VERSION)) { |
| 42 | return new FetchFuture(name); |
| 43 | } |
| 44 | |
| 45 | // TODO(jmlvanre): Deprecate anonymous future in 0.24 (MESOS-2161). |
| 46 | final long future = __fetch(name); // Asynchronously start the operation. |
| 47 | return new Future<Variable>() { |
| 48 | @Override |
| 49 | public boolean cancel(boolean mayInterruptIfRunning) { |
| 50 | if (mayInterruptIfRunning) { |
| 51 | return __fetch_cancel(future); |
| 52 | } |
| 53 | return false; // Should not interrupt and already running (or finished). |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public boolean isCancelled() { |
| 58 | return __fetch_is_cancelled(future); |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public boolean isDone() { |
| 63 | return __fetch_is_done(future); |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public Variable get() throws InterruptedException, ExecutionException { |
| 68 | return __fetch_get(future); |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public Variable get(long timeout, TimeUnit unit) |
| 73 | throws InterruptedException, ExecutionException, TimeoutException { |
| 74 | return __fetch_get_timeout(future, timeout, unit); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | protected void finalize() { |
| 79 | __fetch_finalize(future); |
| 80 | } |
| 81 | }; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public Future<Variable> store(Variable variable) { |
| 86 | if (!MesosNativeLibrary.version().before(MESOS_2161_JIRA_FIX_VERSION)) { |
| 87 | return new StoreFuture(variable); |
| 88 | } |
| 89 | |
| 90 | // TODO(jmlvanre): Deprecate anonymous future in 0.24 (MESOS-2161). |
| 91 | final long future = __store(variable); // Asynchronously start the operation. |