ArrayHandler.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 org.apache.commons.dbutils.BasicRowProcessor;
  21. import org.apache.commons.dbutils.ResultSetHandler;
  22. import org.apache.commons.dbutils.RowProcessor;

  23. /**
  24.  * {@code ResultSetHandler} implementation that converts a
  25.  * {@code ResultSet} into an {@code Object[]}. This class is
  26.  * thread safe.
  27.  *
  28.  * @see org.apache.commons.dbutils.ResultSetHandler
  29.  */
  30. public class ArrayHandler implements ResultSetHandler<Object[]> {

  31.     /**
  32.      * Singleton processor instance that handlers share to save memory.  Notice
  33.      * the default scoping to allow only classes in this package to use this
  34.      * instance.
  35.      */
  36.     static final RowProcessor ROW_PROCESSOR = new BasicRowProcessor();

  37.     /**
  38.      * An empty array to return when no more rows are available in the ResultSet.
  39.      */
  40.     private static final Object[] EMPTY_ARRAY = {};

  41.     /**
  42.      * The RowProcessor implementation to use when converting rows
  43.      * into arrays.
  44.      */
  45.     private final RowProcessor convert;

  46.     /**
  47.      * Creates a new instance of ArrayHandler using a
  48.      * {@code BasicRowProcessor} for conversion.
  49.      */
  50.     public ArrayHandler() {
  51.         this(ROW_PROCESSOR);
  52.     }

  53.     /**
  54.      * Creates a new instance of ArrayHandler.
  55.      *
  56.      * @param convert The {@code RowProcessor} implementation
  57.      * to use when converting rows into arrays.
  58.      */
  59.     public ArrayHandler(final RowProcessor convert) {
  60.         this.convert = convert;
  61.     }

  62.     /**
  63.      * Places the column values from the first row in an {@code Object[]}.
  64.      * @param resultSet {@code ResultSet} to process.
  65.      * @return An Object[]. If there are no rows in the {@code ResultSet}
  66.      * an empty array will be returned.
  67.      *
  68.      * @throws SQLException if a database access error occurs
  69.      * @see org.apache.commons.dbutils.ResultSetHandler#handle(java.sql.ResultSet)
  70.      */
  71.     @Override
  72.     public Object[] handle(final ResultSet resultSet) throws SQLException {
  73.         return resultSet.next() ? this.convert.toArray(resultSet) : EMPTY_ARRAY;
  74.     }

  75. }