io.vavr.control.Try.isSuccess()方法的使用及代码示例

x33g5p2x  于2022-01-30 转载在 其他  
字(10.9k)|赞(0)|评价(0)|浏览(241)

本文整理了Java中io.vavr.control.Try.isSuccess()方法的一些代码示例,展示了Try.isSuccess()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Try.isSuccess()方法的具体详情如下:
包路径:io.vavr.control.Try
类名称:Try
方法名:isSuccess

Try.isSuccess介绍

[英]Checks if this is a Success.
[中]检查这是否成功。

代码示例

代码示例来源:origin: vavr-io/vavr

  1. @SuppressWarnings("unchecked")
  2. default Try<T> orElse(Try<? extends T> other) {
  3. Objects.requireNonNull(other, "other is null");
  4. return isSuccess() ? this : (Try<T>) other;
  5. }

代码示例来源:origin: vavr-io/vavr

  1. @SuppressWarnings("unchecked")
  2. default Try<T> orElse(Supplier<? extends Try<? extends T>> supplier) {
  3. Objects.requireNonNull(supplier, "supplier is null");
  4. return isSuccess() ? this : (Try<T>) supplier.get();
  5. }

代码示例来源:origin: vavr-io/vavr

  1. /**
  2. * Applies the action to the value of a Success or does nothing in the case of a Failure.
  3. *
  4. * @param action A Consumer
  5. * @return this {@code Try}
  6. * @throws NullPointerException if {@code action} is null
  7. */
  8. @Override
  9. default Try<T> peek(Consumer<? super T> action) {
  10. Objects.requireNonNull(action, "action is null");
  11. if (isSuccess()) {
  12. action.accept(get());
  13. }
  14. return this;
  15. }

代码示例来源:origin: vavr-io/vavr

  1. default Future<T> orElse(Supplier<? extends Future<? extends T>> supplier) {
  2. Objects.requireNonNull(supplier, "supplier is null");
  3. return run(executor(), complete ->
  4. onComplete(result -> {
  5. if (result.isSuccess()) {
  6. complete.with(result);
  7. } else {
  8. supplier.get().onComplete(complete::with);
  9. }
  10. })
  11. );
  12. }

代码示例来源:origin: vavr-io/vavr

  1. /**
  2. * Creates a {@code Validation} of an {@code Try}.
  3. *
  4. * @param t A {@code Try}
  5. * @param <T> type of the valid value
  6. * @return A {@code Valid(t.get())} if t is a Success, otherwise {@code Invalid(t.getCause())}.
  7. * @throws NullPointerException if {@code t} is null
  8. */
  9. static <T> Validation<Throwable, T> fromTry(Try<? extends T> t) {
  10. Objects.requireNonNull(t, "t is null");
  11. return t.isSuccess() ? valid(t.get()) : invalid(t.getCause());
  12. }

代码示例来源:origin: vavr-io/vavr

  1. /**
  2. * Checks if this Future completed with a success.
  3. *
  4. * @return true, if this Future completed and is a Success, false otherwise.
  5. */
  6. default boolean isSuccess() {
  7. return isCompleted() && getValue().get().isSuccess();
  8. }

代码示例来源:origin: vavr-io/vavr

  1. @Override
  2. default Iterator<T> iterator() {
  3. return isSuccess() ? Iterator.of(get()) : Iterator.empty();
  4. }

代码示例来源:origin: resilience4j/resilience4j

  1. @Override
  2. public Response<T> execute() throws IOException {
  3. CheckedFunction0<Response<T>> restrictedSupplier = RateLimiter.decorateCheckedSupplier(rateLimiter, call::execute);
  4. final Try<Response<T>> response = Try.of(restrictedSupplier);
  5. return response.isSuccess() ? response.get() : handleFailure(response);
  6. }

代码示例来源:origin: vavr-io/vavr

  1. default Future<T> orElse(Future<? extends T> other) {
  2. Objects.requireNonNull(other, "other is null");
  3. return run(executor(), complete ->
  4. onComplete(result -> {
  5. if (result.isSuccess()) {
  6. complete.with(result);
  7. } else {
  8. other.onComplete(complete::with);
  9. }
  10. })
  11. );
  12. }

代码示例来源:origin: resilience4j/resilience4j

  1. @Test
  2. public void shouldInvokeRecoverFunction() {
  3. // tag::shouldInvokeRecoverFunction[]
  4. // Given
  5. CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("testName");
  6. // When I decorate my function and invoke the decorated function
  7. CheckedFunction0<String> checkedSupplier = CircuitBreaker.decorateCheckedSupplier(circuitBreaker, () -> {
  8. throw new RuntimeException("BAM!");
  9. });
  10. Try<String> result = Try.of(checkedSupplier)
  11. .recover(throwable -> "Hello Recovery");
  12. // Then the function should be a success, because the exception could be recovered
  13. assertThat(result.isSuccess()).isTrue();
  14. // and the result must match the result of the recovery function.
  15. assertThat(result.get()).isEqualTo("Hello Recovery");
  16. // end::shouldInvokeRecoverFunction[]
  17. }

