@Test public void flatMap(@Mock DataSource dataSource, @Mock Connection connection, @Mock PreparedStatement statement, @Mock ResultSet resultSet) throws SQLException { when(dataSource.getConnection()).thenReturn(connection); when(connection.prepareStatement("sql")).thenReturn(statement); when(statement.executeQuery()).thenReturn(resultSet); when(resultSet.getString(0)).thenReturn("result"); Managed<Nothing, Throwable, ResultSet> flatMap = Managed.<Nothing, Throwable, Connection>from(ZIO.task(dataSource::getConnection)) .flatMap(conn -> Managed.from(ZIO.task(() -> conn.prepareStatement("sql")))) .flatMap(stmt -> Managed.from(ZIO.task(() -> stmt.executeQuery()))); ZIO<Nothing, Throwable, String> use = flatMap.use(rs -> ZIO.task(() -> rs.getString(0))); assertEquals(right("result"), use.provide(nothing())); InOrder inOrder = inOrder(resultSet, statement, connection); inOrder.verify(resultSet).close(); inOrder.verify(statement).close(); inOrder.verify(connection).close(); }
public <B> Managed<R, E, B> flatMap(Function1<? super A, ? extends Managed<R, E, ? extends B>> mapper) { ZIO<R, E, Tuple2<B, Consumer1<? super B>>> result = resource.flatMap(t -> { Managed<R, E, B> apply = ManagedOf.narrowK(mapper.apply(t.get1())); return apply.resource.map(r -> r.map2(ignore -> releaseAndThen(t, r))); }); return new Managed<>(result); }
Managed implements ManagedOf<R, E, A> { public <B> Managed<R, E, B> flatMap(Function1<? super A, ? extends Managed<R, E, ? extends B>> mapper) { ZIO<R, E, Tuple2<B, Consumer1<? super B>>> result = resource.flatMap(t -> { Managed<R, E, B> apply = ManagedOf.narrowK(mapper.apply(t.get1())); return apply.resource.map(r -> r.map2(ignore -> releaseAndThen(t, r))); }); return new Managed<>(result); } }
Managed implements ManagedOf<R, E, A> { public <B> Managed<R, E, B> flatMap(Function1<? super A, ? extends Managed<R, E, ? extends B>> mapper) { ZIO<R, E, Tuple2<B, Consumer1<? super B>>> result = resource.flatMap(t -> { Managed<R, E, B> apply = ManagedOf.narrowK(mapper.apply(t.get1())); return apply.resource.map(r -> r.map2(ignore -> releaseAndThen(t, r))); }); return new Managed<>(result); } protected  Managed(ZIO<R, E, Tuple2<A, Consumer1<? super A>>> resource); }
Managed implements ManagedOf<R, E, A> { public <B> Managed<R, E, B> flatMap(Function1<? super A, ? extends Managed<R, E, ? extends B>> mapper) { ZIO<R, E, Tuple2<B, Consumer1<? super B>>> result = resource.flatMap(t -> { Managed<R, E, B> apply = ManagedOf.narrowK(mapper.apply(t.get1())); return apply.resource.map(r -> r.map2(ignore -> releaseAndThen(t, r))); }); return new Managed<>(result); } protected  Managed(ZIO<R, E, Tuple2<A, Consumer1<? super A>>> resource); Managed<R, E, B> map(Function1<? super A, ? extends B> mapper); Managed<R, F, A> mapError(Function1<? super E, ? extends F> mapper); Managed<R, E, B> flatMap(Function1<? super A, ? extends Managed<R, E, ? extends B>> mapper); Managed<R, F, A> flatMapError(Function1<? super E, ? extends Managed<R, F, ? extends A>> mapper); Managed<R, E, B> andThen(Managed<A, E, B> other); ZIO<R, E, B> use(Function1<? super A, ? extends ZIO<R, E, ? extends B>> use); Managed<R, Nothing, B> fold(
      Function1<? super E, ? extends B> mapError, Function1<? super A, ? extends B> mapper); Managed<R, Nothing, A> recover(Function1<? super E, ? extends A> mapError); Managed<R, E, A> orElse(Managed<R, E, ? extends A> other); Managed<R, F, B> foldM(
      Function1<? super E, ? extends Managed<R, F, ? extends B>> mapError, 
      Function1<? super A, ? extends Managed<R, F, ? extends B>> mapper); Managed<R, E, Tuple2<A, B>> combine(Managed<R, E, B> other); Managed<R, E, Either<A, B>> either(Managed<R, E, B> other); Managed<R, E, A> retry(); Managed<R, E, A> retry(int maxRetries); Managed<R, E, A> retry(Duration delay); Managed<R, E, A> retry(Duration delay, int maxRetries); Managed<R, E, A> retry(Schedule<R, E, B> schedule); Managed<R, E, Tuple2<Duration, A>> timed(); static Managed<R, E, A> pure(A resource); static Managed<R, E, A> pure(ZIO<R, E, ? extends A> resource); static Managed<R, E, A> from(ZIO<R, E, ? extends A> resource); static Managed<R, E, A> from(ZIO<R, E, ? extends A> resource, Consumer1<? super A> release); static Managed<R, E, A> from(Function1<? super R, ? extends A> mapper); static Managed<R, E, A> from(Function1<? super R, ? extends A> mapper, Consumer1<? super A> release); static Managed<R, E, A> fromM(Function1<? super R, ? extends ZIO<R, E, ? extends A>> mapper); static Managed<R, E, A> fromM(
      Function1<? super R, ? extends ZIO<R, E, ? extends A>> mapper, Consumer1<? super A> release); }
