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.ArrayList;
022 import java.util.List;
023
024 import 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 * @see org.apache.commons.dbutils.ResultSetHandler
031 */
032 public abstract class AbstractListHandler implements ResultSetHandler {
033 /**
034 * Whole <code>ResultSet</code> handler. It produce <code>List</code> as
035 * result. To convert individual rows into Java objects it uses
036 * <code>handleRow(ResultSet)</code> method.
037 *
038 * @see #handleRow(ResultSet)
039 */
040 public Object handle(ResultSet rs) throws SQLException {
041 List rows = new ArrayList();
042 while (rs.next()) {
043 rows.add(this.handleRow(rs));
044 }
045 return rows;
046 }
047
048 /**
049 * Row handler. Method converts current row into some Java object.
050 *
051 * @param rs <code>ResultSet</code> to process.
052 * @return row processing result
053 * @throws SQLException error occurs
054 */
055 protected abstract Object handleRow(ResultSet rs) throws SQLException;
056 }