代码示例来源:origin: resilience4j/resilience4j

  1. @Test
  2. public void decorateConsumer() throws Exception {
  3. Consumer<Integer> consumer = mock(Consumer.class);
  4. Consumer<Integer> decorated = RateLimiter.decorateConsumer(limit, consumer);
  5. when(limit.getPermission(config.getTimeoutDuration()))
  6. .thenReturn(false);
  7. Try<Integer> decoratedConsumerResult = Try.success(1).andThen(decorated);
  8. then(decoratedConsumerResult.isFailure()).isTrue();
  9. then(decoratedConsumerResult.getCause()).isInstanceOf(RequestNotPermitted.class);
  10. verify(consumer, never()).accept(any());
  11. when(limit.getPermission(config.getTimeoutDuration()))
  12. .thenReturn(true);
  13. Try secondConsumerResult = Try.success(1).andThen(decorated);
  14. then(secondConsumerResult.isSuccess()).isTrue();
  15. verify(consumer, times(1)).accept(1);
  16. }

代码示例来源:origin: resilience4j/resilience4j

  1. @Test
  2. public void decorateFunction() throws Exception {
  3. Function<Integer, String> function = mock(Function.class);
  4. Function<Integer, String> decorated = RateLimiter.decorateFunction(limit, function);
  5. when(limit.getPermission(config.getTimeoutDuration()))
  6. .thenReturn(false);
  7. Try<String> decoratedFunctionResult = Try.success(1).map(decorated);
  8. then(decoratedFunctionResult.isFailure()).isTrue();
  9. then(decoratedFunctionResult.getCause()).isInstanceOf(RequestNotPermitted.class);
  10. verify(function, never()).apply(any());
  11. when(limit.getPermission(config.getTimeoutDuration()))
  12. .thenReturn(true);
  13. Try secondFunctionResult = Try.success(1).map(decorated);
  14. then(secondFunctionResult.isSuccess()).isTrue();
  15. verify(function, times(1)).apply(1);
  16. }

代码示例来源:origin: resilience4j/resilience4j

  1. @Test
  2. public void shouldInvokeMap() {
  3. // tag::shouldInvokeMap[]
  4. // Given
  5. CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("testName");
  6. // When I decorate my function
  7. CheckedFunction0<String> decoratedSupplier = CircuitBreaker
  8. .decorateCheckedSupplier(circuitBreaker, () -> "This can be any method which returns: 'Hello");
  9. // and chain an other function with map
  10. Try<String> result = Try.of(decoratedSupplier)
  11. .map(value -> value + " world'");
  12. // Then the Try Monad returns a Success<String>, if all functions ran successfully.
  13. assertThat(result.isSuccess()).isTrue();
  14. assertThat(result.get()).isEqualTo("This can be any method which returns: 'Hello world'");
  15. // end::shouldInvokeMap[]
  16. }

代码示例来源:origin: resilience4j/resilience4j

  1. @Test
  2. public void shouldInvokeMap() {
  3. // tag::shouldInvokeMap[]
  4. // Given
  5. Bulkhead bulkhead = Bulkhead.of("testName", config);
  6. // When I decorate my function
  7. CheckedFunction0<String> decoratedSupplier = Bulkhead.decorateCheckedSupplier(bulkhead, () -> "This can be any method which returns: 'Hello");
  8. // and chain an other function with map
  9. Try<String> result = Try.of(decoratedSupplier)
  10. .map(value -> value + " world'");
  11. // Then the Try Monad returns a Success<String>, if all functions ran successfully.
  12. assertThat(result.isSuccess()).isTrue();
  13. assertThat(result.get()).isEqualTo("This can be any method which returns: 'Hello world'");
  14. assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
  15. // end::shouldInvokeMap[]
  16. }

代码示例来源:origin: resilience4j/resilience4j

  1. @Test
  2. public void decorateCheckedRunnable() throws Throwable {
  3. CheckedRunnable runnable = mock(CheckedRunnable.class);
  4. CheckedRunnable decorated = RateLimiter.decorateCheckedRunnable(limit, runnable);
  5. when(limit.getPermission(config.getTimeoutDuration()))
  6. .thenReturn(false);
  7. Try decoratedRunnableResult = Try.run(decorated);
  8. then(decoratedRunnableResult.isFailure()).isTrue();
  9. then(decoratedRunnableResult.getCause()).isInstanceOf(RequestNotPermitted.class);
  10. verify(runnable, never()).run();
  11. when(limit.getPermission(config.getTimeoutDuration()))
  12. .thenReturn(true);
  13. Try secondRunnableResult = Try.run(decorated);
  14. then(secondRunnableResult.isSuccess()).isTrue();
  15. verify(runnable, times(1)).run();
  16. }

