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

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

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

Try.recover介绍

[英]Returns this, if this is a Try.Success or this is a Failure and the cause is not assignable from cause.getClass(). Otherwise returns a Try.Success containing the given value.

  1. // = Success(13)

[中]如果这是一次尝试,则返回此。成功或失败,原因无法从原因中确定。getClass()。否则返回一次尝试。包含给定值的成功

  1. // = Success(13)

代码示例

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

  1. /**
  2. * Handles a failure of this Future by returning another result.
  3. * <p>
  4. * Example:
  5. * <pre><code>
  6. * // = "oh!"
  7. * Future.of(() -&gt; new Error("oh!")).recover(Throwable::getMessage);
  8. * </code></pre>
  9. *
  10. * @param f A function which takes the exception of a failure and returns a new value.
  11. * @return A new Future.
  12. * @throws NullPointerException if {@code f} is null
  13. */
  14. default Future<T> recover(Function<? super Throwable, ? extends T> f) {
  15. Objects.requireNonNull(f, "f is null");
  16. return transformValue(t -> t.recover(f));
  17. }

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

  1. final Gen<T1> gen1 = Try.of(() -> a1.apply(size)).recover(x -> { throw arbitraryError(1, size, x); }).get();
  2. final Gen<T2> gen2 = Try.of(() -> a2.apply(size)).recover(x -> { throw arbitraryError(2, size, x); }).get();
  3. final Gen<T3> gen3 = Try.of(() -> a3.apply(size)).recover(x -> { throw arbitraryError(3, size, x); }).get();
  4. final Gen<T4> gen4 = Try.of(() -> a4.apply(size)).recover(x -> { throw arbitraryError(4, size, x); }).get();
  5. final Gen<T5> gen5 = Try.of(() -> a5.apply(size)).recover(x -> { throw arbitraryError(5, size, x); }).get();
  6. final Gen<T6> gen6 = Try.of(() -> a6.apply(size)).recover(x -> { throw arbitraryError(6, size, x); }).get();
  7. boolean exhausted = true;
  8. for (int i = 1; i <= tries; i++) {
  9. try {
  10. final T1 val1 = Try.of(() -> gen1.apply(random)).recover(x -> { throw genError(1, size, x); }).get();
  11. final T2 val2 = Try.of(() -> gen2.apply(random)).recover(x -> { throw genError(2, size, x); }).get();
  12. final T3 val3 = Try.of(() -> gen3.apply(random)).recover(x -> { throw genError(3, size, x); }).get();
  13. final T4 val4 = Try.of(() -> gen4.apply(random)).recover(x -> { throw genError(4, size, x); }).get();
  14. final T5 val5 = Try.of(() -> gen5.apply(random)).recover(x -> { throw genError(5, size, x); }).get();
  15. final T6 val6 = Try.of(() -> gen6.apply(random)).recover(x -> { throw genError(6, size, x); }).get();
  16. try {
  17. final Condition condition = Try.of(() -> predicate.apply(val1, val2, val3, val4, val5, val6)).recover(x -> { throw predicateError(x); }).get();
  18. if (condition.precondition) {
  19. exhausted = false;

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

  1. final Gen<T1> gen1 = Try.of(() -> a1.apply(size)).recover(x -> { throw arbitraryError(1, size, x); }).get();
  2. final Gen<T2> gen2 = Try.of(() -> a2.apply(size)).recover(x -> { throw arbitraryError(2, size, x); }).get();
  3. final Gen<T3> gen3 = Try.of(() -> a3.apply(size)).recover(x -> { throw arbitraryError(3, size, x); }).get();
  4. final Gen<T4> gen4 = Try.of(() -> a4.apply(size)).recover(x -> { throw arbitraryError(4, size, x); }).get();
  5. final Gen<T5> gen5 = Try.of(() -> a5.apply(size)).recover(x -> { throw arbitraryError(5, size, x); }).get();
  6. final Gen<T6> gen6 = Try.of(() -> a6.apply(size)).recover(x -> { throw arbitraryError(6, size, x); }).get();
  7. final Gen<T7> gen7 = Try.of(() -> a7.apply(size)).recover(x -> { throw arbitraryError(7, size, x); }).get();
  8. boolean exhausted = true;
  9. for (int i = 1; i <= tries; i++) {
  10. try {
  11. final T1 val1 = Try.of(() -> gen1.apply(random)).recover(x -> { throw genError(1, size, x); }).get();
  12. final T2 val2 = Try.of(() -> gen2.apply(random)).recover(x -> { throw genError(2, size, x); }).get();
  13. final T3 val3 = Try.of(() -> gen3.apply(random)).recover(x -> { throw genError(3, size, x); }).get();
  14. final T4 val4 = Try.of(() -> gen4.apply(random)).recover(x -> { throw genError(4, size, x); }).get();
  15. final T5 val5 = Try.of(() -> gen5.apply(random)).recover(x -> { throw genError(5, size, x); }).get();
  16. final T6 val6 = Try.of(() -> gen6.apply(random)).recover(x -> { throw genError(6, size, x); }).get();
  17. final T7 val7 = Try.of(() -> gen7.apply(random)).recover(x -> { throw genError(7, size, x); }).get();
  18. try {
  19. final Condition condition = Try.of(() -> predicate.apply(val1, val2, val3, val4, val5, val6, val7)).recover(x -> { throw predicateError(x); }).get();
  20. if (condition.precondition) {
  21. exhausted = false;

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

  1. final Gen<T1> gen1 = Try.of(() -> a1.apply(size)).recover(x -> { throw arbitraryError(1, size, x); }).get();
  2. final Gen<T2> gen2 = Try.of(() -> a2.apply(size)).recover(x -> { throw arbitraryError(2, size, x); }).get();
  3. final Gen<T3> gen3 = Try.of(() -> a3.apply(size)).recover(x -> { throw arbitraryError(3, size, x); }).get();
  4. final Gen<T4> gen4 = Try.of(() -> a4.apply(size)).recover(x -> { throw arbitraryError(4, size, x); }).get();
  5. boolean exhausted = true;
  6. for (int i = 1; i <= tries; i++) {
  7. try {
  8. final T1 val1 = Try.of(() -> gen1.apply(random)).recover(x -> { throw genError(1, size, x); }).get();
  9. final T2 val2 = Try.of(() -> gen2.apply(random)).recover(x -> { throw genError(2, size, x); }).get();
  10. final T3 val3 = Try.of(() -> gen3.apply(random)).recover(x -> { throw genError(3, size, x); }).get();
  11. final T4 val4 = Try.of(() -> gen4.apply(random)).recover(x -> { throw genError(4, size, x); }).get();
  12. try {
  13. final Condition condition = Try.of(() -> predicate.apply(val1, val2, val3, val4)).recover(x -> { throw predicateError(x); }).get();
  14. if (condition.precondition) {
  15. exhausted = false;

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

  1. final Gen<T1> gen1 = Try.of(() -> a1.apply(size)).recover(x -> { throw arbitraryError(1, size, x); }).get();
  2. final Gen<T2> gen2 = Try.of(() -> a2.apply(size)).recover(x -> { throw arbitraryError(2, size, x); }).get();
  3. boolean exhausted = true;
  4. for (int i = 1; i <= tries; i++) {
  5. try {
  6. final T1 val1 = Try.of(() -> gen1.apply(random)).recover(x -> { throw genError(1, size, x); }).get();
  7. final T2 val2 = Try.of(() -> gen2.apply(random)).recover(x -> { throw genError(2, size, x); }).get();
  8. try {
  9. final Condition condition = Try.of(() -> predicate.apply(val1, val2)).recover(x -> { throw predicateError(x); }).get();
  10. if (condition.precondition) {
  11. exhausted = false;

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

  1. final Gen<T1> gen1 = Try.of(() -> a1.apply(size)).recover(x -> { throw arbitraryError(1, size, x); }).get();
  2. final Gen<T2> gen2 = Try.of(() -> a2.apply(size)).recover(x -> { throw arbitraryError(2, size, x); }).get();
  3. final Gen<T3> gen3 = Try.of(() -> a3.apply(size)).recover(x -> { throw arbitraryError(3, size, x); }).get();
  4. final Gen<T4> gen4 = Try.of(() -> a4.apply(size)).recover(x -> { throw arbitraryError(4, size, x); }).get();
  5. final Gen<T5> gen5 = Try.of(() -> a5.apply(size)).recover(x -> { throw arbitraryError(5, size, x); }).get();
  6. boolean exhausted = true;
  7. for (int i = 1; i <= tries; i++) {
  8. try {
  9. final T1 val1 = Try.of(() -> gen1.apply(random)).recover(x -> { throw genError(1, size, x); }).get();
  10. final T2 val2 = Try.of(() -> gen2.apply(random)).recover(x -> { throw genError(2, size, x); }).get();
  11. final T3 val3 = Try.of(() -> gen3.apply(random)).recover(x -> { throw genError(3, size, x); }).get();
  12. final T4 val4 = Try.of(() -> gen4.apply(random)).recover(x -> { throw genError(4, size, x); }).get();
  13. final T5 val5 = Try.of(() -> gen5.apply(random)).recover(x -> { throw genError(5, size, x); }).get();
  14. try {
  15. final Condition condition = Try.of(() -> predicate.apply(val1, val2, val3, val4, val5)).recover(x -> { throw predicateError(x); }).get();
  16. if (condition.precondition) {
  17. exhausted = false;

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

  1. final Gen<T1> gen1 = Try.of(() -> a1.apply(size)).recover(x -> { throw arbitraryError(1, size, x); }).get();
  2. final Gen<T2> gen2 = Try.of(() -> a2.apply(size)).recover(x -> { throw arbitraryError(2, size, x); }).get();
  3. final Gen<T3> gen3 = Try.of(() -> a3.apply(size)).recover(x -> { throw arbitraryError(3, size, x); }).get();
  4. boolean exhausted = true;
  5. for (int i = 1; i <= tries; i++) {
  6. try {
  7. final T1 val1 = Try.of(() -> gen1.apply(random)).recover(x -> { throw genError(1, size, x); }).get();
  8. final T2 val2 = Try.of(() -> gen2.apply(random)).recover(x -> { throw genError(2, size, x); }).get();
  9. final T3 val3 = Try.of(() -> gen3.apply(random)).recover(x -> { throw genError(3, size, x); }).get();
  10. try {
  11. final Condition condition = Try.of(() -> predicate.apply(val1, val2, val3)).recover(x -> { throw predicateError(x); }).get();
  12. if (condition.precondition) {
  13. exhausted = false;

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

  1. final Gen<T1> gen1 = Try.of(() -> a1.apply(size)).recover(x -> { throw arbitraryError(1, size, x); }).get();
  2. final Gen<T2> gen2 = Try.of(() -> a2.apply(size)).recover(x -> { throw arbitraryError(2, size, x); }).get();
  3. final Gen<T3> gen3 = Try.of(() -> a3.apply(size)).recover(x -> { throw arbitraryError(3, size, x); }).get();
  4. final Gen<T4> gen4 = Try.of(() -> a4.apply(size)).recover(x -> { throw arbitraryError(4, size, x); }).get();
  5. final Gen<T5> gen5 = Try.of(() -> a5.apply(size)).recover(x -> { throw arbitraryError(5, size, x); }).get();
  6. final Gen<T6> gen6 = Try.of(() -> a6.apply(size)).recover(x -> { throw arbitraryError(6, size, x); }).get();
  7. final Gen<T7> gen7 = Try.of(() -> a7.apply(size)).recover(x -> { throw arbitraryError(7, size, x); }).get();
  8. final Gen<T8> gen8 = Try.of(() -> a8.apply(size)).recover(x -> { throw arbitraryError(8, size, x); }).get();
  9. boolean exhausted = true;
  10. for (int i = 1; i <= tries; i++) {
  11. try {
  12. final T1 val1 = Try.of(() -> gen1.apply(random)).recover(x -> { throw genError(1, size, x); }).get();
  13. final T2 val2 = Try.of(() -> gen2.apply(random)).recover(x -> { throw genError(2, size, x); }).get();
  14. final T3 val3 = Try.of(() -> gen3.apply(random)).recover(x -> { throw genError(3, size, x); }).get();
  15. final T4 val4 = Try.of(() -> gen4.apply(random)).recover(x -> { throw genError(4, size, x); }).get();
  16. final T5 val5 = Try.of(() -> gen5.apply(random)).recover(x -> { throw genError(5, size, x); }).get();
  17. final T6 val6 = Try.of(() -> gen6.apply(random)).recover(x -> { throw genError(6, size, x); }).get();
  18. final T7 val7 = Try.of(() -> gen7.apply(random)).recover(x -> { throw genError(7, size, x); }).get();
  19. final T8 val8 = Try.of(() -> gen8.apply(random)).recover(x -> { throw genError(8, size, x); }).get();
  20. try {
  21. final Condition condition = Try.of(() -> predicate.apply(val1, val2, val3, val4, val5, val6, val7, val8)).recover(x -> { throw predicateError(x); }).get();
  22. if (condition.precondition) {
  23. exhausted = false;

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

  1. final Gen<T1> gen1 = Try.of(() -> a1.apply(size)).recover(x -> { throw arbitraryError(1, size, x); }).get();
  2. boolean exhausted = true;
  3. for (int i = 1; i <= tries; i++) {
  4. try {
  5. final T1 val1 = Try.of(() -> gen1.apply(random)).recover(x -> { throw genError(1, size, x); }).get();
  6. try {
  7. final Condition condition = Try.of(() -> predicate.apply(val1)).recover(x -> { throw predicateError(x); }).get();
  8. if (condition.precondition) {
  9. exhausted = false;

代码示例来源: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 shouldReturnAfterThreeAttemptsAndRecover() {
  3. // Given the HelloWorldService throws an exception
  4. BDDMockito.given(helloWorldService.returnHelloWorld()).willThrow(new WebServiceException("BAM!"));
  5. // Create a Retry with default configuration
  6. Retry retry = Retry.ofDefaults("id");
  7. // Decorate the invocation of the HelloWorldService
  8. CheckedFunction0<String> retryableSupplier = Retry
  9. .decorateCheckedSupplier(retry, helloWorldService::returnHelloWorld);
  10. // When
  11. Try<String> result = Try.of(retryableSupplier).recover((throwable) -> "Hello world from recovery function");
  12. assertThat(retry.getMetrics().getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(1);
  13. // Then the helloWorldService should be invoked 3 times
  14. BDDMockito.then(helloWorldService).should(Mockito.times(3)).returnHelloWorld();
  15. // and the returned exception should be of type RuntimeException
  16. assertThat(result.get()).isEqualTo("Hello world from recovery function");
  17. assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION * 2);
  18. }

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

  1. @Test
  2. public void shouldReturnAfterThreeAttemptsAndRecoverWithResult() {
  3. // Given the HelloWorldService throws an exception
  4. BDDMockito.given(helloWorldService.returnHelloWorld()).willThrow(new WebServiceException("BAM!"))
  5. .willReturn("Hello world")
  6. .willThrow(new WebServiceException("BAM!"));
  7. // Create a Retry with default configuration
  8. final RetryConfig tryAgain = RetryConfig.<String>custom().retryOnResult(s -> s.contains("Hello world"))
  9. .maxAttempts(3).build();
  10. Retry retry = Retry.of("id", tryAgain);
  11. // Decorate the invocation of the HelloWorldService
  12. CheckedFunction0<String> retryableSupplier = Retry
  13. .decorateCheckedSupplier(retry, helloWorldService::returnHelloWorld);
  14. // When
  15. Try<String> result = Try.of(retryableSupplier).recover((throwable) -> "Hello world from recovery function");
  16. // Then the helloWorldService should be invoked 3 times
  17. BDDMockito.then(helloWorldService).should(Mockito.times(3)).returnHelloWorld();
  18. // and the returned exception should be of type RuntimeException
  19. assertThat(result.get()).isEqualTo("Hello world from recovery function");
  20. assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION * 2);
  21. }

代码示例来源:origin: com.github.robozonky/robozonky-common

  1. private static <O> Either<Throwable, O> execute(final Supplier<O> operation) {
  2. LOGGER.trace("Will execute {}.", operation);
  3. return Try.ofSupplier(operation).map(Either::<Throwable, O>right)
  4. .recover(t -> {
  5. LOGGER.debug("Operation failed.", t);
  6. return Either.left(t);
  7. }).get();
  8. }

代码示例来源:origin: RoboZonky/robozonky

  1. private static <O> Either<Throwable, O> execute(final Supplier<O> operation) {
  2. LOGGER.trace("Will execute {}.", operation);
  3. return Try.ofSupplier(operation).map(Either::<Throwable, O>right)
  4. .recover(t -> {
  5. LOGGER.debug("Operation failed.", t);
  6. return Either.left(t);
  7. }).get();
  8. }

代码示例来源:origin: spring-cloud-incubator/spring-cloud-circuitbreaker

  1. @Override
  2. public <T> T run(Supplier<T> toRun, Function<Throwable, T> fallback) {
  3. TimeLimiter timeLimiter = TimeLimiter.of(timeLimiterConfig);
  4. Supplier<Future<T>> futureSupplier = () -> executorService.submit(toRun::get);
  5. Callable restrictedCall = TimeLimiter
  6. .decorateFutureSupplier(timeLimiter, futureSupplier);
  7. io.github.resilience4j.circuitbreaker.CircuitBreaker defaultCircuitBreaker = registry.circuitBreaker(id, circuitBreakerConfig);
  8. Callable<T> callable = io.github.resilience4j.circuitbreaker.CircuitBreaker
  9. .decorateCallable(defaultCircuitBreaker, restrictedCall);
  10. return Try.of(callable::call).recover(fallback).get();
  11. }
  12. }

代码示例来源:origin: Tristan971/Lyrebird

  1. private void createTabForPal(final User user) {
  2. if (loadedPals.contains(user)) return;
  3. LOG.debug("Creating a conversation tab for conversation with {}", user.getScreenName());
  4. easyFxml.loadNode(DIRECT_MESSAGE_CONVERSATION, Pane.class, DMConversationController.class)
  5. .afterControllerLoaded(dmc -> dmc.setPal(user))
  6. .getNode()
  7. .recover(ExceptionHandler::fromThrowable)
  8. .map(conversation -> new Tab(user.getName(), conversation))
  9. .onSuccess(tab -> Platform.runLater(() -> {
  10. LOG.debug("Adding [{}] as tab for user {}", tab.getText(), user.getScreenName());
  11. this.conversationsManaged.add(tab);
  12. }));
  13. loadedPals.add(user);
  14. }

代码示例来源:origin: Tristan971/Lyrebird

  1. /**
  2. * If this tweet is in reply to another, we set proper display of the previous one because it looks good.
  3. *
  4. * @param repliedTweet the tweet ({@link Status} to which this is in reply to
  5. */
  6. public void setInReplyToTweet(final Status repliedTweet) {
  7. LOG.debug("Set new tweet stage to embed status : {}", repliedTweet.getId());
  8. inReplyStatus.setValue(repliedTweet);
  9. easyFxml.loadNode(FxComponent.TWEET, Pane.class, TweetPaneController.class)
  10. .afterControllerLoaded(tpc -> {
  11. tpc.embeddedPropertyProperty().setValue(true);
  12. tpc.updateWithValue(repliedTweet);
  13. })
  14. .getNode()
  15. .recover(ExceptionHandler::fromThrowable)
  16. .onSuccess(this.container::setTop);
  17. }

代码示例来源:origin: Tristan971/Lyrebird

  1. /**
  2. * Loads the target user's timeline into the view.
  3. */
  4. private void loadTargetUserTimeline() {
  5. final User user = targetUserProp.getValue();
  6. easyFxml.loadNode(FxComponent.USER_TIMELINE, Pane.class, UserTimelineController.class)
  7. .afterControllerLoaded(utc -> utc.setTargetUser(user))
  8. .afterNodeLoaded(userDetailsTimeline -> VBox.setVgrow(userDetailsTimeline, Priority.ALWAYS))
  9. .getNode()
  10. .recover(ExceptionHandler::fromThrowable)
  11. .onSuccess(container.getChildren()::add);
  12. }

代码示例来源:origin: Tristan971/Lyrebird

  1. /**
  2. * Pre-formats a status' text content.
  3. *
  4. * @param status The status whose content we have to format
  5. */
  6. private void loadTextIntoTextFlow(final Status status) {
  7. tweetContent.getChildren().clear();
  8. final FxmlLoadResult<Pane, TweetContentPaneController> result = easyFxml.loadNode(
  9. FxComponent.TWEET_CONTENT_PANE,
  10. Pane.class,
  11. TweetContentPaneController.class
  12. );
  13. result.afterControllerLoaded(tcpc -> tcpc.setStatusProp(status))
  14. .getNode()
  15. .peek(pane -> VBox.setVgrow(pane, Priority.ALWAYS))
  16. .recover(ExceptionHandler::fromThrowable)
  17. .andThen(tweetContent.getChildren()::add);
  18. }

代码示例来源:origin: Tristan971/Lyrebird

  1. private void setUpInteractionButtons() {
  2. interactionBox.visibleProperty().bind(embeddedProperty.not());
  3. interactionBox.managedProperty().bind(embeddedProperty.not());
  4. easyFxml.loadNode(TWEET_INTERACTION_BOX, Pane.class, TweetInteractionPaneController.class)
  5. .afterControllerLoaded(tipc -> tipc.targetStatusProperty().bind(currentStatus))
  6. .getNode()
  7. .recover(ExceptionHandler::fromThrowable)
  8. .onSuccess(interactionBox::setCenter);
  9. }

相关文章