/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jdbi.v3.core.result;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.function.Supplier;

import org.jdbi.v3.core.statement.StatementContext;

/**
 * Produces a result from an executed {@link PreparedStatement}.
 *
 * @param <R> Result type
 */
@FunctionalInterface
public interface ResultProducer<R> {
    /**
     * Produces a statement result from a lazily supplied {@link PreparedStatement}. The statement is not executed until
     * {@code statementSupplier.get()} is invoked.
     * <p>
     * Implementors that call {@code statementSupplier.get()} must ensure that the statement context is closed before
     * returning, to ensure that database resources are freed:
     * <pre>
     * try {
     *     PreparedStatement statement = statementSupplier.get()
     *     // generate and return result from the statement
     * }
     * finally {
     *     ctx.close();
     * }
     * </pre>
     * <p>
     * Alternatively, implementors may return some intermediate result object (e.g. {@link ResultBearing} or
     * {@link ResultIterable}) without calling {@code statementSupplier.get()}, in which case the burden of closing
     * resources falls to whichever object ultimately does {@code get()} the statement.
     *
     * @param statementSupplier supplies a PreparedStatement, post-execution.
     * @param ctx               the statement context
     * @return an object of the type your caller expects
     * @throws SQLException if an error occurs while producing the result.
     */
    R produce(Supplier<PreparedStatement> statementSupplier, StatementContext ctx) throws SQLException;
}

@@ -1272,16 +1272,23 @@
  freed:%0A
+     *%0A
      * %3C
@@ -1303,11 +1303,168 @@
   * 
-try
+public R produce(Supplier&lt;PreparedStatement&gt; statementSupplier, StatementContext context) %7B%0A     *     ...%0A     *     try (StatementContext context = ctx)
  %7B%0A 
@@ -1464,24 +1464,28 @@
 tx) %7B%0A     *
+    
      Prepare
@@ -1533,24 +1533,28 @@
 get()%0A     *
+    
      // gene
@@ -1605,51 +1605,14 @@
    *
- %7D%0A
      
-* finally %7B%0A     *     ctx.close();
+%7D
 %0A   
@@ -1925,11 +1925,12 @@
 ely 
-doe
+call
 s %7B@
@@ -1938,29 +1938,443 @@
 ode 
-get()%7D the statement.
+statementSupplier.get()%7D.%0A     *%0A     * %3Cpre%3E%0A     * public R produce(Supplier&lt;PreparedStatement&gt; statementSupplier, StatementContext context) %7B%0A     *     ...%0A     *     // the implementation of SomeOtherClass.createResponse which may call statementSupplier.get()%0A     *     // is now responsible to close the StatementContext.%0A     *     return SomeOtherClass.createResponse(statementSupplier, ..., ctx);%0A     * %7D%0A     * %3C/pre%3E
 %0A   

Update ResultProducer javadoc