/** * Functional try-with-resources: * * <pre>{@code * byte[] content = Try.with(() -> newInputStream()) * .apply(in -> read(in)) * .get(); * * }</pre> * * Jdbc example: * * <pre>{@code * Try.with(() -> newConnection()) * .map(c -> c.preparedStatement("...")) * .map(stt -> stt.executeQuery()) * .apply(rs-> { * return res.getString("column"); * }) * .get(); * * }</pre> * * @param r1 Input resource. * @param <R> Resource type. * @return A resource handler. */ public final static <R extends AutoCloseable> ResourceHandler<R> with(Throwing.Supplier<R> r1) { return new ResourceHandler<>(r1); }
/** * Map the resource to a new closeable resource. * * @param fn Mapper. * @param <V> New resource type. * @return A new resource handler. */ public <V extends AutoCloseable> ResourceHandler<V> map(Throwing.Function<R, V> fn) { return new ResourceHandler<>(new ProxyCloseable<>(this.r, fn)); }