@Test public void testOtherwiseFails() { RuntimeException cause = new RuntimeException("throw"); Future<String> f = Future.future(); Future<String> r = f.otherwise(t -> { throw cause; }); Checker<String> checker = new Checker<>(r); checker.assertNotCompleted(); f.fail("recovered"); checker.assertFailed(cause); }
@Test public void testOtherwiseFailureWithSuccess() { Future<String> f = Future.future(); Future<String> r = f.otherwise(t -> t.getMessage()); Checker<String> checker = new Checker<>(r); checker.assertNotCompleted(); f.fail("recovered"); checker.assertSucceeded("recovered"); }
@Test public void testOtherwiseSuccessWithSuccess() { AtomicBoolean called = new AtomicBoolean(); Future<String> f = Future.future(); Future<String> r = f.otherwise(t -> { called.set(true); throw new AssertionError(); }); Checker<String> checker = new Checker<>(r); checker.assertNotCompleted(); f.complete("yeah"); assertTrue(r.succeeded()); checker.assertSucceeded("yeah"); assertFalse(called.get()); }
@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) { } }
@Test public void testOtherwiseFails() { RuntimeException cause = new RuntimeException("throw"); Future<String> f = Future.future(); Future<String> r = f.otherwise(t -> { throw cause; }); Checker<String> checker = new Checker<>(r); checker.assertNotCompleted(); f.fail("recovered"); checker.assertFailed(cause); }
@Test public void testOtherwiseFailureWithSuccess() { Future<String> f = Future.future(); Future<String> r = f.otherwise(t -> t.getMessage()); Checker<String> checker = new Checker<>(r); checker.assertNotCompleted(); f.fail("recovered"); checker.assertSucceeded("recovered"); }
@Test public void testOtherwiseSuccessWithSuccess() { AtomicBoolean called = new AtomicBoolean(); Future<String> f = Future.future(); Future<String> r = f.otherwise(t -> { called.set(true); throw new AssertionError(); }); Checker<String> checker = new Checker<>(r); checker.assertNotCompleted(); f.complete("yeah"); assertTrue(r.succeeded()); checker.assertSucceeded("yeah"); assertFalse(called.get()); }
@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) { } }
public static io.vertx.core.Future<java.lang.Object> otherwise(io.vertx.core.Future<Object> j_receiver, java.util.function.Function<java.lang.Throwable, java.lang.Object> mapper) { return io.vertx.core.impl.ConversionHelper.fromObject(j_receiver.otherwise(mapper != null ? new java.util.function.Function<java.lang.Throwable, java.lang.Object>() { public java.lang.Object apply(java.lang.Throwable t) { java.lang.Throwable o = io.vertx.core.impl.ConversionHelper.fromObject(t); java.lang.Object p = mapper.apply(o); return io.vertx.core.impl.ConversionHelper.toObject(p); } } : null)); } public static io.vertx.core.Future<java.lang.Object> otherwise(io.vertx.core.Future<Object> j_receiver, java.lang.Object value) {
/** * Apply a <code>mapper</code> function on this future.<p> * * When this future fails, the <code>mapper</code> will be called with the completed value and this mapper * returns a value. This value will complete the future returned by this method call.<p> * * If the <code>mapper</code> throws an exception, the returned future will be failed with this exception.<p> * * When this future succeeds, the result will be propagated to the returned future and the <code>mapper</code> * will not be called. * @param mapper the mapper function * @return the mapped future */ public io.vertx.rxjava.core.Future<T> otherwise(Function<Throwable, T> mapper) { io.vertx.rxjava.core.Future<T> ret = io.vertx.rxjava.core.Future.newInstance(delegate.otherwise(new java.util.function.Function<java.lang.Throwable,T>() { public T apply(java.lang.Throwable arg) { T ret = mapper.apply(arg); return __typeArg_0.<T>unwrap(ret); } }), __typeArg_0); return ret; }
/** * Apply a <code>mapper</code> function on this future.<p> * * When this future fails, the <code>mapper</code> will be called with the completed value and this mapper * returns a value. This value will complete the future returned by this method call.<p> * * If the <code>mapper</code> throws an exception, the returned future will be failed with this exception.<p> * * When this future succeeds, the result will be propagated to the returned future and the <code>mapper</code> * will not be called. * @param mapper the mapper function * @return the mapped future */ public io.vertx.rxjava.core.Future<T> otherwise(Function<Throwable, T> mapper) { io.vertx.rxjava.core.Future<T> ret = io.vertx.rxjava.core.Future.newInstance(delegate.otherwise(new java.util.function.Function<java.lang.Throwable,T>() { public T apply(java.lang.Throwable arg) { T ret = mapper.apply(arg); return __typeArg_0.<T>unwrap(ret); } }), __typeArg_0); return ret; }
/** * Closes a command consumer for a device. * <p> * If no command consumer for the device is open, this method does nothing. * * @param tenantId The tenant that the device belongs to. * @param deviceId The identifier of the device. */ protected final void closeCommandConsumer(final String tenantId, final String deviceId) { getCommandConnection().closeCommandConsumer(tenantId, deviceId).otherwise(t -> { LOG.warn("cannot close command consumer [tenant-id: {}, device-id: {}]: {}", tenantId, deviceId, t.getMessage()); return null; }); }
/** * Closes a command consumer for a device. * <p> * If no command consumer for the device is open, this method does nothing. * * @param tenantId The tenant that the device belongs to. * @param deviceId The identifier of the device. */ protected final void closeCommandConsumer(final String tenantId, final String deviceId) { getCommandConnection().closeCommandConsumer(tenantId, deviceId).otherwise(t -> { LOG.warn("cannot close command consumer [tenant-id: {}, device-id: {}]: {}", tenantId, deviceId, t.getMessage()); return null; }); }
/** * Map the failure of a future to a specific <code>value</code>.<p> * * When this future fails, this <code>value</code> will complete the future returned by this method call.<p> * * When this future succeeds, the result will be propagated to the returned future. * @param value the value that eventually completes the mapped future * @return the mapped future */ public io.vertx.rxjava.core.Future<T> otherwise(T value) { io.vertx.rxjava.core.Future<T> ret = io.vertx.rxjava.core.Future.newInstance(delegate.otherwise(__typeArg_0.<T>unwrap(value)), __typeArg_0); return ret; }
public static io.vertx.core.Future<java.lang.Object> otherwise(io.vertx.core.Future<Object> j_receiver, java.lang.Object value) { return io.vertx.core.impl.ConversionHelper.fromObject(j_receiver.otherwise(io.vertx.core.impl.ConversionHelper.toObject(value))); } }
/** * Map the failure of a future to a specific <code>value</code>.<p> * * When this future fails, this <code>value</code> will complete the future returned by this method call.<p> * * When this future succeeds, the result will be propagated to the returned future. * @param value the value that eventually completes the mapped future * @return the mapped future */ public io.vertx.rxjava.core.Future<T> otherwise(T value) { io.vertx.rxjava.core.Future<T> ret = io.vertx.rxjava.core.Future.newInstance(delegate.otherwise(__typeArg_0.<T>unwrap(value)), __typeArg_0); return ret; }
Future<Void> saveToFile() { if (!getConfig().isSaveToFile()) { return Future.succeededFuture(); } else if (dirty) { return checkFileExists(true).compose(s -> { final JsonArray tenantsJson = new JsonArray(); tenants.values().stream().forEach(tenant -> { tenantsJson.add(JsonObject.mapFrom(tenant)); }); final Future<Void> writeHandler = Future.future(); vertx.fileSystem().writeFile(getConfig().getFilename(), Buffer.factory.buffer(tenantsJson.encodePrettily()), writeHandler.completer()); return writeHandler.map(ok -> { dirty = false; log.trace("successfully wrote {} tenants to file {}", tenantsJson.size(), getConfig().getFilename()); return (Void) null; }).otherwise(t -> { log.warn("could not write tenants to file {}", getConfig().getFilename(), t); return (Void) null; }); }); } else { log.trace("tenants registry does not need to be persisted"); return Future.succeededFuture(); } }
/** * Create a command client for the device for that a {@link TimeUntilDisconnectNotification} was received, if no such * command client is already active. * @param notification The notification that was received for the device. */ private Future<CommandClient> createCommandClientAndSendCommand(final TimeUntilDisconnectNotification notification) { return honoClient.getOrCreateCommandClient(notification.getTenantId(), notification.getDeviceId()) .map(commandClient -> { commandClient.setRequestTimeout(calculateCommandTimeout(notification)); // send the command upstream to the device if (SEND_ONE_WAY_COMMANDS) { sendOneWayCommandToAdapter(commandClient, notification); } else { sendCommandToAdapter(commandClient, notification); } return commandClient; }).otherwise(t -> { LOG.error("Could not create command client", t); return null; }); }
/** * Registers a check that succeeds if this component is connected to the services it depends on. * * @see #isConnected() */ @Override public void registerReadinessChecks(final HealthCheckHandler handler) { handler.register("connection-to-services", status -> { isConnected().map(connected -> { status.tryComplete(Status.OK()); return null; }).otherwise(t -> { status.tryComplete(Status.KO()); return null; }); }); }
/** * Registers a check that succeeds if this component is connected to the services it depends on. * * @see #isConnected() */ @Override public void registerReadinessChecks(final HealthCheckHandler handler) { handler.register("connection-to-services", status -> { isConnected().map(connected -> { status.tryComplete(Status.OK()); return null; }).otherwise(t -> { status.tryComplete(Status.KO()); return null; }); }); }