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

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

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

Try.getOrElseGet介绍

暂无

代码示例

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

  1. @Override
  2. protected String getLatestSource() {
  3. logger.debug("Reading notification configuration from '{}'.", source);
  4. return Try.withResources(() -> new BufferedReader(new InputStreamReader(source.openStream(), Defaults.CHARSET)))
  5. .of(r -> r.lines().collect(Collectors.joining(System.lineSeparator())))
  6. .getOrElseGet(t -> {
  7. logger.warn("Failed reading notification configuration from '{}' due to '{}'.", source,
  8. t.getMessage());
  9. return null;
  10. });
  11. }
  12. }

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

  1. @Override
  2. protected String getLatestSource() {
  3. LOGGER.debug("Reading notification configuration from '{}'.", source);
  4. return Try.withResources(() -> new BufferedReader(new InputStreamReader(source.openStream(), Defaults.CHARSET)))
  5. .of(r -> r.lines().collect(Collectors.joining(System.lineSeparator())))
  6. .getOrElseGet(t -> {
  7. LOGGER.warn("Failed reading notification configuration from '{}' due to '{}'.", source,
  8. t.getMessage());
  9. return null;
  10. });
  11. }
  12. }

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

  1. static Optional<File> download(final URL url) {
  2. return Try.withResources(url::openStream)
  3. .of(Util::download)
  4. .getOrElseGet(t -> {
  5. LOGGER.warn("Failed downloading file.", t);
  6. return Optional.empty();
  7. });
  8. }

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

  1. @Override
  2. protected Optional<VersionIdentifier> transform(final String source) {
  3. return Try.withResources(() -> new ByteArrayInputStream(source.getBytes(Defaults.CHARSET)))
  4. .of(s -> Optional.of(UpdateMonitor.parseVersionString(s)))
  5. .getOrElseGet(ex -> {
  6. LOGGER.debug("Failed parsing source.", ex);
  7. return Optional.empty();
  8. });
  9. }
  10. }

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

  1. public static Optional<File> findFolder(final String folderName) {
  2. final Path root = new File(System.getProperty("user.dir")).toPath();
  3. return Try.withResources(() -> Files.find(root, 1, (path, attr) -> attr.isDirectory()))
  4. .of(s -> s.map(Path::toFile)
  5. .filter(f -> Objects.equals(f.getName(), folderName))
  6. .findFirst())
  7. .getOrElseGet(ex -> {
  8. FileUtil.LOGGER.warn("Exception while walking file tree.", ex);
  9. return Optional.empty();
  10. });
  11. }

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

  1. @Override
  2. protected Optional<ConfigStorage> transform(final String source) {
  3. return Try.withResources(() -> new ByteArrayInputStream(source.getBytes(Defaults.CHARSET)))
  4. .of(baos -> Optional.of(ConfigStorage.create(baos)))
  5. .getOrElseGet(ex -> {
  6. logger.warn("Failed transforming source.", ex);
  7. return Optional.empty();
  8. });
  9. }

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

  1. @Override
  2. protected Optional<VersionIdentifier> transform(final String source) {
  3. return Try.withResources(() -> new ByteArrayInputStream(source.getBytes(Defaults.CHARSET)))
  4. .of(s -> Optional.of(UpdateMonitor.parseVersionString(s)))
  5. .getOrElseGet(ex -> {
  6. logger.debug("Failed parsing source.", ex);
  7. return Optional.empty();
  8. });
  9. }
  10. }

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

  1. ReturnCode execute(final InvestmentMode mode) {
  2. return Try.withResources(() -> mode)
  3. .of(this::executeSafe)
  4. .getOrElseGet(t -> {
  5. LOGGER.error("Caught unexpected exception, terminating daemon.", t);
  6. return ReturnCode.ERROR_UNEXPECTED;
  7. });
  8. }

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

  1. @Override
  2. protected Optional<ConfigStorage> transform(final String source) {
  3. return Try.withResources(() -> new ByteArrayInputStream(source.getBytes(Defaults.CHARSET)))
  4. .of(baos -> Optional.of(ConfigStorage.create(baos)))
  5. .getOrElseGet(ex -> {
  6. LOGGER.warn("Failed transforming source.", ex);
  7. return Optional.empty();
  8. });
  9. }

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

  1. ReturnCode execute(final InvestmentMode mode) {
  2. return Try.withResources(() -> mode)
  3. .of(this::executeSafe)
  4. .getOrElseGet(t -> {
  5. LOGGER.error("Caught unexpected exception, terminating daemon.", t);
  6. return ReturnCode.ERROR_UNEXPECTED;
  7. });
  8. }

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

  1. public static Optional<File> findFolder(final String folderName) {
  2. final Path root = new File(System.getProperty("user.dir")).toPath();
  3. return Try.withResources(() -> Files.find(root, 1, (path, attr) -> attr.isDirectory()))
  4. .of(s -> s.map(Path::toFile)
  5. .filter(f -> Objects.equals(f.getName(), folderName))
  6. .findFirst())
  7. .getOrElseGet(ex -> {
  8. LOGGER.warn("Exception while walking file tree.", ex);
  9. return Optional.empty();
  10. });
  11. }

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

  1. static Optional<File> download(final URL url) {
  2. return Try.withResources(url::openStream)
  3. .of(Util::download)
  4. .getOrElseGet(t -> {
  5. LOGGER.warn("Failed downloading file.", t);
  6. return Optional.empty();
  7. });
  8. }

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

  1. /**
  2. * Remove an entry from the key store.
  3. * @param alias The alias to locate the entry.
  4. * @return True if there is now no entry with a given key.
  5. */
  6. public boolean delete(final String alias) {
  7. return Try.of(() -> {
  8. this.keyStore.deleteEntry(alias);
  9. this.dirty.set(true);
  10. return true;
  11. }).getOrElseGet(t -> {
  12. LOGGER.debug("Entry '{}' not deleted.", alias, t);
  13. return false;
  14. });
  15. }

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

  1. /**
  2. * Remove an entry from the key store.
  3. * @param alias The alias to locate the entry.
  4. * @return True if there is now no entry with a given key.
  5. */
  6. public boolean delete(final String alias) {
  7. return Try.of(() -> {
  8. this.keyStore.deleteEntry(alias);
  9. this.dirty.set(true);
  10. return true;
  11. }).getOrElseGet(t -> {
  12. KeyStoreHandler.LOGGER.debug("Entry '{}' not deleted.", alias, t);
  13. return false;
  14. });
  15. }

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

  1. private Pane loadComponentSynchronous(final FxComponent fxComponent) {
  2. return this.easyFxml
  3. .loadNode(fxComponent)
  4. .getNode()
  5. .getOrElseGet(ExceptionHandler::fromThrowable);
  6. }

