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    import java.util.ArrayList;
022    import java.util.List;
023    
024    import org.apache.commons.dbutils.ResultSetHandler;
025    
026    /**
027     * Abstract class that simplify development of <code>ResultSetHandler</code>
028     * classes that convert <code>ResultSet</code> into <code>List</code>.
029     *
030     * @see org.apache.commons.dbutils.ResultSetHandler
031     */
032    public abstract class AbstractListHandler<T> implements ResultSetHandler<List<T>> {
033        /**
034         * Whole <code>ResultSet</code> handler. It produce <code>List</code> as
035         * result. To convert individual rows into Java objects it uses
036         * <code>handleRow(ResultSet)</code> method.
037         *
038         * @see #handleRow(ResultSet)
039         * @param rs <code>ResultSet</code> to process.
040         * @return a list of all rows in the result set
041         * @throws SQLException error occurs
042         */
043        public List<T> handle(ResultSet rs) throws SQLException {
044            List<T> rows = new ArrayList<T>();
045            while (rs.next()) {
046                rows.add(this.handleRow(rs));
047            }
048            return rows;
049        }
050        
051        /**
052         * Row handler. Method converts current row into some Java object.
053         *
054         * @param rs <code>ResultSet</code> to process.
055         * @return row processing result
056         * @throws SQLException error occurs
057         */
058        protected abstract T handleRow(ResultSet rs) throws SQLException;
059    }