Index: src/java/org/apache/commons/dbutils/handlers/ArrayListHandler.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/dbutils/src/java/org/apache/commons/dbutils/handlers/ArrayListHandler.java,v retrieving revision 1.4 diff -u -r1.4 ArrayListHandler.java --- src/java/org/apache/commons/dbutils/handlers/ArrayListHandler.java 28 Feb 2004 00:12:22 -0000 1.4 +++ src/java/org/apache/commons/dbutils/handlers/ArrayListHandler.java 8 Nov 2004 21:35:44 -0000 @@ -17,8 +17,6 @@ import java.sql.ResultSet; import java.sql.SQLException; -import java.util.ArrayList; -import java.util.List; import org.apache.commons.dbutils.ResultSetHandler; import org.apache.commons.dbutils.RowProcessor; @@ -30,7 +28,7 @@ * * @see ResultSetHandler */ -public class ArrayListHandler implements ResultSetHandler { +public class ArrayListHandler extends GenericListHandler { /** * The RowProcessor implementation to use when converting rows @@ -57,26 +55,17 @@ this.convert = convert; } + /** - * Convert each row's columns into an Object[] and store them - * in a List in the same order they are returned from the - * ResultSet.next() method. - * - * @return A List of Object[]s, never - * null. - * - * @throws SQLException - * @see org.apache.commons.dbutils.ResultSetHandler#handle(java.sql.ResultSet) - */ - public Object handle(ResultSet rs) throws SQLException { - - List result = new ArrayList(); - - while (rs.next()) { - result.add(this.convert.toArray(rs)); - } - - return result; - } + * Convert row's columns into an Object[]. + * + * @return Object[], never null. + * + * @throws SQLException + * @see org.apache.commons.dbutils.handlers.GenericListHandler#handle(ResultSet) + */ + protected Object handleRow(ResultSet rs) throws SQLException { + return this.convert.toArray(rs); + } } Index: src/java/org/apache/commons/dbutils/handlers/BeanListHandler.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/dbutils/src/java/org/apache/commons/dbutils/handlers/BeanListHandler.java,v retrieving revision 1.4 diff -u -r1.4 BeanListHandler.java --- src/java/org/apache/commons/dbutils/handlers/BeanListHandler.java 28 Feb 2004 00:12:22 -0000 1.4 +++ src/java/org/apache/commons/dbutils/handlers/BeanListHandler.java 8 Nov 2004 21:35:44 -0000 @@ -28,7 +28,7 @@ * * @see ResultSetHandler */ -public class BeanListHandler implements ResultSetHandler { +public class BeanListHandler extends GenericListHandler { /** * The Class of beans produced by this handler. @@ -65,17 +65,16 @@ } /** - * Convert the ResultSet rows into a List of - * beans with the Class given in the constructor. + * Convert the ResultSet row into a bean with + * the Class given in the constructor. * - * @return A List of beans (one for each row), never - * null. + * @return A bean, never null. * * @throws SQLException - * @see org.apache.commons.dbutils.ResultSetHandler#handle(java.sql.ResultSet) + * @see org.apache.commons.dbutils.handlers.GenericListHandler#handle(ResultSet) */ - public Object handle(ResultSet rs) throws SQLException { - return this.convert.toBeanList(rs, type); + protected Object handleRow(ResultSet rs) throws SQLException { + return this.convert.toBean(rs, type); } } Index: src/java/org/apache/commons/dbutils/handlers/ColumnListHandler.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/dbutils/src/java/org/apache/commons/dbutils/handlers/ColumnListHandler.java,v retrieving revision 1.1 diff -u -r1.1 ColumnListHandler.java --- src/java/org/apache/commons/dbutils/handlers/ColumnListHandler.java 9 Mar 2004 03:05:51 -0000 1.1 +++ src/java/org/apache/commons/dbutils/handlers/ColumnListHandler.java 8 Nov 2004 21:35:45 -0000 @@ -18,8 +18,6 @@ import java.sql.ResultSet; import java.sql.SQLException; -import java.util.ArrayList; -import java.util.List; import org.apache.commons.dbutils.ResultSetHandler; @@ -31,7 +29,7 @@ * @see ResultSetHandler * @since DbUtils 1.1 */ -public class ColumnListHandler implements ResultSetHandler { +public class ColumnListHandler extends GenericListHandler { /** * The column number to retrieve. @@ -73,28 +71,20 @@ } /** - * Returns one ResultSet column as a List of - * Objects. The elements are added to the List via - * the ResultSet.getObject() method. + * Returns one ResultSet column value as Object. * - * @return A List of Objects, never - * null. + * @return Object, never null. * * @throws SQLException * - * @see org.apache.commons.dbutils.ResultSetHandler#handle(java.sql.ResultSet) + * @see org.apache.commons.dbutils.handlers.GenericListHandler#handle(ResultSet) */ - public Object handle(ResultSet rs) throws SQLException { - - List result = new ArrayList(); - - while (rs.next()) { - if (this.columnName == null) { - result.add(rs.getObject(this.columnIndex)); - } else { - result.add(rs.getObject(this.columnName)); - } + protected Object handleRow(ResultSet rs) throws SQLException { + if (this.columnName == null) { + return rs.getObject(this.columnIndex); + } else { + return rs.getObject(this.columnName); } - return result; - } + } + } Index: src/java/org/apache/commons/dbutils/handlers/MapListHandler.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/dbutils/src/java/org/apache/commons/dbutils/handlers/MapListHandler.java,v retrieving revision 1.4 diff -u -r1.4 MapListHandler.java --- src/java/org/apache/commons/dbutils/handlers/MapListHandler.java 28 Feb 2004 00:12:22 -0000 1.4 +++ src/java/org/apache/commons/dbutils/handlers/MapListHandler.java 8 Nov 2004 21:35:45 -0000 @@ -17,8 +17,6 @@ import java.sql.ResultSet; import java.sql.SQLException; -import java.util.ArrayList; -import java.util.List; import org.apache.commons.dbutils.ResultSetHandler; import org.apache.commons.dbutils.RowProcessor; @@ -30,7 +28,7 @@ * * @see ResultSetHandler */ -public class MapListHandler implements ResultSetHandler { +public class MapListHandler extends GenericListHandler { /** * The RowProcessor implementation to use when converting rows @@ -58,24 +56,16 @@ } /** - * Converts the ResultSet rows into a List of - * Map objects. + * Converts the ResultSet row into a Map object. * - * @return A List of Maps, never null. + * @return A Map, never null. * * @throws SQLException * - * @see org.apache.commons.dbutils.ResultSetHandler#handle(java.sql.ResultSet) + * @see org.apache.commons.dbutils.handlers.GenericListHandler#handle(ResultSet) */ - public Object handle(ResultSet rs) throws SQLException { - - List results = new ArrayList(); - - while (rs.next()) { - results.add(this.convert.toMap(rs)); - } - - return results; + protected Object handleRow(ResultSet rs) throws SQLException { + return this.convert.toMap(rs); } } Index: src/java/org/apache/commons/dbutils/handlers/GenericListHandler.java =================================================================== RCS file: src/java/org/apache/commons/dbutils/handlers/GenericListHandler.java diff -N src/java/org/apache/commons/dbutils/handlers/GenericListHandler.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/java/org/apache/commons/dbutils/handlers/GenericListHandler.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,54 @@ +/* + * Copyright 2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.dbutils.handlers; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.dbutils.ResultSetHandler; + +/** + * Abstract class that simplify development of ResultSetHandler + * classes that convert ResultSet into List. + */ +abstract class GenericListHandler implements ResultSetHandler { + /** + * Whole ResultSet handler. It produce List as + * result. To convert individual rows into Java objects it uses + * handleRow(ResultSet) method. + * + * @see #handleRow(ResultSet) + */ + public Object handle(ResultSet rs) throws SQLException { + List rows = new ArrayList(); + while (rs.next()) { + rows.add(this.handleRow(rs)); + } + return rows; + } + + /** + * Row handler. Method converts current row into some Java object. + * + * @param rs ResultSet to process. + * @return row processing result + * @throws SQLException error occurs + */ + protected abstract Object handleRow(ResultSet rs) throws SQLException; +}