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

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

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

Try.getCause介绍

[英]Gets the cause if this is a Failure or throws if this is a Success.
[中]如果这是失败的,获取原因;如果这是成功的,则抛出。

代码示例

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

  1. default void orElseRun(Consumer<? super Throwable> action) {
  2. Objects.requireNonNull(action, "action is null");
  3. if (isFailure()) {
  4. action.accept(getCause());
  5. }
  6. }

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

  1. private Response<T> handleFailure(Try<Response<T>> response) throws IOException {
  2. try {
  3. throw response.getCause();
  4. } catch (RequestNotPermitted | IllegalStateException e) {
  5. return tooManyRequestsError();
  6. } catch (IOException ioe) {
  7. throw ioe;
  8. } catch (Throwable t) {
  9. throw new RuntimeException("Exception executing call", t);
  10. }
  11. }

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

  1. default <X extends Throwable> T getOrElseThrow(Function<? super Throwable, X> exceptionProvider) throws X {
  2. Objects.requireNonNull(exceptionProvider, "exceptionProvider is null");
  3. if (isFailure()) {
  4. throw exceptionProvider.apply(getCause());
  5. } else {
  6. return get();
  7. }
  8. }

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

  1. default T getOrElseGet(Function<? super Throwable, ? extends T> other) {
  2. Objects.requireNonNull(other, "other is null");
  3. if (isFailure()) {
  4. return other.apply(getCause());
  5. } else {
  6. return get();
  7. }
  8. }

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

  1. /**
  2. * Folds either the {@code Failure} or the {@code Success} side of the Try value.
  3. *
  4. * @param ifFail maps the left value if this is a {@code Failure}
  5. * @param f maps the value if this is a {@code Success}
  6. * @param <X> type of the folded value
  7. * @return A value of type X
  8. */
  9. default <X> X fold(Function<? super Throwable, ? extends X> ifFail, Function<? super T, ? extends X> f) {
  10. if (isFailure()) {
  11. return ifFail.apply(getCause());
  12. } else {
  13. return f.apply(get());
  14. }
  15. }

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

  1. /**
  2. * Returns {@code Success(throwable)} if this is a {@code Failure(throwable)}, otherwise
  3. * a {@code Failure(new NoSuchElementException("Success.failed()"))} if this is a Success.
  4. *
  5. * @return a new Try
  6. */
  7. default Try<Throwable> failed() {
  8. if (isFailure()) {
  9. return new Success<>(getCause());
  10. } else {
  11. return new Failure<>(new NoSuchElementException("Success.failed()"));
  12. }
  13. }

代码示例来源: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. * Converts this {@code Try} to a {@link Validation}, converting the Throwable (if present)
  3. * to another object using passed {@link Function}.
  4. *
  5. * <pre>{@code
  6. * Validation<String, Integer> = Try.of(() -> 1/0).toValidation(Throwable::getMessage));
  7. * }</pre>
  8. *
  9. * @param <U> result type of the throwable mapper
  10. * @param throwableMapper A transformation from throwable to desired invalid type of new {@code Validation}
  11. * @return A new {@code Validation}
  12. * @throws NullPointerException if the given {@code throwableMapper} is null.
  13. */
  14. default <U> Validation<U, T> toValidation(Function<? super Throwable, ? extends U> throwableMapper) {
  15. Objects.requireNonNull(throwableMapper, "throwableMapper is null");
  16. if (isFailure()) {
  17. return Validation.invalid(throwableMapper.apply(getCause()));
  18. } else {
  19. return Validation.valid(get());
  20. }
  21. }

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

  1. /**
  2. * Converts this {@code Try} to an {@link Either}.
  3. *
  4. * @return A new {@code Either}
  5. */
  6. default Either<Throwable, T> toEither() {
  7. if (isFailure()) {
  8. return Either.left(getCause());
  9. } else {
  10. return Either.right(get());
  11. }
  12. }

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

  1. /**
  2. * Reduces many {@code Try}s into a single {@code Try} by transforming an
  3. * {@code Iterable<Try<? extends T>>} into a {@code Try<Seq<T>>}. If any of
  4. * the {@code Try}s are {@link Try.Failure}, then this returns a {@link Try.Failure}.
  5. *
  6. * @param values An {@link Iterable} of {@code Try}s
  7. * @param <T> type of the Trys
  8. * @return A {@code Try} of a {@link Seq} of results
  9. * @throws NullPointerException if {@code values} is null
  10. */
  11. static <T> Try<Seq<T>> sequence(Iterable<? extends Try<? extends T>> values) {
  12. Objects.requireNonNull(values, "values is null");
  13. Vector<T> vector = Vector.empty();
  14. for (Try<? extends T> value : values) {
  15. if (value.isFailure()) {
  16. return Try.failure(value.getCause());
  17. }
  18. vector = vector.append(value.get());
  19. }
  20. return Try.success(vector);
  21. }

代码示例来源: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: vavr-io/vavr

  1. /**
  2. * A projection that inverses the result of this Future.
  3. * <p>
  4. * If this Future succeeds, the failed projection returns a failure containing a {@code NoSuchElementException}.
  5. * <p>
  6. * If this Future fails, the failed projection returns a success containing the exception.
  7. *
  8. * @return A new Future which contains an exception at a point of time.
  9. */
  10. default Future<Throwable> failed() {
  11. return run(executor(), complete ->
  12. onComplete(result -> {
  13. if (result.isFailure()) {
  14. complete.with(Try.success(result.getCause()));
  15. } else {
  16. complete.with(Try.failure(new NoSuchElementException("Future.failed completed without a throwable")));
  17. }
  18. })
  19. );
  20. }

代码示例来源: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 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: 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 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: 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. }

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

  1. @Test
  2. public void executeFutureSupplier() throws Throwable {
  3. Future<Integer> future = EXECUTOR_SERVICE.submit(() -> {
  4. Thread.sleep(SLEEP_DURATION.toMillis());
  5. return 1;
  6. }
  7. );
  8. Supplier<Future<Integer>> supplier = () -> future;
  9. Try decoratedResult = Try.of(() -> TimeLimiter.of(shortConfig).executeFutureSupplier(supplier));
  10. then(decoratedResult.isFailure()).isTrue();
  11. then(decoratedResult.getCause()).isInstanceOf(TimeoutException.class);
  12. then(future.isCancelled()).isTrue();
  13. Future<Integer> secondFuture = EXECUTOR_SERVICE.submit(() -> {
  14. Thread.sleep(SLEEP_DURATION.toMillis());
  15. return 1;
  16. }
  17. );
  18. Supplier<Future<Integer>> secondSupplier = () -> secondFuture;
  19. Try secondResult = Try.of(() -> TimeLimiter.of(longConfig).executeFutureSupplier(secondSupplier));
  20. then(secondResult.isSuccess()).isTrue();
  21. }
  22. }

相关文章