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

x33g5p2x  于2022-01-19 转载在 其他  
字(4.7k)|赞(0)|评价(0)|浏览(199)

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

Either.fold介绍

[英]Folds either the left or the right side of this disjunction.
[中]折叠此分离的左侧或右侧。

代码示例

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

private static boolean isValidForLoansSeenBefore(final Either<InvestmentFailure, BigDecimal> responseType) {
  // previously delegated means it can still be on the marketplace when checked next time
  if (responseType == null) {
    return false;
  }
  return responseType.fold(response -> response == InvestmentFailure.DELEGATED, amount -> false);
}

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

private void ensureConsumerIsAlive() {
  final boolean isReady = firingThread.get().fold(t -> {
    LOGGER.debug("Failed retrieving event firing thread.", t);
    return false;
  }, Thread::isAlive);
  if (!isReady) {
    LOGGER.debug("Consumer thread not alive, restarting.");
    firingThread.clear();
    ensureConsumerIsAlive();
  }
}

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

private void ensureConsumerIsAlive(final boolean isRestarted) {
  final boolean isReady = firingThread.get().fold(t -> {
    LOGGER.debug("Failed retrieving event firing thread.", t);
    return false;
  }, Thread::isAlive);
  if (isReady) { // the thread is available
    return;
  } else if (isRestarted) { // the thread is not available even though it really should have been by now
    throw new IllegalStateException("Event firing thread could not be started.");
  } else {
    LOGGER.debug("Consumer thread not alive, restarting.");
    firingThread.clear();
    ensureConsumerIsAlive(true); // "true" here prevents endless recursion
  }
}

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

public CompletableFuture<Optional<File>> download(final Tenant tenant, final Duration backoffTime) {
  final Backoff<URL> waitWhileExportRunning = Backoff.exponential(() -> tenant.call(download, ZonkyScope.FILES),
                                  Duration.ofSeconds(1), backoffTime);
  return CompletableFuture.runAsync(() -> tenant.run(trigger, ZonkyScope.APP))
      .thenApplyAsync(v -> waitWhileExportRunning.get())
      .thenApplyAsync(urlOrError -> urlOrError.fold(r -> Optional.empty(), Util::download));
}

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

public CompletableFuture<Optional<File>> download(final Tenant tenant, final Duration backoffTime) {
  final Backoff<URL> waitWhileExportRunning = Backoff.exponential(() -> tenant.call(download, OAuthScope.SCOPE_FILE_DOWNLOAD),
                                  Duration.ofSeconds(1), backoffTime);
  return CompletableFuture.runAsync(() -> tenant.run(trigger, OAuthScope.SCOPE_APP_WEB))
      .thenApplyAsync(v -> waitWhileExportRunning.get())
      .thenApplyAsync(urlOrError -> urlOrError.fold(r -> Optional.empty(), Util::download));
}

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

/**
 * Request {@link ControlApi} to invest in a given loan, leveraging the {@link ConfirmationProvider}.
 * @param recommendation Loan to invest into.
 * @return True if investment successful. The investment is reflected in {@link #getResult()}.
 */
boolean invest(final RecommendedLoan recommendation) {
  LOGGER.debug("Will attempt to invest in {}.", recommendation);
  final LoanDescriptor loan = recommendation.descriptor();
  final int loanId = loan.item().getId();
  if (tenant.getPortfolio().getBalance().compareTo(recommendation.amount()) < 0) {
    // should not be allowed by the calling code
    LOGGER.debug("Balance was less than recommendation.");
    return false;
  }
  tenant.fire(investmentRequested(recommendation));
  final boolean seenBefore = seen.contains(loan);
  final Either<InvestmentFailure, BigDecimal> response = investor.invest(recommendation, seenBefore);
  InvestingSession.LOGGER.debug("Response for loan {}: {}.", loanId, response);
  return response.fold(reason -> unsuccessfulInvestment(recommendation, reason),
             amount -> successfulInvestment(recommendation, amount));
}

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

/**
 * Request {@link ControlApi} to invest in a given loan, leveraging the {@link ConfirmationProvider}.
 * @param recommendation Loan to invest into.
 * @return True if investment successful. The investment is reflected in {@link #getResult()}.
 */
boolean invest(final RecommendedLoan recommendation) {
  LOGGER.debug("Will attempt to invest in {}.", recommendation);
  final LoanDescriptor loan = recommendation.descriptor();
  final int loanId = loan.item().getId();
  if (tenant.getPortfolio().getBalance().compareTo(recommendation.amount()) < 0) {
    // should not be allowed by the calling code
    LOGGER.debug("Balance was less than recommendation.");
    return false;
  }
  tenant.fire(investmentRequested(recommendation));
  final boolean seenBefore = seen.contains(loan);
  final Either<InvestmentFailure, BigDecimal> response = investor.invest(recommendation, seenBefore);
  LOGGER.debug("Response for loan {}: {}.", loanId, response);
  return response.fold(reason -> unsuccessfulInvestment(recommendation, reason),
             amount -> successfulInvestment(recommendation, amount));
}

相关文章