AbstractListHandler.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.dbutils.handlers;

  18. import java.sql.ResultSet;
  19. import java.sql.SQLException;
  20. import java.util.ArrayList;
  21. import java.util.List;

  22. import org.apache.commons.dbutils.ResultSetHandler;

  23. /**
  24.  * Abstract class that simplify development of {@code ResultSetHandler}
  25.  * classes that convert {@code ResultSet} into {@code List}.
  26.  *
  27.  * @param <T> the target List generic type
  28.  * @see org.apache.commons.dbutils.ResultSetHandler
  29.  */
  30. public abstract class AbstractListHandler<T> implements ResultSetHandler<List<T>> {

  31.     /**
  32.      * Whole {@code ResultSet} handler. It produce {@code List} as
  33.      * result. To convert individual rows into Java objects it uses
  34.      * {@code handleRow(ResultSet)} method.
  35.      *
  36.      * @see #handleRow(ResultSet)
  37.      * @param resultSet {@code ResultSet} to process.
  38.      * @return a list of all rows in the result set
  39.      * @throws SQLException error occurs
  40.      */
  41.     @Override
  42.     public List<T> handle(final ResultSet resultSet) throws SQLException {
  43.         final List<T> rows = new ArrayList<>();
  44.         while (resultSet.next()) {
  45.             rows.add(this.handleRow(resultSet));
  46.         }
  47.         return rows;
  48.     }

  49.     /**
  50.      * Row handler. Method converts current row into some Java object.
  51.      *
  52.      * @param resultSet {@code ResultSet} to process.
  53.      * @return row processing result
  54.      * @throws SQLException error occurs
  55.      */
  56.     protected abstract T handleRow(ResultSet resultSet) throws SQLException;
  57. }