/** * Constructs a {@link ListenableFuture} that has already had the provided result given to it. * Thus the resulting future can not error, block, or be canceled. * <p> * If {@code null} is provided here the static instance of * {@link ImmediateResultListenableFuture#NULL_RESULT} will be returned to reduce GC overhead. * * @since 1.2.0 * @param <T> The result object type returned by the returned future * @param result result to be provided in .get() call * @return Already satisfied future */ @SuppressWarnings("unchecked") public static <T> ListenableFuture<T> immediateResultFuture(T result) { if (result == null) { return (ListenableFuture<T>)ImmediateResultListenableFuture.NULL_RESULT; } else { return new ImmediateResultListenableFuture<>(result); } }
/** * Constructs a {@link ListenableFuture} that has already had the provided result given to it. * Thus the resulting future can not error, block, or be canceled. * <p> * If {@code null} is provided here the static instance of * {@link ImmediateResultListenableFuture#NULL_RESULT} will be returned to reduce GC overhead. * * @since 1.2.0 * @param <T> The result object type returned by the returned future * @param result result to be provided in .get() call * @return Already satisfied future */ @SuppressWarnings("unchecked") public static <T> ListenableFuture<T> immediateResultFuture(T result) { if (result == null) { return (ListenableFuture<T>)ImmediateResultListenableFuture.NULL_RESULT; } else { return new ImmediateResultListenableFuture<>(result); } }
@Test public void nonNullResultTest() throws InterruptedException, ExecutionException, TimeoutException { Object result = new Object(); ListenableFuture<?> testFuture = new ImmediateResultListenableFuture<>(result); ImmediateListenableFutureTest.resultTest(testFuture, result); }
@Test public void addCallbackTest() { Object result = new Object(); ListenableFuture<?> testFuture = new ImmediateResultListenableFuture<>(result); ImmediateListenableFutureTest.resultAddCallbackTest(testFuture, result); }
@Test public void addListenerTest() { ListenableFuture<?> testFuture = new ImmediateResultListenableFuture<>(null); ImmediateListenableFutureTest.addListenerTest(testFuture); }
@Test public void cancelTest() { ListenableFuture<?> testFuture = new ImmediateResultListenableFuture<>(null); ImmediateListenableFutureTest.cancelTest(testFuture); }
@Test public void getRunningStackTraceTest() { ListenableFuture<?> testFuture = new ImmediateResultListenableFuture<>(null); ImmediateListenableFutureTest.getRunningStackTraceTest(testFuture); } }
@Test public void nullResultTest() throws InterruptedException, ExecutionException, TimeoutException { ImmediateListenableFutureTest.resultTest(new ImmediateResultListenableFuture<>(null), null); ImmediateListenableFutureTest.resultTest(ImmediateResultListenableFuture.NULL_RESULT, null); }
@Test public void mapFailureTest() { // should be straight through ListenableFuture<?> testFuture = new ImmediateResultListenableFuture<>(null); assertTrue(testFuture == testFuture.mapFailure(Exception.class, (t) -> { throw new RuntimeException(); })); assertTrue(testFuture == testFuture.mapFailure(Exception.class, (t) -> { throw new RuntimeException(); }, SameThreadSubmitterExecutor.instance())); assertTrue(testFuture == testFuture.mapFailure(Exception.class, (t) -> { throw new RuntimeException(); }, SameThreadSubmitterExecutor.instance(), null)); }
@Test public void flatMapFailureTest() { // should be straight through ListenableFuture<Object> testFuture = new ImmediateResultListenableFuture<>(null); assertTrue(testFuture == testFuture.flatMapFailure(Exception.class, (t) -> FutureUtils.immediateFailureFuture(new RuntimeException()))); assertTrue(testFuture == testFuture.flatMapFailure(Exception.class, (t) -> FutureUtils.immediateFailureFuture(new RuntimeException()), SameThreadSubmitterExecutor.instance())); assertTrue(testFuture == testFuture.flatMapFailure(Exception.class, (t) -> FutureUtils.immediateFailureFuture(new RuntimeException()), SameThreadSubmitterExecutor.instance(), null)); }
@Test public void addAsCallbackResultTest() throws InterruptedException, ExecutionException { String testResult = StringUtils.makeRandomString(5); ListenableFuture<String> resultFuture = new ImmediateResultListenableFuture<>(testResult); resultFuture.addCallback(slf); assertTrue(slf.isDone()); assertEquals(testResult, slf.get()); }