代码示例来源: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-integration-zonkoid

  1. private static boolean requestConfirmation(final RequestId requestId, final int loanId, final int amount,
  2. final String rootUrl, final String protocol) {
  3. return Try.withResources(HttpClients::createDefault)
  4. .of(httpClient -> {
  5. LOGGER.debug("Requesting notification of {} CZK for loan #{}.", amount, loanId);
  6. final HttpPost post = getRequest(requestId, loanId, amount, protocol, rootUrl);
  7. return httpClient.execute(post, ZonkoidConfirmationProvider::respond);
  8. })
  9. .getOrElseGet(t -> handleError(requestId, loanId, amount, rootUrl, protocol, t));
  10. }

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

  1. private static boolean requestConfirmation(final RequestId requestId, final int loanId, final int amount,
  2. final String rootUrl, final String protocol) {
  3. return Try.withResources(HttpClients::createDefault)
  4. .of(httpClient -> {
  5. LOGGER.debug("Requesting notification of {} CZK for loan #{}.", amount, loanId);
  6. final HttpPost post = getRequest(requestId, loanId, amount, protocol, rootUrl);
  7. return httpClient.execute(post, ZonkoidConfirmationProvider::respond);
  8. })
  9. .getOrElseGet(t -> handleError(requestId, loanId, amount, rootUrl, protocol, t));
  10. }

代码示例来源: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: Tristan971/Lyrebird

  1. /**
  2. * The {@link #update} box only show up when an update is detected as available. Then if it is the case, this method
  3. * is called on click to open the update information screen.
  4. *
  5. * @see Screen#UPDATE_VIEW
  6. */
  7. private void openUpdatesScreen() {
  8. final FxmlLoadResult<Pane, FxmlController> updateScreenLoadResult = easyFxml.loadNode(Screen.UPDATE_VIEW);
  9. final Pane updatePane = updateScreenLoadResult.getNode().getOrElseGet(ExceptionHandler::fromThrowable);
  10. Stages.stageOf("Updates", updatePane).thenAcceptAsync(Stages::scheduleDisplaying);
  11. }

相关文章