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.List;
022
023import org.apache.commons.dbutils.ResultSetHandler;
024import org.apache.commons.dbutils.RowProcessor;
025
026/**
027 * <code>ResultSetHandler</code> implementation that converts a
028 * <code>ResultSet</code> into a <code>List</code> of beans. This class is
029 * thread safe.
030 *
031 * @param <T> the target bean type
032 * @see org.apache.commons.dbutils.ResultSetHandler
033 */
034public class BeanListHandler<T> implements ResultSetHandler<List<T>> {
035
036    /**
037     * The Class of beans produced by this handler.
038     */
039    private final Class<T> type;
040
041    /**
042     * The RowProcessor implementation to use when converting rows
043     * into beans.
044     */
045    private final RowProcessor convert;
046
047    /**
048     * Creates a new instance of BeanListHandler.
049     *
050     * @param type The Class that objects returned from <code>handle()</code>
051     * are created from.
052     */
053    public BeanListHandler(Class<T> type) {
054        this(type, ArrayHandler.ROW_PROCESSOR);
055    }
056
057    /**
058     * Creates a new instance of BeanListHandler.
059     *
060     * @param type The Class that objects returned from <code>handle()</code>
061     * are created from.
062     * @param convert The <code>RowProcessor</code> implementation
063     * to use when converting rows into beans.
064     */
065    public BeanListHandler(Class<T> type, RowProcessor convert) {
066        this.type = type;
067        this.convert = convert;
068    }
069
070    /**
071     * Convert the whole <code>ResultSet</code> into a List of beans with
072     * the <code>Class</code> given in the constructor.
073     *
074     * @param rs The <code>ResultSet</code> to handle.
075     *
076     * @return A List of beans, never <code>null</code>.
077     *
078     * @throws SQLException if a database access error occurs
079     * @see org.apache.commons.dbutils.RowProcessor#toBeanList(ResultSet, Class)
080     */
081    @Override
082    public List<T> handle(ResultSet rs) throws SQLException {
083        return this.convert.toBeanList(rs, type);
084    }
085}