/** * Map the result of this async result to a specific {@code value}.<p> * * When this async result succeeds, this {@code value} will succeeed the async result returned by this method call.<p> * * When this async result fails, the failure 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 <V> AsyncResult<V> map(V value) { return map(t -> value); }
/** * Map the result of this async result to {@code null}.<p> * * This is a convenience for {@code asyncResult.map((T) null)} or {@code asyncResult.map((Void) null)}.<p> * * When this async result succeeds, {@code null} will succeeed the async result returned by this method call.<p> * * When this async result fails, the failure will be propagated to the returned async result. * * @return the mapped async result */ default <V> AsyncResult<V> mapEmpty() { return map((V)null); }
@Override public synchronized NetServer listen(SocketAddress localAddress, Handler<AsyncResult<NetServer>> listenHandler) { listen(handler, localAddress, ar -> { if (listenHandler != null) { listenHandler.handle(ar.map(this)); } }); return this; }
private <T> void lookupSingle(String name, Handler<AsyncResult<T>> handler, DnsRecordType... types) { this.<T>lookupList(name, ar -> handler.handle(ar.map(result -> result.isEmpty() ? null : result.get(0))), types); }
@Override public NetClient connect(int port, String host, String serverName, Handler<AsyncResult<NetSocket>> connectHandler) { doConnect(SocketAddress.inetSocketAddress(port, host), serverName, connectHandler != null ? ar -> connectHandler.handle(ar.map(s -> (NetSocket) s)) : null); return this; }
@Test public void testUncompletedAsyncResultMap() { Future<String> f = Future.future(); AsyncResult<String> res = asyncResult(f); AsyncResult<Integer> map1 = res.map(String::length); AsyncResult<Integer> map2 = res.map(17); assertNull(map1.result()); assertNull(map1.cause()); assertNull(map2.result()); assertNull(map2.cause()); }
@Test public void testFailedAsyncResultMap() { Future<String> f = Future.future(); AsyncResult<String> res = asyncResult(f); AsyncResult<Integer> map1 = res.map(String::length); AsyncResult<Integer> map2 = res.map(17); Throwable cause = new Throwable(); f.fail(cause); assertNull(map1.result()); assertSame(cause, map1.cause()); assertNull(map2.result()); assertSame(cause, map2.cause()); }
@Test public void testSucceededAsyncResultMap() { Future<String> f = Future.future(); AsyncResult<String> res = asyncResult(f); AsyncResult<Integer> map1 = res.map(String::length); AsyncResult<Integer> map2 = res.map(17); f.complete("foobar"); assertEquals(6, (int)map1.result()); assertNull(map1.cause()); assertEquals(17, (int)map2.result()); assertNull(map2.cause()); }
@Test public void testMapWithNullFunction() { Future<Integer> fut = Future.future(); try { fut.map((Function<Integer, String>) null); fail(); } catch (NullPointerException ignore) { } try { asyncResult(fut).map((Function<Integer, String>) null); fail(); } catch (NullPointerException ignore) { } }
/** * Map the result of this async result to {@code null}.<p> * * This is a convenience for {@code asyncResult.map((T) null)} or {@code asyncResult.map((Void) null)}.<p> * * When this async result succeeds, {@code null} will succeeed the async result returned by this method call.<p> * * When this async result fails, the failure will be propagated to the returned async result. * * @return the mapped async result */ default <V> AsyncResult<V> mapEmpty() { return map((V)null); }
/** * Map the result of this async result to a specific {@code value}.<p> * * When this async result succeeds, this {@code value} will succeeed the async result returned by this method call.<p> * * When this async result fails, the failure 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 <V> AsyncResult<V> map(V value) { return map(t -> value); }
@Override public synchronized NetServer listen(SocketAddress localAddress, Handler<AsyncResult<NetServer>> listenHandler) { listen(handler, localAddress, ar -> { if (listenHandler != null) { listenHandler.handle(ar.map(this)); } }); return this; }
private <T> void lookupSingle(String name, Handler<AsyncResult<T>> handler, DnsRecordType... types) { this.<T>lookupList(name, ar -> handler.handle(ar.map(result -> result.isEmpty() ? null : result.get(0))), types); }
@Override public NetClient connect(int port, String host, String serverName, Handler<AsyncResult<NetSocket>> connectHandler) { doConnect(SocketAddress.inetSocketAddress(port, host), serverName, connectHandler != null ? ar -> connectHandler.handle(ar.map(s -> (NetSocket) s)) : null); return this; }
@Test public void testFailedAsyncResultMap() { Future<String> f = Future.future(); AsyncResult<String> res = asyncResult(f); AsyncResult<Integer> map1 = res.map(String::length); AsyncResult<Integer> map2 = res.map(17); Throwable cause = new Throwable(); f.fail(cause); assertNull(map1.result()); assertSame(cause, map1.cause()); assertNull(map2.result()); assertSame(cause, map2.cause()); }
@Test public void testUncompletedAsyncResultMap() { Future<String> f = Future.future(); AsyncResult<String> res = asyncResult(f); AsyncResult<Integer> map1 = res.map(String::length); AsyncResult<Integer> map2 = res.map(17); assertNull(map1.result()); assertNull(map1.cause()); assertNull(map2.result()); assertNull(map2.cause()); }
@Test public void testSucceededAsyncResultMap() { Future<String> f = Future.future(); AsyncResult<String> res = asyncResult(f); AsyncResult<Integer> map1 = res.map(String::length); AsyncResult<Integer> map2 = res.map(17); f.complete("foobar"); assertEquals(6, (int)map1.result()); assertNull(map1.cause()); assertEquals(17, (int)map2.result()); assertNull(map2.cause()); }
@Test public void testMapWithNullFunction() { Future<Integer> fut = Future.future(); try { fut.map((Function<Integer, String>) null); fail(); } catch (NullPointerException ignore) { } try { asyncResult(fut).map((Function<Integer, String>) null); fail(); } catch (NullPointerException ignore) { } }
@Override void wait(long index, Handler<AsyncResult<State<KeyValueList>>> handler) { BlockingQueryOptions options = new BlockingQueryOptions().setWait(BLOCKING_WAIT).setIndex(index); consulClient.getValuesWithOptions(keyPrefix, options, h -> handler.handle(h.map(kv -> new State<KeyValueList>(kv, kv.getIndex())))); } }
@Override void wait(long index, Handler<AsyncResult<State<KeyValue>>> handler) { BlockingQueryOptions options = new BlockingQueryOptions().setWait(BLOCKING_WAIT).setIndex(index); consulClient.getValueWithOptions(key, options, h -> handler.handle(h.map(kv -> new State<KeyValue>(kv, kv.getModifyIndex())))); } }