/** * Gets the reference or the default value. * * @param defaultValue * @return the reference or the default value if the reference is {@code null} */ default T orElse(T defaultValue) { T value = get(); return value != null ? value : defaultValue; }
/** * Gets the reference or the default value. * * @param defaultValue * @return the reference or the default value if the reference is {@code null} */ default T orElse(T defaultValue) { T value = get(); return value != null ? value : defaultValue; }
/** * If {@link #isDone()} returns <code>true</code>, invoke the specified consumer with the reference (may be {@code null}) and the exception (or {@code null} * if processed successfully). * * @param consumer */ default void ifDone(BiConsumer<? super T, ? super Throwable> consumer) { if (isDone()) { consumer.accept(get(), cause()); } }
/** * If {@link #isDone()} returns <code>true</code>, invoke the specified consumer with the reference (may be {@code null}) and the exception (or {@code null} * if processed successfully). * * @param consumer */ default void ifDone(BiConsumer<? super T, ? super Throwable> consumer) { if (isDone()) { consumer.accept(get(), cause()); } }
@SuppressWarnings("serial") @Test public void testAsyncReferenceDynamicLookup() throws InterruptedException, ExecutionException { BlockingFoo.reset(); BlockingBarProducer.reset(); Boss.DESTROYED.set(false); List<Boolean> stageResults = new CopyOnWriteArrayList<>(); AsyncReference<Boss> ref = weld.select(new TypeLiteral<AsyncReference<Boss>>() { }).get(); ref.thenAccept((boss) -> stageResults.add(true)); assertEquals(0, stageResults.size()); Awaitility.await().atMost(Timeouts.DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS).until(() -> ref.isDone()); BlockingFoo.complete("Foo"); BlockingBarProducer.complete(152); Awaitility.await().atMost(Timeouts.DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS).until(() -> ref.get().isReadyToTest()); assertEquals(1, stageResults.size()); ref.whenComplete((r, t) -> { if (r != null) stageResults.add(true); }); assertEquals(2, stageResults.size()); assertEquals(152, ref.get().juicyBar.get().code); weld.destroy(ref); assertTrue(Boss.DESTROYED.get()); }
@Test public void testAsyncReference() throws InterruptedException, ExecutionException { BlockingFoo.reset(); BlockingBarProducer.reset(); Boss boss = weld.select(Boss.class).get(); assertFalse(boss.foo.isDone()); assertEquals("", boss.foo.orElse(BlockingFoo.EMPTY).getMessage()); boss.foo.ifDone((r, t) -> fail("BlockingFoo not complete yet")); BlockingFoo.complete("Foo"); BlockingBarProducer.complete(152); Awaitility.await().atMost(Timeouts.DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS).until(() -> boss.isReadyToTest()); assertEquals("Foo", boss.foo.get().getMessage()); boss.foo.ifDone((r, t) -> assertEquals("Foo", r.getMessage())); Throwable cause = boss.unsatisfied.cause(); assertNotNull(cause); assertTrue(cause instanceof UnsatisfiedResolutionException); boss.unsatisfied.ifDone((r, t) -> { assertNotNull(t); assertTrue(t instanceof UnsatisfiedResolutionException); }); assertNull(boss.noBing.get()); assertEquals(55, boss.juicyBing.get().value); assertNull(boss.juicyBar.cause()); assertEquals(152, boss.juicyBar.get().code); assertTrue(BlockingBarProducer.PRODUCER_USED.get()); }