本文整理了Java中io.vavr.control.Try.success()
方法的一些代码示例,展示了Try.success()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Try.success()
方法的具体详情如下:
包路径:io.vavr.control.Try
类名称:Try
方法名:success
[英]Creates a Success that contains the given value. Shortcut for new Success<>(value).
[中]创建包含给定值的成功。新成功的捷径<>(价值)。
代码示例来源:origin: vavr-io/vavr
/**
* Alias for {@link Try#success(Object)}
*
* @param <T> Type of the given {@code value}.
* @param value A value.
* @return A new {@link Try.Success}.
*/
@SuppressWarnings("unchecked")
public static <T> Try.Success<T> Success(T value) {
return (Try.Success<T>) Try.success(value);
}
代码示例来源:origin: vavr-io/vavr
/**
* Completes this {@code Promise} with the given {@code value}.
*
* @param value A value.
* @return {@code false} if this {@code Promise} has already been completed, {@code true} otherwise.
*/
default boolean trySuccess(T value) {
return tryComplete(Try.success(value));
}
代码示例来源:origin: vavr-io/vavr
/**
* Completes this {@code Promise} with the given {@code value}.
*
* @param value A value.
* @return This {@code Promise}.
* @throws IllegalStateException if this {@code Promise} has already been completed.
*/
default Promise<T> success(T value) {
return complete(Try.success(value));
}
代码示例来源:origin: vavr-io/vavr
/**
* Creates a succeeded {@code Future}, backed by the given {@link Executor}.
*
* @param executor An {@link Executor}.
* @param result The result.
* @param <T> The value type of a successful result.
* @return A succeeded {@code Future}.
* @throws NullPointerException if executor is null
*/
static <T> Future<T> successful(Executor executor, T result) {
Objects.requireNonNull(executor, "executor is null");
return FutureImpl.of(executor, Try.success(result));
}
代码示例来源:origin: vavr-io/vavr
/**
* Returns {@code this}, if this is a {@link Try.Success} or this is a {@code Failure} and the cause is not assignable
* from {@code cause.getClass()}. Otherwise returns a {@link Try.Success} containing the given {@code value}.
*
* <pre>{@code
* // = Success(13)
* Try.of(() -> 27/2).recover(ArithmeticException.class, Integer.MAX_VALUE);
*
* // = Success(2147483647)
* Try.of(() -> 1/0)
* .recover(Error.class, -1);
* .recover(ArithmeticException.class, Integer.MAX_VALUE);
*
* // = Failure(java.lang.ArithmeticException: / by zero)
* Try.of(() -> 1/0).recover(Error.class, Integer.MAX_VALUE);
* }</pre>
*
* @param <X> Exception type
* @param exceptionType The specific exception type that should be handled
* @param value A value that is used in case of a recovery
* @return a {@code Try}
* @throws NullPointerException if {@code exception} is null
*/
@GwtIncompatible
default <X extends Throwable> Try<T> recover(Class<X> exceptionType, T value) {
Objects.requireNonNull(exceptionType, "exceptionType is null");
return (isFailure() && exceptionType.isAssignableFrom(getCause().getClass()))
? Try.success(value)
: this;
}
代码示例来源:origin: vavr-io/vavr
/**
* Creates a {@code Future} with the given {@link java.util.concurrent.CompletableFuture}, backed by given {@link Executor}
*
* @param executor An {@link Executor}.
* @param future A {@link java.util.concurrent.CompletableFuture}.
* @param <T> Result type of the Future
* @return A new {@code Future} wrapping the result of the {@link java.util.concurrent.CompletableFuture}
* @throws NullPointerException if executor or future is null
*/
@GwtIncompatible
static <T> Future<T> fromCompletableFuture(Executor executor, CompletableFuture<T> future) {
Objects.requireNonNull(executor, "executor is null");
Objects.requireNonNull(future, "future is null");
if (future.isDone() || future.isCompletedExceptionally() || future.isCancelled()) {
return fromTry(Try.of(future::get).recoverWith(error -> Try.failure(error.getCause())));
} else {
return run(executor, complete ->
future.handle((t, err) -> complete.with((err == null) ? Try.success(t) : Try.failure(err)))
);
}
}
代码示例来源:origin: vavr-io/vavr
.onSuccess(value -> completed.set(complete.with(Try.success(Option.some(value)))))
.onFailure(ignored -> {
if (wasLast) {
completed.set(complete.with(Try.success(Option.none())));
代码示例来源:origin: vavr-io/vavr
/**
* Reduces many {@code Try}s into a single {@code Try} by transforming an
* {@code Iterable<Try<? extends T>>} into a {@code Try<Seq<T>>}. If any of
* the {@code Try}s are {@link Try.Failure}, then this returns a {@link Try.Failure}.
*
* @param values An {@link Iterable} of {@code Try}s
* @param <T> type of the Trys
* @return A {@code Try} of a {@link Seq} of results
* @throws NullPointerException if {@code values} is null
*/
static <T> Try<Seq<T>> sequence(Iterable<? extends Try<? extends T>> values) {
Objects.requireNonNull(values, "values is null");
Vector<T> vector = Vector.empty();
for (Try<? extends T> value : values) {
if (value.isFailure()) {
return Try.failure(value.getCause());
}
vector = vector.append(value.get());
}
return Try.success(vector);
}
代码示例来源:origin: resilience4j/resilience4j
@Test
public void decorateConsumer() throws Exception {
Consumer<Integer> consumer = mock(Consumer.class);
Consumer<Integer> decorated = RateLimiter.decorateConsumer(limit, consumer);
when(limit.getPermission(config.getTimeoutDuration()))
.thenReturn(false);
Try<Integer> decoratedConsumerResult = Try.success(1).andThen(decorated);
then(decoratedConsumerResult.isFailure()).isTrue();
then(decoratedConsumerResult.getCause()).isInstanceOf(RequestNotPermitted.class);
verify(consumer, never()).accept(any());
when(limit.getPermission(config.getTimeoutDuration()))
.thenReturn(true);
Try secondConsumerResult = Try.success(1).andThen(decorated);
then(secondConsumerResult.isSuccess()).isTrue();
verify(consumer, times(1)).accept(1);
}
代码示例来源:origin: resilience4j/resilience4j
@Test
public void decorateFunction() throws Exception {
Function<Integer, String> function = mock(Function.class);
Function<Integer, String> decorated = RateLimiter.decorateFunction(limit, function);
when(limit.getPermission(config.getTimeoutDuration()))
.thenReturn(false);
Try<String> decoratedFunctionResult = Try.success(1).map(decorated);
then(decoratedFunctionResult.isFailure()).isTrue();
then(decoratedFunctionResult.getCause()).isInstanceOf(RequestNotPermitted.class);
verify(function, never()).apply(any());
when(limit.getPermission(config.getTimeoutDuration()))
.thenReturn(true);
Try secondFunctionResult = Try.success(1).map(decorated);
then(secondFunctionResult.isSuccess()).isTrue();
verify(function, times(1)).apply(1);
}
代码示例来源:origin: resilience4j/resilience4j
@Test
public void decorateRunnable() throws Exception {
Runnable runnable = mock(Runnable.class);
Runnable decorated = RateLimiter.decorateRunnable(limit, runnable);
when(limit.getPermission(config.getTimeoutDuration()))
.thenReturn(false);
Try decoratedRunnableResult = Try.success(decorated).andThen(Runnable::run);
then(decoratedRunnableResult.isFailure()).isTrue();
then(decoratedRunnableResult.getCause()).isInstanceOf(RequestNotPermitted.class);
verify(runnable, never()).run();
when(limit.getPermission(config.getTimeoutDuration()))
.thenReturn(true);
Try secondRunnableResult = Try.success(decorated).andThen(Runnable::run);
then(secondRunnableResult.isSuccess()).isTrue();
verify(runnable, times(1)).run();
}
代码示例来源:origin: resilience4j/resilience4j
@Test
public void decorateSupplier() throws Exception {
Supplier supplier = mock(Supplier.class);
Supplier decorated = RateLimiter.decorateSupplier(limit, supplier);
when(limit.getPermission(config.getTimeoutDuration()))
.thenReturn(false);
Try decoratedSupplierResult = Try.success(decorated).map(Supplier::get);
then(decoratedSupplierResult.isFailure()).isTrue();
then(decoratedSupplierResult.getCause()).isInstanceOf(RequestNotPermitted.class);
verify(supplier, never()).get();
when(limit.getPermission(config.getTimeoutDuration()))
.thenReturn(true);
Try secondSupplierResult = Try.success(decorated).map(Supplier::get);
then(secondSupplierResult.isSuccess()).isTrue();
verify(supplier, times(1)).get();
}
代码示例来源:origin: vavr-io/vavr
/**
* A projection that inverses the result of this Future.
* <p>
* If this Future succeeds, the failed projection returns a failure containing a {@code NoSuchElementException}.
* <p>
* If this Future fails, the failed projection returns a success containing the exception.
*
* @return A new Future which contains an exception at a point of time.
*/
default Future<Throwable> failed() {
return run(executor(), complete ->
onComplete(result -> {
if (result.isFailure()) {
complete.with(Try.success(result.getCause()));
} else {
complete.with(Try.failure(new NoSuchElementException("Future.failed completed without a throwable")));
}
})
);
}
代码示例来源:origin: resilience4j/resilience4j
@Test
public void decorateCheckedFunction() throws Throwable {
CheckedFunction1<Integer, String> function = mock(CheckedFunction1.class);
CheckedFunction1<Integer, String> decorated = RateLimiter.decorateCheckedFunction(limit, function);
when(limit.getPermission(config.getTimeoutDuration()))
.thenReturn(false);
Try<String> decoratedFunctionResult = Try.success(1).mapTry(decorated);
then(decoratedFunctionResult.isFailure()).isTrue();
then(decoratedFunctionResult.getCause()).isInstanceOf(RequestNotPermitted.class);
verify(function, never()).apply(any());
when(limit.getPermission(config.getTimeoutDuration()))
.thenReturn(true);
Try secondFunctionResult = Try.success(1).mapTry(decorated);
then(secondFunctionResult.isSuccess()).isTrue();
verify(function, times(1)).apply(1);
}
代码示例来源:origin: resilience4j/resilience4j
@Test
public void decorateFutureSupplier() throws Throwable {
when(timeLimiter.getTimeLimiterConfig()).thenReturn(shortConfig);
Future<Integer> future = EXECUTOR_SERVICE.submit(() -> {
Thread.sleep(SLEEP_DURATION.toMillis());
return 1;
}
);
Supplier<Future<Integer>> supplier = () -> future;
Callable<Integer> decorated = TimeLimiter.decorateFutureSupplier(timeLimiter, supplier);
Try decoratedResult = Try.success(decorated).mapTry(Callable::call);
then(decoratedResult.isFailure()).isTrue();
then(decoratedResult.getCause()).isInstanceOf(TimeoutException.class);
then(future.isCancelled()).isTrue();
when(timeLimiter.getTimeLimiterConfig())
.thenReturn(longConfig);
Future<Integer> secondFuture = EXECUTOR_SERVICE.submit(() -> {
Thread.sleep(SLEEP_DURATION.toMillis());
return 1;
}
);
supplier = () -> secondFuture;
decorated = TimeLimiter.decorateFutureSupplier(timeLimiter, supplier);
Try secondResult = Try.success(decorated).mapTry(Callable::call);
then(secondResult.isSuccess()).isTrue();
}
代码示例来源:origin: jdbi/jdbi
@Test
public void testGetTrySuccessShouldReturnCorrectRow() {
Something result = dbRule.getSharedHandle().createQuery(SELECT_BY_NAME)
.bind("name", Try.success("brian"))
.mapToBean(Something.class)
.findOnly();
assertThat(result).isEqualTo(BRIANSOMETHING);
}
代码示例来源:origin: io.vavr/vavr
/**
* Alias for {@link Try#success(Object)}
*
* @param <T> Type of the given {@code value}.
* @param value A value.
* @return A new {@link Try.Success}.
*/
@SuppressWarnings("unchecked")
public static <T> Try.Success<T> Success(T value) {
return (Try.Success<T>) Try.success(value);
}
代码示例来源:origin: io.vavr/vavr
/**
* Completes this {@code Promise} with the given {@code value}.
*
* @param value A value.
* @return This {@code Promise}.
* @throws IllegalStateException if this {@code Promise} has already been completed.
*/
default Promise<T> success(T value) {
return complete(Try.success(value));
}
代码示例来源:origin: io.vavr/vavr
/**
* Completes this {@code Promise} with the given {@code value}.
*
* @param value A value.
* @return {@code false} if this {@code Promise} has already been completed, {@code true} otherwise.
*/
default boolean trySuccess(T value) {
return tryComplete(Try.success(value));
}
代码示例来源:origin: martincooper/java-datatable
/**
* Returns the IDataColumn at the specified index.
* Performs bounds check, returns results in a Try.
*
* @param index The index to return the IDataColumn.
* @return Returns the IDataColumn.
*/
public Try<IDataColumn> tryGet(int index) {
return VectorExtensions.outOfBounds(this.columns, index)
? DataTableException.tryError("Column index out of bounds")
: Try.success(get(index));
}
内容来源于网络,如有侵权,请联系作者删除!