org.jooby.funzy.Try.with()方法的使用及代码示例

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

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

Try.with介绍

[英]Functional try-with-resources:

  1. byte[] content = Try.with(() -> newInputStream())

Jdbc example:

  1. Try.with(() -> newConnection()))
  2. .get();
  3. }

[中]使用资源进行功能性尝试:

  1. byte[] content = Try.with(() -> newInputStream())

Jdbc示例:

  1. Try.with(() -> newConnection()))
  2. .get();
  3. }

代码示例

代码示例来源:origin: jooby-project/jooby

  1. static String readString(final ClassLoader loader, final String path) {
  2. return Try.with(() -> loader.getResourceAsStream("whoops/" + path))
  3. .apply(stream -> new String(ByteStreams.toByteArray(stream), StandardCharsets.UTF_8))
  4. .get();
  5. }

代码示例来源:origin: jooby-project/jooby

  1. @Override
  2. public Collection<Import> apply(final String url, final Import previous) {
  3. String fname = nameWithExtension(url);
  4. List<String> segments = Splitter.on('/').trimResults().omitEmptyStrings()
  5. .splitToList(previous.getAbsoluteUri().toString());
  6. String relative = segments.subList(0, segments.size() - 1).stream()
  7. .collect(Collectors.joining("/", "/", ""));
  8. return Arrays.asList(relative + fname, fname)
  9. .stream()
  10. .map(resolver::apply)
  11. .filter(it -> it != null)
  12. .findFirst()
  13. .map(throwingFunction(it ->
  14. Try.with(it.toURL()::openStream)
  15. .apply(in -> new String(ByteStreams.toByteArray(in), StandardCharsets.UTF_8))
  16. .map(content -> Arrays.asList(new Import(it, it, content)))
  17. .get()
  18. ))
  19. .orElse(null);
  20. }

代码示例来源:origin: org.jooby/funzy

  1. /**
  2. * Functional try-with-resources with 3 inputs.
  3. *
  4. * @param r1 Input resource.
  5. * @param r2 Input resource.
  6. * @param r3 Input resource.
  7. * @param <R1> Resource type.
  8. * @param <R2> Resource type.
  9. * @param <R3> Resource type.
  10. * @return A resource handler.
  11. */
  12. public final static <R1 extends AutoCloseable, R2 extends AutoCloseable, R3 extends AutoCloseable> ResourceHandler3<R1, R2, R3> of(
  13. R1 r1, R2 r2, R3 r3) {
  14. return with(() -> r1, () -> r2, () -> r3);
  15. }

代码示例来源:origin: org.jooby/funzy

  1. /**
  2. * Functional try-with-resources with 4 inputs.
  3. *
  4. * @param r1 Input resource.
  5. * @param r2 Input resource.
  6. * @param r3 Input resource.
  7. * @param r4 Input resource.
  8. * @param <R1> Resource type.
  9. * @param <R2> Resource type.
  10. * @param <R3> Resource type.
  11. * @param <R4> Resource type.
  12. * @return A resource handler.
  13. */
  14. public final static <R1 extends AutoCloseable, R2 extends AutoCloseable, R3 extends AutoCloseable, R4 extends AutoCloseable> ResourceHandler4<R1, R2, R3, R4> of(
  15. R1 r1, R2 r2, R3 r3, R4 r4) {
  16. return with(() -> r1, () -> r2, () -> r3, () -> r4);
  17. }

代码示例来源:origin: org.jooby/funzy

  1. /**
  2. * Functional try-with-resources:
  3. *
  4. * <pre>{@code
  5. * InputStream in = ...;
  6. * OutputStream out = ...;
  7. *
  8. * Try.of(in, out)
  9. * .run((from, to) -> copy(from, to))
  10. * .onFailure(Throwable::printStacktrace);
  11. *
  12. * }</pre>
  13. *
  14. * @param r1 Input resource.
  15. * @param r2 Input resource.
  16. * @param <R1> Resource type.
  17. * @param <R2> Resource type.
  18. * @return A resource handler.
  19. */
  20. public final static <R1 extends AutoCloseable, R2 extends AutoCloseable> ResourceHandler2<R1, R2> of(
  21. R1 r1, R2 r2) {
  22. return with(() -> r1, () -> r2);
  23. }

代码示例来源:origin: org.jooby/funzy

  1. return with(() -> r1);

代码示例来源:origin: org.jooby/jooby-whoops

  1. static String readString(final ClassLoader loader, final String path) {
  2. return Try.with(() -> loader.getResourceAsStream("whoops/" + path))
  3. .apply(stream -> new String(ByteStreams.toByteArray(stream), StandardCharsets.UTF_8))
  4. .get();
  5. }

相关文章