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 */
017package org.apache.commons.dbutils.handlers;
018
019import java.sql.ResultSet;
020import java.sql.SQLException;
021import java.util.ArrayList;
022import java.util.List;
023
024import 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 */
033public 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    @Override
045    public List<T> handle(ResultSet rs) throws SQLException {
046        List<T> rows = new ArrayList<T>();
047        while (rs.next()) {
048            rows.add(this.handleRow(rs));
049        }
050        return rows;
051    }
052
053    /**
054     * Row handler. Method converts current row into some Java object.
055     *
056     * @param rs <code>ResultSet</code> to process.
057     * @return row processing result
058     * @throws SQLException error occurs
059     */
060    protected abstract T handleRow(ResultSet rs) throws SQLException;
061}