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