本文整理了Java中io.vavr.control.Try.recover()
方法的一些代码示例,展示了Try.recover()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Try.recover()
方法的具体详情如下:
包路径:io.vavr.control.Try
类名称: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.
// = Success(13)
[中]如果这是一次尝试,则返回此。成功或失败,原因无法从原因中确定。getClass()。否则返回一次尝试。包含给定值的成功
// = Success(13)
代码示例来源:origin: vavr-io/vavr
/**
* Handles a failure of this Future by returning another result.
* <p>
* Example:
* <pre><code>
* // = "oh!"
* Future.of(() -> new Error("oh!")).recover(Throwable::getMessage);
* </code></pre>
*
* @param f A function which takes the exception of a failure and returns a new value.
* @return A new Future.
* @throws NullPointerException if {@code f} is null
*/
default Future<T> recover(Function<? super Throwable, ? extends T> f) {
Objects.requireNonNull(f, "f is null");
return transformValue(t -> t.recover(f));
}
代码示例来源:origin: vavr-io/vavr
final Gen<T1> gen1 = Try.of(() -> a1.apply(size)).recover(x -> { throw arbitraryError(1, size, x); }).get();
final Gen<T2> gen2 = Try.of(() -> a2.apply(size)).recover(x -> { throw arbitraryError(2, size, x); }).get();
final Gen<T3> gen3 = Try.of(() -> a3.apply(size)).recover(x -> { throw arbitraryError(3, size, x); }).get();
final Gen<T4> gen4 = Try.of(() -> a4.apply(size)).recover(x -> { throw arbitraryError(4, size, x); }).get();
final Gen<T5> gen5 = Try.of(() -> a5.apply(size)).recover(x -> { throw arbitraryError(5, size, x); }).get();
final Gen<T6> gen6 = Try.of(() -> a6.apply(size)).recover(x -> { throw arbitraryError(6, size, x); }).get();
boolean exhausted = true;
for (int i = 1; i <= tries; i++) {
try {
final T1 val1 = Try.of(() -> gen1.apply(random)).recover(x -> { throw genError(1, size, x); }).get();
final T2 val2 = Try.of(() -> gen2.apply(random)).recover(x -> { throw genError(2, size, x); }).get();
final T3 val3 = Try.of(() -> gen3.apply(random)).recover(x -> { throw genError(3, size, x); }).get();
final T4 val4 = Try.of(() -> gen4.apply(random)).recover(x -> { throw genError(4, size, x); }).get();
final T5 val5 = Try.of(() -> gen5.apply(random)).recover(x -> { throw genError(5, size, x); }).get();
final T6 val6 = Try.of(() -> gen6.apply(random)).recover(x -> { throw genError(6, size, x); }).get();
try {
final Condition condition = Try.of(() -> predicate.apply(val1, val2, val3, val4, val5, val6)).recover(x -> { throw predicateError(x); }).get();
if (condition.precondition) {
exhausted = false;
代码示例来源:origin: vavr-io/vavr
final Gen<T1> gen1 = Try.of(() -> a1.apply(size)).recover(x -> { throw arbitraryError(1, size, x); }).get();
final Gen<T2> gen2 = Try.of(() -> a2.apply(size)).recover(x -> { throw arbitraryError(2, size, x); }).get();
final Gen<T3> gen3 = Try.of(() -> a3.apply(size)).recover(x -> { throw arbitraryError(3, size, x); }).get();
final Gen<T4> gen4 = Try.of(() -> a4.apply(size)).recover(x -> { throw arbitraryError(4, size, x); }).get();
final Gen<T5> gen5 = Try.of(() -> a5.apply(size)).recover(x -> { throw arbitraryError(5, size, x); }).get();
final Gen<T6> gen6 = Try.of(() -> a6.apply(size)).recover(x -> { throw arbitraryError(6, size, x); }).get();
final Gen<T7> gen7 = Try.of(() -> a7.apply(size)).recover(x -> { throw arbitraryError(7, size, x); }).get();
boolean exhausted = true;
for (int i = 1; i <= tries; i++) {
try {
final T1 val1 = Try.of(() -> gen1.apply(random)).recover(x -> { throw genError(1, size, x); }).get();
final T2 val2 = Try.of(() -> gen2.apply(random)).recover(x -> { throw genError(2, size, x); }).get();
final T3 val3 = Try.of(() -> gen3.apply(random)).recover(x -> { throw genError(3, size, x); }).get();
final T4 val4 = Try.of(() -> gen4.apply(random)).recover(x -> { throw genError(4, size, x); }).get();
final T5 val5 = Try.of(() -> gen5.apply(random)).recover(x -> { throw genError(5, size, x); }).get();
final T6 val6 = Try.of(() -> gen6.apply(random)).recover(x -> { throw genError(6, size, x); }).get();
final T7 val7 = Try.of(() -> gen7.apply(random)).recover(x -> { throw genError(7, size, x); }).get();
try {
final Condition condition = Try.of(() -> predicate.apply(val1, val2, val3, val4, val5, val6, val7)).recover(x -> { throw predicateError(x); }).get();
if (condition.precondition) {
exhausted = false;
代码示例来源:origin: vavr-io/vavr
final Gen<T1> gen1 = Try.of(() -> a1.apply(size)).recover(x -> { throw arbitraryError(1, size, x); }).get();
final Gen<T2> gen2 = Try.of(() -> a2.apply(size)).recover(x -> { throw arbitraryError(2, size, x); }).get();
final Gen<T3> gen3 = Try.of(() -> a3.apply(size)).recover(x -> { throw arbitraryError(3, size, x); }).get();
final Gen<T4> gen4 = Try.of(() -> a4.apply(size)).recover(x -> { throw arbitraryError(4, size, x); }).get();
boolean exhausted = true;
for (int i = 1; i <= tries; i++) {
try {
final T1 val1 = Try.of(() -> gen1.apply(random)).recover(x -> { throw genError(1, size, x); }).get();
final T2 val2 = Try.of(() -> gen2.apply(random)).recover(x -> { throw genError(2, size, x); }).get();
final T3 val3 = Try.of(() -> gen3.apply(random)).recover(x -> { throw genError(3, size, x); }).get();
final T4 val4 = Try.of(() -> gen4.apply(random)).recover(x -> { throw genError(4, size, x); }).get();
try {
final Condition condition = Try.of(() -> predicate.apply(val1, val2, val3, val4)).recover(x -> { throw predicateError(x); }).get();
if (condition.precondition) {
exhausted = false;
代码示例来源:origin: vavr-io/vavr
final Gen<T1> gen1 = Try.of(() -> a1.apply(size)).recover(x -> { throw arbitraryError(1, size, x); }).get();
final Gen<T2> gen2 = Try.of(() -> a2.apply(size)).recover(x -> { throw arbitraryError(2, size, x); }).get();
boolean exhausted = true;
for (int i = 1; i <= tries; i++) {
try {
final T1 val1 = Try.of(() -> gen1.apply(random)).recover(x -> { throw genError(1, size, x); }).get();
final T2 val2 = Try.of(() -> gen2.apply(random)).recover(x -> { throw genError(2, size, x); }).get();
try {
final Condition condition = Try.of(() -> predicate.apply(val1, val2)).recover(x -> { throw predicateError(x); }).get();
if (condition.precondition) {
exhausted = false;
代码示例来源:origin: vavr-io/vavr
final Gen<T1> gen1 = Try.of(() -> a1.apply(size)).recover(x -> { throw arbitraryError(1, size, x); }).get();
final Gen<T2> gen2 = Try.of(() -> a2.apply(size)).recover(x -> { throw arbitraryError(2, size, x); }).get();
final Gen<T3> gen3 = Try.of(() -> a3.apply(size)).recover(x -> { throw arbitraryError(3, size, x); }).get();
final Gen<T4> gen4 = Try.of(() -> a4.apply(size)).recover(x -> { throw arbitraryError(4, size, x); }).get();
final Gen<T5> gen5 = Try.of(() -> a5.apply(size)).recover(x -> { throw arbitraryError(5, size, x); }).get();
boolean exhausted = true;
for (int i = 1; i <= tries; i++) {
try {
final T1 val1 = Try.of(() -> gen1.apply(random)).recover(x -> { throw genError(1, size, x); }).get();
final T2 val2 = Try.of(() -> gen2.apply(random)).recover(x -> { throw genError(2, size, x); }).get();
final T3 val3 = Try.of(() -> gen3.apply(random)).recover(x -> { throw genError(3, size, x); }).get();
final T4 val4 = Try.of(() -> gen4.apply(random)).recover(x -> { throw genError(4, size, x); }).get();
final T5 val5 = Try.of(() -> gen5.apply(random)).recover(x -> { throw genError(5, size, x); }).get();
try {
final Condition condition = Try.of(() -> predicate.apply(val1, val2, val3, val4, val5)).recover(x -> { throw predicateError(x); }).get();
if (condition.precondition) {
exhausted = false;
代码示例来源:origin: vavr-io/vavr
final Gen<T1> gen1 = Try.of(() -> a1.apply(size)).recover(x -> { throw arbitraryError(1, size, x); }).get();
final Gen<T2> gen2 = Try.of(() -> a2.apply(size)).recover(x -> { throw arbitraryError(2, size, x); }).get();
final Gen<T3> gen3 = Try.of(() -> a3.apply(size)).recover(x -> { throw arbitraryError(3, size, x); }).get();
boolean exhausted = true;
for (int i = 1; i <= tries; i++) {
try {
final T1 val1 = Try.of(() -> gen1.apply(random)).recover(x -> { throw genError(1, size, x); }).get();
final T2 val2 = Try.of(() -> gen2.apply(random)).recover(x -> { throw genError(2, size, x); }).get();
final T3 val3 = Try.of(() -> gen3.apply(random)).recover(x -> { throw genError(3, size, x); }).get();
try {
final Condition condition = Try.of(() -> predicate.apply(val1, val2, val3)).recover(x -> { throw predicateError(x); }).get();
if (condition.precondition) {
exhausted = false;
代码示例来源:origin: vavr-io/vavr
final Gen<T1> gen1 = Try.of(() -> a1.apply(size)).recover(x -> { throw arbitraryError(1, size, x); }).get();
final Gen<T2> gen2 = Try.of(() -> a2.apply(size)).recover(x -> { throw arbitraryError(2, size, x); }).get();
final Gen<T3> gen3 = Try.of(() -> a3.apply(size)).recover(x -> { throw arbitraryError(3, size, x); }).get();
final Gen<T4> gen4 = Try.of(() -> a4.apply(size)).recover(x -> { throw arbitraryError(4, size, x); }).get();
final Gen<T5> gen5 = Try.of(() -> a5.apply(size)).recover(x -> { throw arbitraryError(5, size, x); }).get();
final Gen<T6> gen6 = Try.of(() -> a6.apply(size)).recover(x -> { throw arbitraryError(6, size, x); }).get();
final Gen<T7> gen7 = Try.of(() -> a7.apply(size)).recover(x -> { throw arbitraryError(7, size, x); }).get();
final Gen<T8> gen8 = Try.of(() -> a8.apply(size)).recover(x -> { throw arbitraryError(8, size, x); }).get();
boolean exhausted = true;
for (int i = 1; i <= tries; i++) {
try {
final T1 val1 = Try.of(() -> gen1.apply(random)).recover(x -> { throw genError(1, size, x); }).get();
final T2 val2 = Try.of(() -> gen2.apply(random)).recover(x -> { throw genError(2, size, x); }).get();
final T3 val3 = Try.of(() -> gen3.apply(random)).recover(x -> { throw genError(3, size, x); }).get();
final T4 val4 = Try.of(() -> gen4.apply(random)).recover(x -> { throw genError(4, size, x); }).get();
final T5 val5 = Try.of(() -> gen5.apply(random)).recover(x -> { throw genError(5, size, x); }).get();
final T6 val6 = Try.of(() -> gen6.apply(random)).recover(x -> { throw genError(6, size, x); }).get();
final T7 val7 = Try.of(() -> gen7.apply(random)).recover(x -> { throw genError(7, size, x); }).get();
final T8 val8 = Try.of(() -> gen8.apply(random)).recover(x -> { throw genError(8, size, x); }).get();
try {
final Condition condition = Try.of(() -> predicate.apply(val1, val2, val3, val4, val5, val6, val7, val8)).recover(x -> { throw predicateError(x); }).get();
if (condition.precondition) {
exhausted = false;
代码示例来源:origin: vavr-io/vavr
final Gen<T1> gen1 = Try.of(() -> a1.apply(size)).recover(x -> { throw arbitraryError(1, size, x); }).get();
boolean exhausted = true;
for (int i = 1; i <= tries; i++) {
try {
final T1 val1 = Try.of(() -> gen1.apply(random)).recover(x -> { throw genError(1, size, x); }).get();
try {
final Condition condition = Try.of(() -> predicate.apply(val1)).recover(x -> { throw predicateError(x); }).get();
if (condition.precondition) {
exhausted = false;
代码示例来源: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 shouldReturnAfterThreeAttemptsAndRecover() {
// Given the HelloWorldService throws an exception
BDDMockito.given(helloWorldService.returnHelloWorld()).willThrow(new WebServiceException("BAM!"));
// Create a Retry with default configuration
Retry retry = Retry.ofDefaults("id");
// Decorate the invocation of the HelloWorldService
CheckedFunction0<String> retryableSupplier = Retry
.decorateCheckedSupplier(retry, helloWorldService::returnHelloWorld);
// When
Try<String> result = Try.of(retryableSupplier).recover((throwable) -> "Hello world from recovery function");
assertThat(retry.getMetrics().getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(1);
// Then the helloWorldService should be invoked 3 times
BDDMockito.then(helloWorldService).should(Mockito.times(3)).returnHelloWorld();
// and the returned exception should be of type RuntimeException
assertThat(result.get()).isEqualTo("Hello world from recovery function");
assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION * 2);
}
代码示例来源:origin: resilience4j/resilience4j
@Test
public void shouldReturnAfterThreeAttemptsAndRecoverWithResult() {
// Given the HelloWorldService throws an exception
BDDMockito.given(helloWorldService.returnHelloWorld()).willThrow(new WebServiceException("BAM!"))
.willReturn("Hello world")
.willThrow(new WebServiceException("BAM!"));
// Create a Retry with default configuration
final RetryConfig tryAgain = RetryConfig.<String>custom().retryOnResult(s -> s.contains("Hello world"))
.maxAttempts(3).build();
Retry retry = Retry.of("id", tryAgain);
// Decorate the invocation of the HelloWorldService
CheckedFunction0<String> retryableSupplier = Retry
.decorateCheckedSupplier(retry, helloWorldService::returnHelloWorld);
// When
Try<String> result = Try.of(retryableSupplier).recover((throwable) -> "Hello world from recovery function");
// Then the helloWorldService should be invoked 3 times
BDDMockito.then(helloWorldService).should(Mockito.times(3)).returnHelloWorld();
// and the returned exception should be of type RuntimeException
assertThat(result.get()).isEqualTo("Hello world from recovery function");
assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION * 2);
}
代码示例来源:origin: com.github.robozonky/robozonky-common
private static <O> Either<Throwable, O> execute(final Supplier<O> operation) {
LOGGER.trace("Will execute {}.", operation);
return Try.ofSupplier(operation).map(Either::<Throwable, O>right)
.recover(t -> {
LOGGER.debug("Operation failed.", t);
return Either.left(t);
}).get();
}
代码示例来源:origin: RoboZonky/robozonky
private static <O> Either<Throwable, O> execute(final Supplier<O> operation) {
LOGGER.trace("Will execute {}.", operation);
return Try.ofSupplier(operation).map(Either::<Throwable, O>right)
.recover(t -> {
LOGGER.debug("Operation failed.", t);
return Either.left(t);
}).get();
}
代码示例来源:origin: spring-cloud-incubator/spring-cloud-circuitbreaker
@Override
public <T> T run(Supplier<T> toRun, Function<Throwable, T> fallback) {
TimeLimiter timeLimiter = TimeLimiter.of(timeLimiterConfig);
Supplier<Future<T>> futureSupplier = () -> executorService.submit(toRun::get);
Callable restrictedCall = TimeLimiter
.decorateFutureSupplier(timeLimiter, futureSupplier);
io.github.resilience4j.circuitbreaker.CircuitBreaker defaultCircuitBreaker = registry.circuitBreaker(id, circuitBreakerConfig);
Callable<T> callable = io.github.resilience4j.circuitbreaker.CircuitBreaker
.decorateCallable(defaultCircuitBreaker, restrictedCall);
return Try.of(callable::call).recover(fallback).get();
}
}
代码示例来源:origin: Tristan971/Lyrebird
private void createTabForPal(final User user) {
if (loadedPals.contains(user)) return;
LOG.debug("Creating a conversation tab for conversation with {}", user.getScreenName());
easyFxml.loadNode(DIRECT_MESSAGE_CONVERSATION, Pane.class, DMConversationController.class)
.afterControllerLoaded(dmc -> dmc.setPal(user))
.getNode()
.recover(ExceptionHandler::fromThrowable)
.map(conversation -> new Tab(user.getName(), conversation))
.onSuccess(tab -> Platform.runLater(() -> {
LOG.debug("Adding [{}] as tab for user {}", tab.getText(), user.getScreenName());
this.conversationsManaged.add(tab);
}));
loadedPals.add(user);
}
代码示例来源:origin: Tristan971/Lyrebird
/**
* If this tweet is in reply to another, we set proper display of the previous one because it looks good.
*
* @param repliedTweet the tweet ({@link Status} to which this is in reply to
*/
public void setInReplyToTweet(final Status repliedTweet) {
LOG.debug("Set new tweet stage to embed status : {}", repliedTweet.getId());
inReplyStatus.setValue(repliedTweet);
easyFxml.loadNode(FxComponent.TWEET, Pane.class, TweetPaneController.class)
.afterControllerLoaded(tpc -> {
tpc.embeddedPropertyProperty().setValue(true);
tpc.updateWithValue(repliedTweet);
})
.getNode()
.recover(ExceptionHandler::fromThrowable)
.onSuccess(this.container::setTop);
}
代码示例来源:origin: Tristan971/Lyrebird
/**
* Loads the target user's timeline into the view.
*/
private void loadTargetUserTimeline() {
final User user = targetUserProp.getValue();
easyFxml.loadNode(FxComponent.USER_TIMELINE, Pane.class, UserTimelineController.class)
.afterControllerLoaded(utc -> utc.setTargetUser(user))
.afterNodeLoaded(userDetailsTimeline -> VBox.setVgrow(userDetailsTimeline, Priority.ALWAYS))
.getNode()
.recover(ExceptionHandler::fromThrowable)
.onSuccess(container.getChildren()::add);
}
代码示例来源:origin: Tristan971/Lyrebird
/**
* Pre-formats a status' text content.
*
* @param status The status whose content we have to format
*/
private void loadTextIntoTextFlow(final Status status) {
tweetContent.getChildren().clear();
final FxmlLoadResult<Pane, TweetContentPaneController> result = easyFxml.loadNode(
FxComponent.TWEET_CONTENT_PANE,
Pane.class,
TweetContentPaneController.class
);
result.afterControllerLoaded(tcpc -> tcpc.setStatusProp(status))
.getNode()
.peek(pane -> VBox.setVgrow(pane, Priority.ALWAYS))
.recover(ExceptionHandler::fromThrowable)
.andThen(tweetContent.getChildren()::add);
}
代码示例来源:origin: Tristan971/Lyrebird
private void setUpInteractionButtons() {
interactionBox.visibleProperty().bind(embeddedProperty.not());
interactionBox.managedProperty().bind(embeddedProperty.not());
easyFxml.loadNode(TWEET_INTERACTION_BOX, Pane.class, TweetInteractionPaneController.class)
.afterControllerLoaded(tipc -> tipc.targetStatusProperty().bind(currentStatus))
.getNode()
.recover(ExceptionHandler::fromThrowable)
.onSuccess(interactionBox::setCenter);
}
内容来源于网络,如有侵权,请联系作者删除!