/** * Generate a resource and a Perhaps based on that resource and then * dispose that resource eagerly when the Perhaps terminates or the * downstream cancels the sequence. * @param <T> the value type * @param <R> the resource type * @param resourceSupplier the callback to get a resource for each subscriber * @param sourceSupplier the function that returns a Perhaps for the generated resource * @param disposer the consumer of the resource once the upstream terminates or the * downstream cancels * @return the new Perhaps instance */ public static <T, R> Perhaps<T> using(Callable<R> resourceSupplier, Function<? super R, ? extends Perhaps<? extends T>> sourceSupplier, Consumer<? super R> disposer) { return using(resourceSupplier, sourceSupplier, disposer, true); }
/** * Generate a resource and a Perhaps based on that resource and then * dispose that resource eagerly when the Perhaps terminates or the * downstream cancels the sequence. * @param <T> the value type * @param <R> the resource type * @param resourceSupplier the callback to get a resource for each subscriber * @param sourceSupplier the function that returns a Perhaps for the generated resource * @param disposer the consumer of the resource once the upstream terminates or the * downstream cancels * @return the new Perhaps instance */ public static <T, R> Perhaps<T> using(Callable<R> resourceSupplier, Function<? super R, ? extends Perhaps<? extends T>> sourceSupplier, Consumer<? super R> disposer) { return using(resourceSupplier, sourceSupplier, disposer, true); }
@Test public void usingEagerError() { Perhaps.using(Functions.justCallable(1), Functions.justFunction(Perhaps.error(new IOException())), this) .test() .assertFailure(IOException.class); assertEquals(1, count); }
@Test public void usingNonEager() { Perhaps.using(Functions.justCallable(1), Functions.justFunction(Perhaps.just(1)), this, false) .test() .assertResult(1); assertEquals(1, count); }
@Test public void usingEager() { Perhaps.using(Functions.justCallable(1), Functions.justFunction(Perhaps.just(1)), this) .test() .assertResult(1); assertEquals(1, count); }