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.Map;
022
023import org.apache.commons.dbutils.RowProcessor;
024
025/**
026 * {@code ResultSetHandler} implementation that converts a
027 * {@code ResultSet} into a {@code List} of {@code Map}s.
028 * This class is thread safe.
029 *
030 * @see org.apache.commons.dbutils.ResultSetHandler
031 */
032public class MapListHandler extends AbstractListHandler<Map<String, Object>> {
033
034    /**
035     * The RowProcessor implementation to use when converting rows
036     * into Maps.
037     */
038    private final RowProcessor convert;
039
040    /**
041     * Creates a new instance of MapListHandler using a
042     * {@code BasicRowProcessor} for conversion.
043     */
044    public MapListHandler() {
045        this(ArrayHandler.ROW_PROCESSOR);
046    }
047
048    /**
049     * Creates a new instance of MapListHandler.
050     *
051     * @param convert The {@code RowProcessor} implementation
052     * to use when converting rows into Maps.
053     */
054    public MapListHandler(final RowProcessor convert) {
055        this.convert = convert;
056    }
057
058    /**
059     * Converts the {@code ResultSet} row into a {@code Map} object.
060     * @param resultSet {@code ResultSet} to process.
061     * @return A {@code Map}, never null.
062     *
063     * @throws SQLException if a database access error occurs
064     *
065     * @see org.apache.commons.dbutils.handlers.AbstractListHandler#handle(ResultSet)
066     */
067    @Override
068    protected Map<String, Object> handleRow(final ResultSet resultSet) throws SQLException {
069        return this.convert.toMap(resultSet);
070    }
071
072}