    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}
    028 * classes that convert {@code ResultSet} into {@code List}.
    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    /**
    036     * Whole {@code ResultSet} handler. It produce {@code List} as
    037     * result. To convert individual rows into Java objects it uses
    038     * {@code handleRow(ResultSet)} method.
    039     *
    040     * @see #handleRow(ResultSet)
    041     * @param resultSet {@code ResultSet} to process.
    042     * @return a list of all rows in the result set
    043     * @throws SQLException error occurs
    044     */
    045    @Override
    046    public List<T> handle(final ResultSet resultSet) throws SQLException {
    047        final List<T> rows = new ArrayList<>();
    048        while (resultSet.next()) {
    049            rows.add(this.handleRow(resultSet));
    050        }
    051        return rows;
    052    }
    053
    054    /**
    055     * Row handler. Method converts current row into some Java object.
    056     *
    057     * @param resultSet {@code ResultSet} to process.
    058     * @return row processing result
    059     * @throws SQLException error occurs
    060     */
    061    protected abstract T handleRow(ResultSet resultSet) throws SQLException;
    062}