代码示例来源:origin: resilience4j/resilience4j

  1. @Test
  2. public void decorateRunnable() throws Exception {
  3. Runnable runnable = mock(Runnable.class);
  4. Runnable decorated = RateLimiter.decorateRunnable(limit, runnable);
  5. when(limit.getPermission(config.getTimeoutDuration()))
  6. .thenReturn(false);
  7. Try decoratedRunnableResult = Try.success(decorated).andThen(Runnable::run);
  8. then(decoratedRunnableResult.isFailure()).isTrue();
  9. then(decoratedRunnableResult.getCause()).isInstanceOf(RequestNotPermitted.class);
  10. verify(runnable, never()).run();
  11. when(limit.getPermission(config.getTimeoutDuration()))
  12. .thenReturn(true);
  13. Try secondRunnableResult = Try.success(decorated).andThen(Runnable::run);
  14. then(secondRunnableResult.isSuccess()).isTrue();
  15. verify(runnable, times(1)).run();
  16. }

代码示例来源:origin: resilience4j/resilience4j

  1. @Test
  2. public void decorateSupplier() throws Exception {
  3. Supplier supplier = mock(Supplier.class);
  4. Supplier decorated = RateLimiter.decorateSupplier(limit, supplier);
  5. when(limit.getPermission(config.getTimeoutDuration()))
  6. .thenReturn(false);
  7. Try decoratedSupplierResult = Try.success(decorated).map(Supplier::get);
  8. then(decoratedSupplierResult.isFailure()).isTrue();
  9. then(decoratedSupplierResult.getCause()).isInstanceOf(RequestNotPermitted.class);
  10. verify(supplier, never()).get();
  11. when(limit.getPermission(config.getTimeoutDuration()))
  12. .thenReturn(true);
  13. Try secondSupplierResult = Try.success(decorated).map(Supplier::get);
  14. then(secondSupplierResult.isSuccess()).isTrue();
  15. verify(supplier, times(1)).get();
  16. }

代码示例来源:origin: resilience4j/resilience4j

  1. @Test
  2. public void decorateCheckedSupplier() throws Throwable {
  3. CheckedFunction0 supplier = mock(CheckedFunction0.class);
  4. CheckedFunction0 decorated = RateLimiter.decorateCheckedSupplier(limit, supplier);
  5. when(limit.getPermission(config.getTimeoutDuration()))
  6. .thenReturn(false);
  7. Try decoratedSupplierResult = Try.of(decorated);
  8. then(decoratedSupplierResult.isFailure()).isTrue();
  9. then(decoratedSupplierResult.getCause()).isInstanceOf(RequestNotPermitted.class);
  10. verify(supplier, never()).apply();
  11. when(limit.getPermission(config.getTimeoutDuration()))
  12. .thenReturn(true);
  13. Try secondSupplierResult = Try.of(decorated);
  14. then(secondSupplierResult.isSuccess()).isTrue();
  15. verify(supplier, times(1)).apply();
  16. }

代码示例来源:origin: vavr-io/vavr

  1. /**
  2. * Maps the cause to a new exception if this is a {@code Failure} or returns this instance if this is a {@code Success}.
  3. * <p>
  4. * If none of the given cases matches the cause, the same {@code Failure} is returned.
  5. *
  6. * @param cases A not necessarily exhaustive sequence of cases that will be matched against a cause.
  7. * @return A new {@code Try} if this is a {@code Failure}, otherwise this.
  8. */
  9. @GwtIncompatible
  10. @SuppressWarnings({ "unchecked", "varargs" })
  11. default Try<T> mapFailure(Match.Case<? extends Throwable, ? extends Throwable>... cases) {
  12. if (isSuccess()) {
  13. return this;
  14. } else {
  15. final Option<Throwable> x = Match(getCause()).option(cases);
  16. return x.isEmpty() ? this : failure(x.get());
  17. }
  18. }

代码示例来源:origin: resilience4j/resilience4j

  1. @Test
  2. public void decorateCheckedFunction() throws Throwable {
  3. CheckedFunction1<Integer, String> function = mock(CheckedFunction1.class);
  4. CheckedFunction1<Integer, String> decorated = RateLimiter.decorateCheckedFunction(limit, function);
  5. when(limit.getPermission(config.getTimeoutDuration()))
  6. .thenReturn(false);
  7. Try<String> decoratedFunctionResult = Try.success(1).mapTry(decorated);
  8. then(decoratedFunctionResult.isFailure()).isTrue();
  9. then(decoratedFunctionResult.getCause()).isInstanceOf(RequestNotPermitted.class);
  10. verify(function, never()).apply(any());
  11. when(limit.getPermission(config.getTimeoutDuration()))
  12. .thenReturn(true);
  13. Try secondFunctionResult = Try.success(1).mapTry(decorated);
  14. then(secondFunctionResult.isSuccess()).isTrue();
  15. verify(function, times(1)).apply(1);
  16. }

相关文章