@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"); Resource<F, ResultSet> flatMap = makeResource(later(dataSource::getConnection)) .flatMap(conn -> makeResource(later(() -> conn.prepareStatement("sql")))) .flatMap(stmt -> makeResource(later(() -> stmt.executeQuery()))); Kind<F, String> use = flatMap.use(rs -> later(() -> rs.getString(0))); assertEquals("result", run(use)); InOrder inOrder = inOrder(resultSet, statement, connection); inOrder.verify(resultSet).close(); inOrder.verify(statement).close(); inOrder.verify(connection).close(); }
public <R> Resource<F, R> flatMap(Function1<? super T, ? extends Resource<F, ? extends R>> mapper) { return new Resource<>(monad, monad.flatMap(resource, t -> monad.map(mapper.andThen(ResourceOf::narrowK).apply(t.get1()).resource, r -> Tuple.of(r.get1(), (Consumer1<R>) ignore -> releaseAndThen(t, r))))); }
Resource implements ResourceOf<F, T> { public <R> Resource<F, R> flatMap(Function1<? super T, ? extends Resource<F, ? extends R>> mapper) { return new Resource<>(monad, monad.flatMap(resource, t -> monad.map(mapper.andThen(ResourceOf::narrowK).apply(t.get1()).resource, r -> Tuple.of(r.get1(), (Consumer1<R>) ignore -> releaseAndThen(t, r))))); } }
Resource implements ResourceOf<F, T> { public <R> Resource<F, R> flatMap(Function1<? super T, ? extends Resource<F, ? extends R>> mapper) { return new Resource<>(monad, monad.flatMap(resource, t -> monad.map(mapper.andThen(ResourceOf::narrowK).apply(t.get1()).resource, r -> Tuple.of(r.get1(), (Consumer1<R>) ignore -> releaseAndThen(t, r))))); } protected  Resource(MonadDefer<F> monad, Kind<F, Tuple2<T, Consumer1<? super T>>> resource); }
Resource implements ResourceOf<F, T> { public <R> Resource<F, R> flatMap(Function1<? super T, ? extends Resource<F, ? extends R>> mapper) { return new Resource<>(monad, monad.flatMap(resource, t -> monad.map(mapper.andThen(ResourceOf::narrowK).apply(t.get1()).resource, r -> Tuple.of(r.get1(), (Consumer1<R>) ignore -> releaseAndThen(t, r))))); } protected  Resource(MonadDefer<F> monad, Kind<F, Tuple2<T, Consumer1<? super T>>> resource); Resource<F, R> map(Function1<? super T, ? extends R> mapper); Resource<F, R> flatMap(Function1<? super T, ? extends Resource<F, ? extends R>> mapper); Kind<F, R> use(Function1<? super T, ? extends Kind<F, ? extends R>> use); Resource<F, Tuple2<T, R>> combine(Resource<F, ? extends R> other); static Resource<F, T> pure(
      MonadDefer<F> monad, Kind<F, ? extends T> acquire); static Resource<F, T> from(
      MonadDefer<F> monad, Kind<F, ? extends T> acquire, Consumer1<? super T> release); static Resource<F, T> from(
      MonadDefer<F> monad, Kind<F, ? extends T> acquire); }
Resource implements ResourceOf<F, T> { public <R> Resource<F, R> flatMap(Function1<? super T, ? extends Resource<F, ? extends R>> mapper) { return new Resource<>(monad, monad.flatMap(resource, t -> monad.map(mapper.andThen(ResourceOf::narrowK).apply(t.get1()).resource, r -> Tuple.of(r.get1(), (Consumer1<R>) ignore -> releaseAndThen(t, r))))); } protected  Resource(MonadDefer<F> monad, Kind<F, Tuple2<T, Consumer1<? super T>>> resource); Resource<F, R> map(Function1<? super T, ? extends R> mapper); Resource<F, R> flatMap(Function1<? super T, ? extends Resource<F, ? extends R>> mapper); Kind<F, R> use(Function1<? super T, ? extends Kind<F, ? extends R>> use); Resource<F, Tuple2<T, R>> combine(Resource<F, ? extends R> other); static Resource<F, T> pure(
      MonadDefer<F> monad, Kind<F, ? extends T> acquire); static Resource<F, T> from(
      MonadDefer<F> monad, Kind<F, ? extends T> acquire, Consumer1<? super T> release); static Resource<F, T> from(
      MonadDefer<F> monad, Kind<F, ? extends T> acquire);  }