ColumnListHandler.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. /**
  21.  * {@code ResultSetHandler} implementation that converts one
  22.  * {@code ResultSet} column into a {@code List} of
  23.  * {@code Object}s. This class is thread safe.
  24.  *
  25.  * @param <T> The type of the column.
  26.  * @see org.apache.commons.dbutils.ResultSetHandler
  27.  * @since 1.1
  28.  */
  29. public class ColumnListHandler<T> extends AbstractListHandler<T> {

  30.     /**
  31.      * The column number to retrieve.
  32.      */
  33.     private final int columnIndex;

  34.     /**
  35.      * The column name to retrieve.  Either columnName or columnIndex
  36.      * will be used but never both.
  37.      */
  38.     private final String columnName;

  39.     /**
  40.      * Creates a new instance of ColumnListHandler.  The first column of each
  41.      * row will be returned from {@code handle()}.
  42.      */
  43.     public ColumnListHandler() {
  44.         this(1, null);
  45.     }

  46.     /**
  47.      * Creates a new instance of ColumnListHandler.
  48.      *
  49.      * @param columnIndex The index of the column to retrieve from the
  50.      * {@code ResultSet}.
  51.      */
  52.     public ColumnListHandler(final int columnIndex) {
  53.         this(columnIndex, null);
  54.     }

  55.     /** Private Helper
  56.      * @param columnIndex The index of the column to retrieve from the
  57.      * {@code ResultSet}.
  58.      * @param columnName The name of the column to retrieve from the
  59.      * {@code ResultSet}.
  60.      */
  61.     private ColumnListHandler(final int columnIndex, final String columnName) {
  62.         this.columnIndex = columnIndex;
  63.         this.columnName = columnName;
  64.     }

  65.     /**
  66.      * Creates a new instance of ColumnListHandler.
  67.      *
  68.      * @param columnName The name of the column to retrieve from the
  69.      * {@code ResultSet}.
  70.      */
  71.     public ColumnListHandler(final String columnName) {
  72.         this(1, columnName);
  73.     }

  74.     /**
  75.      * Returns one {@code ResultSet} column value as {@code Object}.
  76.      * @param resultSet {@code ResultSet} to process.
  77.      * @return {@code Object}, never {@code null}.
  78.      *
  79.      * @throws SQLException if a database access error occurs
  80.      * @throws ClassCastException if the class datatype does not match the column type
  81.      *
  82.      * @see org.apache.commons.dbutils.handlers.AbstractListHandler#handle(ResultSet)
  83.      */
  84.     // We assume that the user has picked the correct type to match the column
  85.     // so getObject will return the appropriate type and the cast will succeed.
  86.     @SuppressWarnings("unchecked")
  87.     @Override
  88.     protected T handleRow(final ResultSet resultSet) throws SQLException {
  89.         if (this.columnName == null) {
  90.             return (T) resultSet.getObject(this.columnIndex);
  91.         }
  92.         return (T) resultSet.getObject(this.columnName);
  93.    }

  94. }