/** * Map the failure of this async result to a specific {@code value}.<p> * * When this async result fails, this {@code value} will succeeed the async result returned by this method call.<p> * * When this async succeeds, the result will be propagated to the returned async result. * * @param value the value that eventually completes the mapped async result * @return the mapped async result */ default AsyncResult<T> otherwise(T value) { return otherwise(err -> value); }
/** * Map the failure of this async result to {@code null}.<p> * * This is a convenience for {@code asyncResult.otherwise((T) null)}.<p> * * When this async result fails, the {@code null} will succeeed the async result returned by this method call.<p> * * When this async succeeds, the result will be propagated to the returned async result. * * @return the mapped async result */ default AsyncResult<T> otherwiseEmpty() { return otherwise(err -> null); } }
private void testFailedOtherwiseApplyFunction(AsyncResult<String> res, Future<String> f) { AsyncResult<String> map1 = res.otherwise(Throwable::getMessage); Throwable cause = new Throwable("the-failure"); f.fail(cause); assertTrue(map1.succeeded()); assertFalse(map1.failed()); assertEquals("the-failure", map1.result()); assertNull(map1.cause()); }
private void testFailedOtherwise(AsyncResult<String> res, Future<String> f) { AsyncResult<String> map1 = res.otherwise("something-else"); Throwable cause = new Throwable("the-failure"); f.fail(cause); assertTrue(map1.succeeded()); assertFalse(map1.failed()); assertEquals("something-else", map1.result()); assertNull(map1.cause()); }
private void testSucceededOtherwise(AsyncResult<String> res, Future<String> f) { AsyncResult<String> ar = res.otherwise(Throwable::getMessage); f.complete("foobar"); assertTrue(ar.succeeded()); assertFalse(ar.failed()); assertEquals("foobar", ar.result()); assertNull(ar.cause()); }
private void testSucceededOtherwiseApplyFunction(AsyncResult<String> res, Future<String> f) { AsyncResult<String> ar = res.otherwise("whatever"); f.complete("foobar"); assertTrue(ar.succeeded()); assertFalse(ar.failed()); assertEquals("foobar", ar.result()); assertNull(ar.cause()); }
private void testUncompletedAsyncResultOtherwise(AsyncResult<String> res) { AsyncResult<String> ar1 = res.otherwise("something-else"); assertFalse(ar1.succeeded()); assertFalse(ar1.failed()); assertNull(ar1.result()); assertNull(ar1.cause()); }
private void testUncompletedOtherwiseApplyFunction(AsyncResult<String> res) { AsyncResult<String> ar1 = res.otherwise(Throwable::getMessage); assertFalse(ar1.succeeded()); assertFalse(ar1.failed()); assertNull(ar1.result()); assertNull(ar1.cause()); }
/** * Map the failure of this async result to a specific {@code value}.<p> * * When this async result fails, this {@code value} will succeeed the async result returned by this method call.<p> * * When this async succeeds, the result will be propagated to the returned async result. * * @param value the value that eventually completes the mapped async result * @return the mapped async result */ default AsyncResult<T> otherwise(T value) { return otherwise(err -> value); }
@Test public void testOtherwiseWithNullFunction() { Future<Integer> fut = Future.future(); try { fut.otherwise((Function<Throwable, Integer>) null); fail(); } catch (NullPointerException ignore) { } try { asyncResult(fut).otherwise((Function<Throwable, Integer>) null); fail(); } catch (NullPointerException ignore) { } }
/** * Map the failure of this async result to {@code null}.<p> * * This is a convenience for {@code asyncResult.otherwise((T) null)}.<p> * * When this async result fails, the {@code null} will succeeed the async result returned by this method call.<p> * * When this async succeeds, the result will be propagated to the returned async result. * * @return the mapped async result */ default AsyncResult<T> otherwiseEmpty() { return otherwise(err -> null); } }
private void testFailedOtherwise(AsyncResult<String> res, Future<String> f) { AsyncResult<String> map1 = res.otherwise("something-else"); Throwable cause = new Throwable("the-failure"); f.fail(cause); assertTrue(map1.succeeded()); assertFalse(map1.failed()); assertEquals("something-else", map1.result()); assertNull(map1.cause()); }
private void testFailedOtherwiseApplyFunction(AsyncResult<String> res, Future<String> f) { AsyncResult<String> map1 = res.otherwise(Throwable::getMessage); Throwable cause = new Throwable("the-failure"); f.fail(cause); assertTrue(map1.succeeded()); assertFalse(map1.failed()); assertEquals("the-failure", map1.result()); assertNull(map1.cause()); }
private void testSucceededOtherwiseApplyFunction(AsyncResult<String> res, Future<String> f) { AsyncResult<String> ar = res.otherwise("whatever"); f.complete("foobar"); assertTrue(ar.succeeded()); assertFalse(ar.failed()); assertEquals("foobar", ar.result()); assertNull(ar.cause()); }
private void testSucceededOtherwise(AsyncResult<String> res, Future<String> f) { AsyncResult<String> ar = res.otherwise(Throwable::getMessage); f.complete("foobar"); assertTrue(ar.succeeded()); assertFalse(ar.failed()); assertEquals("foobar", ar.result()); assertNull(ar.cause()); }
private void testUncompletedOtherwiseApplyFunction(AsyncResult<String> res) { AsyncResult<String> ar1 = res.otherwise(Throwable::getMessage); assertFalse(ar1.succeeded()); assertFalse(ar1.failed()); assertNull(ar1.result()); assertNull(ar1.cause()); }
private void testUncompletedAsyncResultOtherwise(AsyncResult<String> res) { AsyncResult<String> ar1 = res.otherwise("something-else"); assertFalse(ar1.succeeded()); assertFalse(ar1.failed()); assertNull(ar1.result()); assertNull(ar1.cause()); }
@Test public void testOtherwiseWithNullFunction() { Future<Integer> fut = Future.future(); try { fut.otherwise((Function<Throwable, Integer>) null); fail(); } catch (NullPointerException ignore) { } try { asyncResult(fut).otherwise((Function<Throwable, Integer>) null); fail(); } catch (NullPointerException ignore) { } }