001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.dbutils.handlers;
018    
019    import java.sql.ResultSet;
020    import java.sql.SQLException;
021    
022    import org.apache.commons.dbutils.ResultSetHandler;
023    import org.apache.commons.dbutils.RowProcessor;
024    
025    /**
026     * <code>ResultSetHandler</code> implementation that converts a
027     * <code>ResultSet</code> into a <code>List</code> of beans. This class is 
028     * thread safe.
029     * 
030     * @see org.apache.commons.dbutils.ResultSetHandler
031     */
032    public class BeanListHandler implements ResultSetHandler {
033    
034        /**
035         * The Class of beans produced by this handler.
036         */
037        private final Class type;
038    
039        /**
040         * The RowProcessor implementation to use when converting rows 
041         * into beans.
042         */
043        private final RowProcessor convert;
044    
045        /** 
046         * Creates a new instance of BeanListHandler.
047         * 
048         * @param type The Class that objects returned from <code>handle()</code>
049         * are created from.
050         */
051        public BeanListHandler(Class type) {
052            this(type, ArrayHandler.ROW_PROCESSOR);
053        }
054    
055        /** 
056         * Creates a new instance of BeanListHandler.
057         * 
058         * @param type The Class that objects returned from <code>handle()</code>
059         * are created from.
060         * @param convert The <code>RowProcessor</code> implementation 
061         * to use when converting rows into beans.
062         */
063        public BeanListHandler(Class type, RowProcessor convert) {
064            this.type = type;
065            this.convert = convert;
066        }
067    
068        /**
069         * Convert the whole <code>ResultSet</code> into a List of beans with
070         * the <code>Class</code> given in the constructor.
071         * 
072         * @param rs The <code>ResultSet</code> to handle.
073         * 
074         * @return A List of beans, never <code>null</code>.
075         * 
076         * @throws SQLException if a database access error occurs
077         * @see org.apache.commons.dbutils.RowProcessor#toBeanList(ResultSet, Class)
078         */
079        public Object handle(ResultSet rs) throws SQLException {
080            return this.convert.toBeanList(rs, type);
081        }
082    }