@Override public void addListener(Runnable listener, Executor executor, ListenerOptimizationStrategy optimize) { listenerHelper.addListener(listener, executor == executingExecutor && (optimize == ListenerOptimizationStrategy.SingleThreadIfExecutorMatchOrDone | optimize == ListenerOptimizationStrategy.SingleThreadIfExecutorMatch) ? null : executor, optimize == ListenerOptimizationStrategy.SingleThreadIfExecutorMatchOrDone ? null : executor); }
private static <T> T setupHelper(ListenerHelper<T> lh, T ... instances) { for (T instance : instances) { lh.addListener(instance); } return lh.call(); } }
/** * Adds a listener to be executed on the next {@link #call()} to this instance. This is the same * as adding a listener and providing null for the {@link Executor}. * * @param listener Listener to be called when {@link #call()} is invoked */ public void addListener(T listener) { addListener(listener, null); }
private void removeListenerTest(Executor executor) { ListenerHelper<TestInterface> ch = makeListenerHelper(TestInterface.class); TestImp ti = new TestImp(); assertFalse(ch.removeListener(null)); ch.addListener(ti, executor); assertFalse(ch.removeListener(null)); assertFalse(ch.removeListener(new TestImp())); assertEquals(1, ch.registeredListenerCount()); assertTrue(ch.removeListener(ti)); assertEquals(0, ch.registeredListenerCount()); ch.call().call(10, StringUtils.makeRandomString(10)); // verify not called assertNull(ti.lastString); }
@Override public void call(int i, String s) { super.call(i, s); ch.addListener(addedListener); } };
@Test public void addNullListenerTest() { makeListenerHelper(TestInterface.class).addListener(null); // no exception thrown }
/** * Can not be overridden, please use {@link #addListener(Runnable)} as an alternative. */ @Override protected final void done() { executingExecutor = null; callable = null; listenerHelper.callListeners(); }
/** * Constructs a runnable future with a callable work unit. * * @param recurring boolean to indicate if this task can run multiple times, and thus must be reset after each run * @param task callable to be run * @param executingExecutor Executor task will be run on for possible listener optimization, or {@code null} */ public ListenableFutureTask(boolean recurring, Callable<T> task, Executor executingExecutor) { super(task); this.listenerHelper = new RunnableListenerHelper(true); this.recurring = recurring; this.callable = task; this.executingExecutor = executingExecutor; }
@Override public void call(int i, String s) { super.call(i, s); ch.removeListener(removedListener); } };
@Override public void run() { repeatedHelper.removeListener(removedRunnable); } }, executor);
private static <T> T setupHelper(ListenerHelper<T> lh, T ... instances) { for (T instance : instances) { lh.addListener(instance); } return lh.call(); } }
@Override public void addListener(Runnable listener, Executor executor, ListenerOptimizationStrategy optimize) { listenerHelper.addListener(listener, executor == executingExecutor && (optimize == ListenerOptimizationStrategy.SingleThreadIfExecutorMatchOrDone | optimize == ListenerOptimizationStrategy.SingleThreadIfExecutorMatch) ? null : executor, optimize == ListenerOptimizationStrategy.SingleThreadIfExecutorMatchOrDone ? null : executor); }
/** * Adds a listener to be executed on the next {@link #call()} to this instance. This is the same * as adding a listener and providing null for the {@link Executor}. * * @param listener Listener to be called when {@link #call()} is invoked */ public void addListener(T listener) { addListener(listener, null); }
/** * Can not be overridden, please use {@link #addListener(Runnable)} as an alternative. */ @Override protected final void done() { executingExecutor = null; callable = null; listenerHelper.callListeners(); }
/** * Constructs a runnable future with a callable work unit. * * @param recurring boolean to indicate if this task can run multiple times, and thus must be reset after each run * @param task callable to be run * @param executingExecutor Executor task will be run on for possible listener optimization, or {@code null} */ public ListenableFutureTask(boolean recurring, Callable<T> task, Executor executingExecutor) { super(task); this.listenerHelper = new RunnableListenerHelper(true); this.recurring = recurring; this.callable = task; this.executingExecutor = executingExecutor; }
@Override public void addListener(Runnable listener, Executor executor, ListenerOptimizationStrategy optimize) { listenerHelper.addListener(listener, executor == executingExecutor && (optimize == ListenerOptimizationStrategy.SingleThreadIfExecutorMatchOrDone | optimize == ListenerOptimizationStrategy.SingleThreadIfExecutorMatch) ? null : executor, optimize == ListenerOptimizationStrategy.SingleThreadIfExecutorMatchOrDone ? null : executor); }
/** * Adds a listener to be called. If the {@link RunnableListenerHelper} was constructed with * {@code true} (listeners can only be called once) then this listener will be called * immediately. This just defers to the other addListener call, providing null for the executor. * So when the listener runs, it will be on the same thread as the one invoking * {@link #callListeners()}. * * @param listener runnable to call when trigger event called */ public void addListener(Runnable listener) { addListener(listener, null, null); }
@Override public void addListener(Runnable listener, Executor executor, ListenerOptimizationStrategy optimize) { listenerHelper.addListener(listener, executor == executingExecutor && (optimize == ListenerOptimizationStrategy.SingleThreadIfExecutorMatchOrDone | optimize == ListenerOptimizationStrategy.SingleThreadIfExecutorMatch) ? null : executor, optimize == ListenerOptimizationStrategy.SingleThreadIfExecutorMatchOrDone ? null : executor); }
/** * Adds a listener to be called. If the {@link RunnableListenerHelper} was constructed with * {@code true} (listeners can only be called once) then this listener will be called * immediately. This just defers to the other addListener call, providing null for the executor. * So when the listener runs, it will be on the same thread as the one invoking * {@link #callListeners()}. * * @param listener runnable to call when trigger event called */ public void addListener(Runnable listener) { addListener(listener, null, null); }
@Override public void handleRunFinish() { repeatedHelper.addListener(addedTR, executor); } };