001/*
002 * Copyright 2015-2022 Transmogrify LLC, 2022-2024 Revetware LLC.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.pyranid;
018
019import javax.annotation.Nonnull;
020import java.sql.ResultSet;
021import java.util.Optional;
022
023/**
024 * Contract for mapping a {@link ResultSet} row to a different type.
025 *
026 * @author <a href="https://www.revetkn.com">Mark Allen</a>
027 * @since 1.0.0
028 */
029@FunctionalInterface
030public interface ResultSetMapper {
031        /**
032         * Maps the current row of {@code resultSet} to the result class indicated by {@code statementContext}.
033         *
034         * @param <T>              result instance type token
035         * @param statementContext current SQL context
036         * @param resultSet        provides raw row data to pull from*
037         * @param resultSetRowType the type to which the {@link ResultSet} row should be marshaled
038         * @param instanceProvider instance-creation factory, used to instantiate {@code resultSetRowType} row objects
039         * @return an instance of the given {@code resultClass}
040         * @throws DatabaseException if an error occurs during mapping
041         */
042        @Nonnull
043        <T> Optional<T> map(@Nonnull StatementContext<T> statementContext,
044                                                                                        @Nonnull ResultSet resultSet,
045                                                                                        @Nonnull Class<T> resultSetRowType,
046                                                                                        @Nonnull InstanceProvider instanceProvider);
047}