AbstractKeyedHandler.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.HashMap;
  21. import java.util.Map;

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

  23. /**
  24.  * <p>
  25.  * {@code ResultSetHandler} implementation that returns a Map.
  26.  * {@code ResultSet} rows are converted into objects (Vs) which are then stored
  27.  * in a Map under the given keys (Ks).
  28.  * </p>
  29.  *
  30.  * @param <K> the type of keys maintained by the returned map
  31.  * @param <V> the type of mapped values
  32.  * @see org.apache.commons.dbutils.ResultSetHandler
  33.  * @since 1.3
  34.  */
  35. public abstract class AbstractKeyedHandler<K, V> implements ResultSetHandler<Map<K, V>> {

  36.     /**
  37.      * This factory method is called by {@code handle()} to retrieve the
  38.      * key value from the current {@code ResultSet} row.
  39.      * @param resultSet ResultSet to create a key from
  40.      * @return K from the configured key column name/index
  41.      * @throws SQLException if a database access error occurs
  42.      */
  43.     protected abstract K createKey(ResultSet resultSet) throws SQLException;

  44.     /**
  45.      * This factory method is called by {@code handle()} to create the Map
  46.      * to store records in.  This implementation returns a {@code HashMap}
  47.      * instance.
  48.      *
  49.      * @return Map to store records in
  50.      */
  51.     protected Map<K, V> createMap() {
  52.         return new HashMap<>();
  53.     }

  54.     /**
  55.      * This factory method is called by {@code handle()} to store the
  56.      * current {@code ResultSet} row in some object.
  57.      * @param resultSet ResultSet to create a row from
  58.      * @return V object created from the current row
  59.      * @throws SQLException if a database access error occurs
  60.      */
  61.     protected abstract V createRow(ResultSet resultSet) throws SQLException;

  62.     /**
  63.      * Convert each row's columns into a Map and store then
  64.      * in a {@code Map} under {@code ResultSet.getObject(key)} key.
  65.      * @param resultSet {@code ResultSet} to process.
  66.      * @return A {@code Map}, never {@code null}.
  67.      * @throws SQLException if a database access error occurs
  68.      * @see org.apache.commons.dbutils.ResultSetHandler#handle(java.sql.ResultSet)
  69.      */
  70.     @Override
  71.     public Map<K, V> handle(final ResultSet resultSet) throws SQLException {
  72.         final Map<K, V> result = createMap();
  73.         while (resultSet.next()) {
  74.             result.put(createKey(resultSet), createRow(resultSet));
  75.         }
  76.         return result;
  77.     }

  78. }