本文整理了Java中io.vertx.core.Future.otherwise()
方法的一些代码示例,展示了Future.otherwise()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Future.otherwise()
方法的具体详情如下:
包路径:io.vertx.core.Future
类名称:Future
方法名:otherwise
[英]Map the failure of a future to a specific value.
When this future fails, this value will complete the future returned by this method call.
When this future succeeds, the result will be propagated to the returned future.
[中]将未来的失败映射到特定值。
当此future失败时,此值将完成此方法调用返回的future。
当此未来成功时,结果将传播到返回的未来。
代码示例来源:origin: eclipse-vertx/vert.x
@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);
}
代码示例来源:origin: eclipse-vertx/vert.x
@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");
}
代码示例来源:origin: eclipse-vertx/vert.x
@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());
}
代码示例来源:origin: eclipse-vertx/vert.x
@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) {
}
}
代码示例来源:origin: io.vertx/vertx-core
@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);
}
代码示例来源:origin: io.vertx/vertx-core
@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");
}
代码示例来源:origin: io.vertx/vertx-core
@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());
}
代码示例来源:origin: io.vertx/vertx-core
@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) {
}
}
代码示例来源:origin: io.vertx/vertx-lang-groovy
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) {
代码示例来源:origin: io.vertx/vertx-rx-java
/**
* 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;
}
代码示例来源:origin: vert-x3/vertx-rx
/**
* 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;
}
代码示例来源:origin: eclipse/hono
/**
* 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;
});
}
代码示例来源:origin: org.eclipse.hono/hono-service-base
/**
* 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;
});
}
代码示例来源:origin: io.vertx/vertx-rx-java
/**
* 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;
}
代码示例来源:origin: io.vertx/vertx-lang-groovy
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)));
}
}
代码示例来源:origin: vert-x3/vertx-rx
/**
* 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;
}
代码示例来源:origin: eclipse/hono
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();
}
}
代码示例来源:origin: eclipse/hono
/**
* 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;
});
}
代码示例来源:origin: eclipse/hono
/**
* 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;
});
});
}
代码示例来源:origin: org.eclipse.hono/hono-service-base
/**
* 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;
});
});
}
内容来源于网络,如有侵权,请联系作者删除!