View Javadoc
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  
19  import java.sql.ResultSet;
20  import java.sql.SQLException;
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import org.apache.commons.dbutils.ResultSetHandler;
25  
26  /**
27   * <p>
28   * {@code ResultSetHandler} implementation that returns a Map.
29   * {@code ResultSet} rows are converted into objects (Vs) which are then stored
30   * in a Map under the given keys (Ks).
31   * </p>
32   *
33   * @param <K> the type of keys maintained by the returned map
34   * @param <V> the type of mapped values
35   * @see org.apache.commons.dbutils.ResultSetHandler
36   * @since 1.3
37   */
38  public abstract class AbstractKeyedHandler<K, V> implements ResultSetHandler<Map<K, V>> {
39  
40      /**
41       * This factory method is called by {@code handle()} to retrieve the
42       * key value from the current {@code ResultSet} row.
43       * @param resultSet ResultSet to create a key from
44       * @return K from the configured key column name/index
45       * @throws SQLException if a database access error occurs
46       */
47      protected abstract K createKey(ResultSet resultSet) throws SQLException;
48  
49      /**
50       * This factory method is called by {@code handle()} to create the Map
51       * to store records in.  This implementation returns a {@code HashMap}
52       * instance.
53       *
54       * @return Map to store records in
55       */
56      protected Map<K, V> createMap() {
57          return new HashMap<>();
58      }
59  
60      /**
61       * This factory method is called by {@code handle()} to store the
62       * current {@code ResultSet} row in some object.
63       * @param resultSet ResultSet to create a row from
64       * @return V object created from the current row
65       * @throws SQLException if a database access error occurs
66       */
67      protected abstract V createRow(ResultSet resultSet) throws SQLException;
68  
69      /**
70       * Convert each row's columns into a Map and store then
71       * in a {@code Map} under {@code ResultSet.getObject(key)} key.
72       * @param resultSet {@code ResultSet} to process.
73       * @return A {@code Map}, never {@code null}.
74       * @throws SQLException if a database access error occurs
75       * @see org.apache.commons.dbutils.ResultSetHandler#handle(java.sql.ResultSet)
76       */
77      @Override
78      public Map<K, V> handle(final ResultSet resultSet) throws SQLException {
79          final Map<K, V> result = createMap();
80          while (resultSet.next()) {
81              result.put(createKey(resultSet), createRow(resultSet));
82          }
83          return result;
84      }
85  
86  }