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

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

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

Try.withResources介绍

[英]Creates a Try-with-resources builder that operates on one AutoCloseable resource.
[中]创建在一个可自动关闭的资源上运行的“资源试用生成器”。

代码示例

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

  1. public static void writeOutProperties(final Properties properties, final File target) {
  2. Try.withResources(() -> Files.newBufferedWriter(target.toPath(), Defaults.CHARSET))
  3. .of(w -> {
  4. properties.store(w, Defaults.ROBOZONKY_USER_AGENT);
  5. LOGGER.debug("Written properties to {}.", target);
  6. return null;
  7. })
  8. .getOrElseThrow((Function<Throwable, IllegalStateException>) IllegalStateException::new);
  9. }

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

  1. public static void writeOutProperties(final Properties properties, final File target) {
  2. Try.withResources(() -> Files.newBufferedWriter(target.toPath(), Defaults.CHARSET))
  3. .of(w -> {
  4. properties.store(w, Defaults.ROBOZONKY_USER_AGENT);
  5. LOGGER.debug("Written properties to {}.", target);
  6. return null;
  7. })
  8. .getOrElseThrow((Function<Throwable, IllegalStateException>) IllegalStateException::new);
  9. }

代码示例来源: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: 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: com.github.robozonky/robozonky-integration-stonky

  1. private URL download(final Zonky zonky, final Function<Zonky, Response> delegate) {
  2. return Try.withResources(() -> delegate.apply(zonky))
  3. .of(response -> {
  4. final int status = response.getStatus();
  5. LOGGER.debug("Download endpoint returned HTTP {}.", status);
  6. if (status != 302) {
  7. throw new IllegalStateException("Download not yet ready: " + this);
  8. }
  9. final String s = response.getHeaderString("Location");
  10. return new URL(s);
  11. }).get();
  12. }

代码示例来源: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: 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: com.github.robozonky/robozonky-app

  1. @Override
  2. protected String getLatestSource() {
  3. return Try.withResources(url::openStream)
  4. .of(s -> IOUtils.toString(s, Defaults.CHARSET))
  5. .getOrElseThrow((Function<Throwable, IllegalStateException>) IllegalStateException::new);
  6. }

代码示例来源: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<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: 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: 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: com.github.robozonky/robozonky-integration-stonky

  1. private GoogleClientSecrets createClientSecrets() {
  2. final byte[] key = secrets.get();
  3. return Try.withResources(() -> new ByteArrayInputStream(key))
  4. .of(s -> GoogleClientSecrets.load(Util.JSON_FACTORY, new InputStreamReader(s)))
  5. .getOrElseThrow((Function<Throwable, IllegalStateException>) IllegalStateException::new);
  6. }

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

  1. @Override
  2. protected String getLatestSource() {
  3. return Try.withResources(() -> UpdateMonitor.getMavenCentralData(this.groupId, this.artifactId,
  4. this.mavenCentralHostname))
  5. .of(s -> IOUtils.toString(s, Defaults.CHARSET))
  6. .getOrElseThrow((Function<Throwable, IllegalStateException>) IllegalStateException::new);
  7. }

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

相关文章