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     */
017    package org.apache.commons.dbutils.handlers;
018    
019    import java.sql.ResultSet;
020    import java.sql.SQLException;
021    
022    import org.apache.commons.dbutils.ResultSetHandler;
023    import org.apache.commons.dbutils.RowProcessor;
024    
025    /**
026     * <code>ResultSetHandler</code> implementation that converts the first
027     * <code>ResultSet</code> row into a JavaBean. This class is thread safe.
028     *
029     * @param <T> the target bean type
030     * @see org.apache.commons.dbutils.ResultSetHandler
031     */
032    public class BeanHandler<T> implements ResultSetHandler<T> {
033    
034        /**
035         * The Class of beans produced by this handler.
036         */
037        private final Class<T> type;
038    
039        /**
040         * The RowProcessor implementation to use when converting rows
041         * into beans.
042         */
043        private final RowProcessor convert;
044    
045        /**
046         * Creates a new instance of BeanHandler.
047         *
048         * @param type The Class that objects returned from <code>handle()</code>
049         * are created from.
050         */
051        public BeanHandler(Class<T> type) {
052            this(type, ArrayHandler.ROW_PROCESSOR);
053        }
054    
055        /**
056         * Creates a new instance of BeanHandler.
057         *
058         * @param type The Class that objects returned from <code>handle()</code>
059         * are created from.
060         * @param convert The <code>RowProcessor</code> implementation
061         * to use when converting rows into beans.
062         */
063        public BeanHandler(Class<T> type, RowProcessor convert) {
064            this.type = type;
065            this.convert = convert;
066        }
067    
068        /**
069         * Convert the first row of the <code>ResultSet</code> into a bean with the
070         * <code>Class</code> given in the constructor.
071         * @param rs <code>ResultSet</code> to process.
072         * @return An initialized JavaBean or <code>null</code> if there were no
073         * rows in the <code>ResultSet</code>.
074         *
075         * @throws SQLException if a database access error occurs
076         * @see org.apache.commons.dbutils.ResultSetHandler#handle(java.sql.ResultSet)
077         */
078        public T handle(ResultSet rs) throws SQLException {
079            return rs.next() ? this.convert.toBean(rs, this.type) : null;
080        }
081    
082    }