本文整理了Java中io.vavr.control.Try.isSuccess()
方法的一些代码示例,展示了Try.isSuccess()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Try.isSuccess()
方法的具体详情如下:
包路径:io.vavr.control.Try
类名称:Try
方法名:isSuccess
[英]Checks if this is a Success.
[中]检查这是否成功。
代码示例来源:origin: vavr-io/vavr
@SuppressWarnings("unchecked")
default Try<T> orElse(Try<? extends T> other) {
Objects.requireNonNull(other, "other is null");
return isSuccess() ? this : (Try<T>) other;
}
代码示例来源:origin: vavr-io/vavr
@SuppressWarnings("unchecked")
default Try<T> orElse(Supplier<? extends Try<? extends T>> supplier) {
Objects.requireNonNull(supplier, "supplier is null");
return isSuccess() ? this : (Try<T>) supplier.get();
}
代码示例来源:origin: vavr-io/vavr
/**
* Applies the action to the value of a Success or does nothing in the case of a Failure.
*
* @param action A Consumer
* @return this {@code Try}
* @throws NullPointerException if {@code action} is null
*/
@Override
default Try<T> peek(Consumer<? super T> action) {
Objects.requireNonNull(action, "action is null");
if (isSuccess()) {
action.accept(get());
}
return this;
}
代码示例来源:origin: vavr-io/vavr
default Future<T> orElse(Supplier<? extends Future<? extends T>> supplier) {
Objects.requireNonNull(supplier, "supplier is null");
return run(executor(), complete ->
onComplete(result -> {
if (result.isSuccess()) {
complete.with(result);
} else {
supplier.get().onComplete(complete::with);
}
})
);
}
代码示例来源:origin: vavr-io/vavr
/**
* Creates a {@code Validation} of an {@code Try}.
*
* @param t A {@code Try}
* @param <T> type of the valid value
* @return A {@code Valid(t.get())} if t is a Success, otherwise {@code Invalid(t.getCause())}.
* @throws NullPointerException if {@code t} is null
*/
static <T> Validation<Throwable, T> fromTry(Try<? extends T> t) {
Objects.requireNonNull(t, "t is null");
return t.isSuccess() ? valid(t.get()) : invalid(t.getCause());
}
代码示例来源:origin: vavr-io/vavr
/**
* Checks if this Future completed with a success.
*
* @return true, if this Future completed and is a Success, false otherwise.
*/
default boolean isSuccess() {
return isCompleted() && getValue().get().isSuccess();
}
代码示例来源:origin: vavr-io/vavr
@Override
default Iterator<T> iterator() {
return isSuccess() ? Iterator.of(get()) : Iterator.empty();
}
代码示例来源:origin: resilience4j/resilience4j
@Override
public Response<T> execute() throws IOException {
CheckedFunction0<Response<T>> restrictedSupplier = RateLimiter.decorateCheckedSupplier(rateLimiter, call::execute);
final Try<Response<T>> response = Try.of(restrictedSupplier);
return response.isSuccess() ? response.get() : handleFailure(response);
}
代码示例来源:origin: vavr-io/vavr
default Future<T> orElse(Future<? extends T> other) {
Objects.requireNonNull(other, "other is null");
return run(executor(), complete ->
onComplete(result -> {
if (result.isSuccess()) {
complete.with(result);
} else {
other.onComplete(complete::with);
}
})
);
}
代码示例来源:origin: resilience4j/resilience4j
@Test
public void shouldInvokeRecoverFunction() {
// tag::shouldInvokeRecoverFunction[]
// Given
CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("testName");
// When I decorate my function and invoke the decorated function
CheckedFunction0<String> checkedSupplier = CircuitBreaker.decorateCheckedSupplier(circuitBreaker, () -> {
throw new RuntimeException("BAM!");
});
Try<String> result = Try.of(checkedSupplier)
.recover(throwable -> "Hello Recovery");
// Then the function should be a success, because the exception could be recovered
assertThat(result.isSuccess()).isTrue();
// and the result must match the result of the recovery function.
assertThat(result.get()).isEqualTo("Hello Recovery");
// end::shouldInvokeRecoverFunction[]
}
代码示例来源: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 shouldInvokeMap() {
// tag::shouldInvokeMap[]
// Given
CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("testName");
// When I decorate my function
CheckedFunction0<String> decoratedSupplier = CircuitBreaker
.decorateCheckedSupplier(circuitBreaker, () -> "This can be any method which returns: 'Hello");
// and chain an other function with map
Try<String> result = Try.of(decoratedSupplier)
.map(value -> value + " world'");
// Then the Try Monad returns a Success<String>, if all functions ran successfully.
assertThat(result.isSuccess()).isTrue();
assertThat(result.get()).isEqualTo("This can be any method which returns: 'Hello world'");
// end::shouldInvokeMap[]
}
代码示例来源:origin: resilience4j/resilience4j
@Test
public void shouldInvokeMap() {
// tag::shouldInvokeMap[]
// Given
Bulkhead bulkhead = Bulkhead.of("testName", config);
// When I decorate my function
CheckedFunction0<String> decoratedSupplier = Bulkhead.decorateCheckedSupplier(bulkhead, () -> "This can be any method which returns: 'Hello");
// and chain an other function with map
Try<String> result = Try.of(decoratedSupplier)
.map(value -> value + " world'");
// Then the Try Monad returns a Success<String>, if all functions ran successfully.
assertThat(result.isSuccess()).isTrue();
assertThat(result.get()).isEqualTo("This can be any method which returns: 'Hello world'");
assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
// end::shouldInvokeMap[]
}
代码示例来源:origin: resilience4j/resilience4j
@Test
public void decorateCheckedRunnable() throws Throwable {
CheckedRunnable runnable = mock(CheckedRunnable.class);
CheckedRunnable decorated = RateLimiter.decorateCheckedRunnable(limit, runnable);
when(limit.getPermission(config.getTimeoutDuration()))
.thenReturn(false);
Try decoratedRunnableResult = Try.run(decorated);
then(decoratedRunnableResult.isFailure()).isTrue();
then(decoratedRunnableResult.getCause()).isInstanceOf(RequestNotPermitted.class);
verify(runnable, never()).run();
when(limit.getPermission(config.getTimeoutDuration()))
.thenReturn(true);
Try secondRunnableResult = Try.run(decorated);
then(secondRunnableResult.isSuccess()).isTrue();
verify(runnable, times(1)).run();
}
代码示例来源: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: resilience4j/resilience4j
@Test
public void decorateCheckedSupplier() throws Throwable {
CheckedFunction0 supplier = mock(CheckedFunction0.class);
CheckedFunction0 decorated = RateLimiter.decorateCheckedSupplier(limit, supplier);
when(limit.getPermission(config.getTimeoutDuration()))
.thenReturn(false);
Try decoratedSupplierResult = Try.of(decorated);
then(decoratedSupplierResult.isFailure()).isTrue();
then(decoratedSupplierResult.getCause()).isInstanceOf(RequestNotPermitted.class);
verify(supplier, never()).apply();
when(limit.getPermission(config.getTimeoutDuration()))
.thenReturn(true);
Try secondSupplierResult = Try.of(decorated);
then(secondSupplierResult.isSuccess()).isTrue();
verify(supplier, times(1)).apply();
}
代码示例来源:origin: vavr-io/vavr
/**
* Maps the cause to a new exception if this is a {@code Failure} or returns this instance if this is a {@code Success}.
* <p>
* If none of the given cases matches the cause, the same {@code Failure} is returned.
*
* @param cases A not necessarily exhaustive sequence of cases that will be matched against a cause.
* @return A new {@code Try} if this is a {@code Failure}, otherwise this.
*/
@GwtIncompatible
@SuppressWarnings({ "unchecked", "varargs" })
default Try<T> mapFailure(Match.Case<? extends Throwable, ? extends Throwable>... cases) {
if (isSuccess()) {
return this;
} else {
final Option<Throwable> x = Match(getCause()).option(cases);
return x.isEmpty() ? this : failure(x.get());
}
}
代码示例来源: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);
}
内容来源于网络,如有侵权,请联系作者删除!