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

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

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

Try.onFailure介绍

[英]Consumes the cause if this is a Try.Failure and the cause is instance of X.

  1. // (does not print anything)

[中]如果这是一次尝试,则消耗原因。失败,原因是X实例。

  1. // (does not print anything)

代码示例

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

  1. /**
  2. * Performs the action once the Future is complete and the result is a {@link Try.Failure}. Please note that the
  3. * future is also a failure when it was cancelled.
  4. *
  5. * @param action An action to be performed when this future failed.
  6. * @return this Future
  7. * @throws NullPointerException if {@code action} is null.
  8. */
  9. default Future<T> onFailure(Consumer<? super Throwable> action) {
  10. Objects.requireNonNull(action, "action is null");
  11. return onComplete(result -> result.onFailure(action));
  12. }

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

  1. /**
  2. * Converts this to a {@link CompletableFuture}
  3. *
  4. * @return A new {@link CompletableFuture} containing the value
  5. */
  6. @GwtIncompatible
  7. default CompletableFuture<T> toCompletableFuture() {
  8. final CompletableFuture<T> completableFuture = new CompletableFuture<>();
  9. Try.of(this::get)
  10. .onSuccess(completableFuture::complete)
  11. .onFailure(completableFuture::completeExceptionally);
  12. return completableFuture;
  13. }

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

  1. private void exec(Runnable runnable, int times) {
  2. for (int i = 0; i < times; i++) {
  3. Try.runRunnable(runnable)
  4. .onFailure(e -> System.out.println(e.getMessage()));
  5. }
  6. }

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

  1. /**
  2. * Transforms the value of this {@code Future}, whether it is a success or a failure.
  3. *
  4. * @param f A transformation
  5. * @param <U> Generic type of transformation {@code Try} result
  6. * @return A {@code Future} of type {@code U}
  7. * @throws NullPointerException if {@code f} is null
  8. */
  9. default <U> Future<U> transformValue(Function<? super Try<T>, ? extends Try<? extends U>> f) {
  10. Objects.requireNonNull(f, "f is null");
  11. return run(executor(), complete ->
  12. onComplete(t -> Try.run(() -> complete.with(f.apply(t)))
  13. .onFailure(x -> complete.with(Try.failure(x)))
  14. )
  15. );
  16. }

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

  1. default <U> Future<U> flatMapTry(CheckedFunction1<? super T, ? extends Future<? extends U>> mapper) {
  2. Objects.requireNonNull(mapper, "mapper is null");
  3. return run(executor(), complete ->
  4. onComplete(result -> result.mapTry(mapper)
  5. .onSuccess(future -> future.onComplete(complete::with))
  6. .onFailure(x -> complete.with(Try.failure(x)))
  7. )
  8. );
  9. }

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

  1. .onFailure(ignored -> {
  2. if (wasLast) {
  3. completed.set(complete.with(Try.success(Option.none())));

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

  1. /**
  2. * Handles a failure of this Future by returning the result of another Future.
  3. * <p>
  4. * Example:
  5. * <pre><code>
  6. * // = "oh!"
  7. * Future.of(() -&gt; { throw new Error("oh!"); }).recoverWith(x -&gt; Future.of(x::getMessage));
  8. * </code></pre>
  9. *
  10. * @param f A function which takes the exception of a failure and returns a new future.
  11. * @return A new Future.
  12. * @throws NullPointerException if {@code f} is null
  13. */
  14. default Future<T> recoverWith(Function<? super Throwable, ? extends Future<? extends T>> f) {
  15. Objects.requireNonNull(f, "f is null");
  16. return run(executor(), complete ->
  17. onComplete(t -> {
  18. if (t.isFailure()) {
  19. Try.run(() -> f.apply(t.getCause()).onComplete(complete::with))
  20. .onFailure(x -> complete.with(Try.failure(x)));
  21. } else {
  22. complete.with(t);
  23. }
  24. })
  25. );
  26. }

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

  1. /**
  2. * Performs the action once the Future is complete and the result is a {@link Try.Failure}. Please note that the
  3. * future is also a failure when it was cancelled.
  4. *
  5. * @param action An action to be performed when this future failed.
  6. * @return this Future
  7. * @throws NullPointerException if {@code action} is null.
  8. */
  9. default Future<T> onFailure(Consumer<? super Throwable> action) {
  10. Objects.requireNonNull(action, "action is null");
  11. return onComplete(result -> result.onFailure(action));
  12. }

代码示例来源:origin: TinkoffCreditSystems/QVisual

  1. public static void post(String url, String snapshot) {
  2. Try.of(() -> {
  3. HttpClient client = HttpClientBuilder.create().build();
  4. HttpPost httpPost = new HttpPost(url);
  5. httpPost.setEntity(new StringEntity(snapshot, "UTF-8"));
  6. HttpResponse response = client.execute(httpPost);
  7. return new BasicResponseHandler().handleResponse(response);
  8. }).onFailure(t -> logger.error("[POST snapshot]", t));
  9. }

代码示例来源:origin: com.mercateo.eventstore/client-common

  1. public Either<EventStoreFailure, String> toJsonString(Object data) {
  2. return Try //
  3. .of(() -> objectMapper.writeValueAsString(data))
  4. .onFailure(e -> log.warn("could not deserialize {}", data != null ? data.getClass().getSimpleName() : null,
  5. e))
  6. .toEither()
  7. .mapLeft(this::mapInternalError);
  8. }

代码示例来源:origin: TinkoffCreditSystems/QVisual

  1. public static Try<String> writeAsString(Object json) {
  2. return Try.of(() -> getMapper().writeValueAsString(json)).onFailure(t -> logger.error("[write json]", t));
  3. }

代码示例来源:origin: TinkoffCreditSystems/QVisual

  1. public static <T> Try<T> parseJson(String json, TypeReference<T> type) {
  2. return (Try<T>) Try.of(() -> getMapper().readValue(json, type)).onFailure(t -> logger.error("[parse json]", t));
  3. }

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

  1. /**
  2. * Converts this to a {@link CompletableFuture}
  3. *
  4. * @return A new {@link CompletableFuture} containing the value
  5. */
  6. @GwtIncompatible
  7. default CompletableFuture<T> toCompletableFuture() {
  8. final CompletableFuture<T> completableFuture = new CompletableFuture<>();
  9. Try.of(this::get)
  10. .onSuccess(completableFuture::complete)
  11. .onFailure(completableFuture::completeExceptionally);
  12. return completableFuture;
  13. }

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

  1. public void openUserDetails(final String screenName) {
  2. findUser(screenName)
  3. .onSuccess(this::openUserDetails)
  4. .onFailure(err -> ExceptionHandler.displayExceptionPane(
  5. "Unknown user!",
  6. "Can't map this user's screen name (@...) to an actual Twitter user!",
  7. err
  8. ));
  9. }

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

  1. @Override
  2. public void refresh() {
  3. if (!sessionManager.isLoggedInProperty().getValue()) {
  4. LOG.debug("Logged out, not refreshing direct messages.");
  5. return;
  6. }
  7. CompletableFuture.runAsync(() -> {
  8. LOG.debug("Requesting last direct messages.");
  9. sessionManager.getCurrentTwitter()
  10. .mapTry(twitter -> twitter.getDirectMessages(20))
  11. .onSuccess(this::addDirectMessages)
  12. .onFailure(err -> LOG.error("Could not load direct messages successfully!", err));
  13. });
  14. }

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

  1. public void openUserDetails(final long targetUserId) {
  2. findUser(targetUserId)
  3. .onSuccess(this::openUserDetails)
  4. .onFailure(err -> ExceptionHandler.displayExceptionPane(
  5. "Unknown user!",
  6. "Can't map this user's userId to an actual Twitter user!",
  7. err
  8. ));
  9. }

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

  1. /**
  2. * Fetches, shows and moves the main application stage to the front.
  3. */
  4. private void showMainStage() {
  5. CompletableFuture.runAsync(
  6. () -> stageManager.getSingle(Screen.ROOT_VIEW)
  7. .toTry(IllegalStateException::new)
  8. .onSuccess(stage -> {
  9. stage.show();
  10. stage.setIconified(false);
  11. }).onFailure(err -> LOG.error("Could not show main stage!", err)),
  12. Platform::runLater
  13. );
  14. }

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

  1. /**
  2. * Loads the current user's account view on the top of the bar.
  3. */
  4. private void loadCurrentAccountPanel() {
  5. easyFxml.loadNode(CURRENT_ACCOUNT)
  6. .getNode()
  7. .onSuccess(container::setTop)
  8. .onFailure(err -> displayExceptionPane(
  9. "Could not load current user!",
  10. "There was an error mapping the current session to a twitter account.",
  11. err
  12. ));
  13. }

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

  1. /**
  2. * Transforms the value of this {@code Future}, whether it is a success or a failure.
  3. *
  4. * @param f A transformation
  5. * @param <U> Generic type of transformation {@code Try} result
  6. * @return A {@code Future} of type {@code U}
  7. * @throws NullPointerException if {@code f} is null
  8. */
  9. default <U> Future<U> transformValue(Function<? super Try<T>, ? extends Try<? extends U>> f) {
  10. Objects.requireNonNull(f, "f is null");
  11. return run(executor(), complete ->
  12. onComplete(t -> Try.run(() -> complete.with(f.apply(t)))
  13. .onFailure(x -> complete.with(Try.failure(x)))
  14. )
  15. );
  16. }

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

  1. default <U> Future<U> flatMapTry(CheckedFunction1<? super T, ? extends Future<? extends U>> mapper) {
  2. Objects.requireNonNull(mapper, "mapper is null");
  3. return run(executor(), complete ->
  4. onComplete(result -> result.mapTry(mapper)
  5. .onSuccess(future -> future.onComplete(complete::with))
  6. .onFailure(x -> complete.with(Try.failure(x)))
  7. )
  8. );
  9. }

相关文章