Managed implements ManagedOf<R, E, A> { public <B> Managed<R, E, B> flatMap(Function1<? super A, ? extends Managed<R, E, ? extends B>> mapper) { ZIO<R, E, Tuple2<B, Consumer1<? super B>>> result = resource.flatMap(t -> { Managed<R, E, B> apply = ManagedOf.narrowK(mapper.apply(t.get1())); return apply.resource.map(r -> r.map2(ignore -> releaseAndThen(t, r))); }); return new Managed<>(result); } protected  Managed(ZIO<R, E, Tuple2<A, Consumer1<? super A>>> resource); Managed<R, E, B> map(Function1<? super A, ? extends B> mapper); Managed<R, F, A> mapError(Function1<? super E, ? extends F> mapper); Managed<R, E, B> flatMap(Function1<? super A, ? extends Managed<R, E, ? extends B>> mapper); Managed<R, F, A> flatMapError(Function1<? super E, ? extends Managed<R, F, ? extends A>> mapper); Managed<R, E, B> andThen(Managed<A, E, B> other); ZIO<R, E, B> use(Function1<? super A, ? extends ZIO<R, E, ? extends B>> use); Managed<R, Nothing, B> fold(
      Function1<? super E, ? extends B> mapError, Function1<? super A, ? extends B> mapper); Managed<R, Nothing, A> recover(Function1<? super E, ? extends A> mapError); Managed<R, E, A> orElse(Managed<R, E, ? extends A> other); Managed<R, F, B> foldM(
      Function1<? super E, ? extends Managed<R, F, ? extends B>> mapError, 
      Function1<? super A, ? extends Managed<R, F, ? extends B>> mapper); Managed<R, E, Tuple2<A, B>> combine(Managed<R, E, B> other); Managed<R, E, Either<A, B>> either(Managed<R, E, B> other); Managed<R, E, A> retry(); Managed<R, E, A> retry(int maxRetries); Managed<R, E, A> retry(Duration delay); Managed<R, E, A> retry(Duration delay, int maxRetries); Managed<R, E, A> retry(Schedule<R, E, B> schedule); Managed<R, E, Tuple2<Duration, A>> timed(); static Managed<R, E, A> pure(A resource); static Managed<R, E, A> pure(ZIO<R, E, ? extends A> resource); static Managed<R, E, A> from(ZIO<R, E, ? extends A> resource); static Managed<R, E, A> from(ZIO<R, E, ? extends A> resource, Consumer1<? super A> release); static Managed<R, E, A> from(Function1<? super R, ? extends A> mapper); static Managed<R, E, A> from(Function1<? super R, ? extends A> mapper, Consumer1<? super A> release); static Managed<R, E, A> fromM(Function1<? super R, ? extends ZIO<R, E, ? extends A>> mapper); static Managed<R, E, A> fromM(
      Function1<? super R, ? extends ZIO<R, E, ? extends A>> mapper, Consumer1<? super A> release);  }