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

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

本文整理了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:

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

Jdbc example:

Try.with(() -> newConnection())) 
.get(); 
}

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

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

Jdbc示例:

Try.with(() -> newConnection())) 
.get(); 
}

代码示例

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

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

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

@Override
public Collection<Import> apply(final String url, final Import previous) {
 String fname = nameWithExtension(url);
 List<String> segments = Splitter.on('/').trimResults().omitEmptyStrings()
   .splitToList(previous.getAbsoluteUri().toString());
 String relative = segments.subList(0, segments.size() - 1).stream()
   .collect(Collectors.joining("/", "/", ""));
 return Arrays.asList(relative + fname, fname)
   .stream()
   .map(resolver::apply)
   .filter(it -> it != null)
   .findFirst()
   .map(throwingFunction(it ->
     Try.with(it.toURL()::openStream)
       .apply(in -> new String(ByteStreams.toByteArray(in), StandardCharsets.UTF_8))
       .map(content -> Arrays.asList(new Import(it, it, content)))
       .get()
   ))
   .orElse(null);
}

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

/**
 * Functional try-with-resources with 3 inputs.
 *
 * @param r1 Input resource.
 * @param r2 Input resource.
 * @param r3 Input resource.
 * @param <R1> Resource type.
 * @param <R2> Resource type.
 * @param <R3> Resource type.
 * @return A resource handler.
 */
public final static <R1 extends AutoCloseable, R2 extends AutoCloseable, R3 extends AutoCloseable> ResourceHandler3<R1, R2, R3> of(
 R1 r1, R2 r2, R3 r3) {
 return with(() -> r1, () -> r2, () -> r3);
}

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

/**
 * Functional try-with-resources with 4 inputs.
 *
 * @param r1 Input resource.
 * @param r2 Input resource.
 * @param r3 Input resource.
 * @param r4 Input resource.
 * @param <R1> Resource type.
 * @param <R2> Resource type.
 * @param <R3> Resource type.
 * @param <R4> Resource type.
 * @return A resource handler.
 */
public final static <R1 extends AutoCloseable, R2 extends AutoCloseable, R3 extends AutoCloseable, R4 extends AutoCloseable> ResourceHandler4<R1, R2, R3, R4> of(
 R1 r1, R2 r2, R3 r3, R4 r4) {
 return with(() -> r1, () -> r2, () -> r3, () -> r4);
}

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

/**
 * Functional try-with-resources:
 *
 * <pre>{@code
 *  InputStream in = ...;
 *  OutputStream out = ...;
 *
 *  Try.of(in, out)
 *    .run((from, to) -> copy(from, to))
 *    .onFailure(Throwable::printStacktrace);
 *
 * }</pre>
 *
 * @param r1 Input resource.
 * @param r2 Input resource.
 * @param <R1> Resource type.
 * @param <R2> Resource type.
 * @return A resource handler.
 */
public final static <R1 extends AutoCloseable, R2 extends AutoCloseable> ResourceHandler2<R1, R2> of(
 R1 r1, R2 r2) {
 return with(() -> r1, () -> r2);
}

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

return with(() -> r1);

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

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

相关文章