001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.dbutils.handlers;
018
019import java.sql.ResultSet;
020import java.sql.SQLException;
021import java.util.HashMap;
022import java.util.Map;
023
024import org.apache.commons.dbutils.ResultSetHandler;
025
026/**
027 * <p>
028 * {@code ResultSetHandler} implementation that returns a Map.
029 * {@code ResultSet} rows are converted into objects (Vs) which are then stored
030 * in a Map under the given keys (Ks).
031 * </p>
032 *
033 * @param <K> the type of keys maintained by the returned map
034 * @param <V> the type of mapped values
035 * @see org.apache.commons.dbutils.ResultSetHandler
036 * @since 1.3
037 */
038public abstract class AbstractKeyedHandler<K, V> implements ResultSetHandler<Map<K, V>> {
039
040    /**
041     * This factory method is called by {@code handle()} to retrieve the
042     * key value from the current {@code ResultSet} row.
043     * @param resultSet ResultSet to create a key from
044     * @return K from the configured key column name/index
045     * @throws SQLException if a database access error occurs
046     */
047    protected abstract K createKey(ResultSet resultSet) throws SQLException;
048
049    /**
050     * This factory method is called by {@code handle()} to create the Map
051     * to store records in.  This implementation returns a {@code HashMap}
052     * instance.
053     *
054     * @return Map to store records in
055     */
056    protected Map<K, V> createMap() {
057        return new HashMap<>();
058    }
059
060    /**
061     * This factory method is called by {@code handle()} to store the
062     * current {@code ResultSet} row in some object.
063     * @param resultSet ResultSet to create a row from
064     * @return V object created from the current row
065     * @throws SQLException if a database access error occurs
066     */
067    protected abstract V createRow(ResultSet resultSet) throws SQLException;
068
069    /**
070     * Convert each row's columns into a Map and store then
071     * in a {@code Map} under {@code ResultSet.getObject(key)} key.
072     * @param resultSet {@code ResultSet} to process.
073     * @return A {@code Map}, never {@code null}.
074     * @throws SQLException if a database access error occurs
075     * @see org.apache.commons.dbutils.ResultSetHandler#handle(java.sql.ResultSet)
076     */
077    @Override
078    public Map<K, V> handle(final ResultSet resultSet) throws SQLException {
079        final Map<K, V> result = createMap();
080        while (resultSet.next()) {
081            result.put(createKey(resultSet), createRow(resultSet));
082        }
083        return result;
084    }
085
086}