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

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

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

Try.peek介绍

[英]Applies the action to the value of a Success or does nothing in the case of a Failure.
[中]

代码示例

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

  1. CompletableFuture<Void> refreshIfNotAlreadyRefreshing(final CompletableFuture<Void> old) {
  2. if (old == null || old.isDone()) {
  3. logger.trace("Starting async reload.");
  4. final Runnable asyncOperation = () -> Try.ofSupplier(() -> getOperation().apply(value.get()))
  5. .peek(v -> processRetrievedValue(v, value::set)) // set the value on success
  6. .getOrElseGet(t -> {
  7. logger.warn("Async reload failed, operating with stale value.", t);
  8. return null;
  9. });
  10. return CompletableFuture.runAsync(asyncOperation, Scheduler.inBackground().getExecutor());
  11. } else {
  12. logger.trace("Reload already in progress on {} with {}.", this, old);
  13. return old;
  14. }
  15. }

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

  1. CompletableFuture<Void> refresh(final CompletableFuture<Void> old) {
  2. if (old == null || old.isDone()) {
  3. logger.trace("Starting async reload.");
  4. final Runnable asyncOperation = () -> Try.ofSupplier(getOperation())
  5. .peek(v -> processRetrievedValue(v, value::set)) // set the value on success
  6. .getOrElseGet(t -> {
  7. logger.warn("Async reload failed, operating with stale value.", t);
  8. return null;
  9. });
  10. return CompletableFuture.runAsync(asyncOperation, Scheduler.inBackground().getExecutor());
  11. } else {
  12. logger.trace("Reload already in progress on {} with {}.", this, old);
  13. return old;
  14. }
  15. }

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

  1. @Override
  2. public synchronized Either<Throwable, T> get() {
  3. if (!needsReload()) {
  4. logger.trace("Not reloading {}.", this);
  5. return Either.right(value.get());
  6. }
  7. logger.trace("Reloading {}.", this);
  8. return Try.ofSupplier(getOperation())
  9. .peek(v -> processRetrievedValue(v, value::set))
  10. .toEither();
  11. }
  12. }

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

  1. @Override
  2. public Either<Throwable, T> get() {
  3. if (needsReload()) { // double-checked locking to make sure the reload only happens once
  4. synchronized (this) {
  5. if (needsReload()) {
  6. logger.trace("Reloading {}.", this);
  7. return Try.ofSupplier(() -> getOperation().apply(value.get()))
  8. .peek(v -> processRetrievedValue(v, value::set))
  9. .toEither();
  10. }
  11. // otherwise fall through to retrieve the value
  12. }
  13. }
  14. logger.trace("Not reloading {}.", this);
  15. return Either.right(value.get());
  16. }

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

  1. @Override
  2. public synchronized Either<Throwable, T> get() {
  3. if (value.get() == null) { // force value retrieval and wait for it
  4. logger.debug("Fetching initial value synchronously on {}.", this);
  5. return Try.ofSupplier(getOperation())
  6. .peek(v -> processRetrievedValue(v, value::set))
  7. .toEither();
  8. }
  9. if (!needsReload()) { // return old value
  10. logger.trace("Not reloading {}.", this);
  11. return Either.right(value.get());
  12. }
  13. // trigger retrieval but return existing value
  14. final CompletableFuture<Void> currentFuture = future.getAndUpdate(this::refresh);
  15. logger.debug("Retrieved potentially stale value on {}, while {}.", this, currentFuture);
  16. return Either.right(value.get());
  17. }
  18. }

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

  1. @Override
  2. public Either<Throwable, T> get() {
  3. if (!hasValue()) { // force value retrieval and wait for it
  4. synchronized (this) {
  5. if (!hasValue()) { // double-checked locking to make sure the value is only ever loaded once
  6. logger.debug("Fetching initial value synchronously on {}.", this);
  7. return Try.ofSupplier(() -> getOperation().apply(null))
  8. .peek(v -> processRetrievedValue(v, value::set))
  9. .toEither();
  10. }
  11. // otherwise fall through to retrieve the current value
  12. }
  13. }
  14. if (needsReload()) { // trigger value retrieval on the background
  15. synchronized (this) {
  16. final CompletableFuture<Void> currentFuture = future.getAndUpdate(this::refreshIfNotAlreadyRefreshing);
  17. logger.debug("Retrieved potentially stale value on {}, while {}.", this, currentFuture);
  18. }
  19. }
  20. // return the current value
  21. return Either.right(value.get());
  22. }

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

相关文章