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