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