// Path: dalesbred/src/main/java/org/dalesbred/internal/jdbc/ResultSetUtils.java
// public final class ResultSetUtils {
// 
//     private ResultSetUtils() { }
// 
//     public static @NotNull NamedTypeList getTypes(@NotNull ResultSetMetaData metaData) throws SQLException {
//         int columns = metaData.getColumnCount();
// 
//         NamedTypeList.Builder result = NamedTypeList.builder(columns);
// 
//         for (int i = 0; i < columns; i++)
//             result.add(metaData.getColumnLabel(i+1), getColumnType(metaData, i + 1));
// 
//         return result.build();
//     }
// 
//     public static @NotNull Type getColumnType(@NotNull ResultSetMetaData metaData, int column) throws SQLException {
//         String className = metaData.getColumnClassName(column);
//         try {
//             return Class.forName(className);
//         } catch (ClassNotFoundException e) {
//             throw new DatabaseException("Could not find class '" + className + "' specified by ResultSet.", e);
//         }
//     }
// }
// 
// Path: dalesbred/src/main/java/org/dalesbred/internal/utils/Primitives.java
// public final class Primitives {
// 
//     private Primitives() { }
// 
//     /**
//      * Returns the corresponding wrapper type for a primitive type, or the type itself if it is not a primitive type.
//      */
//     public static @NotNull <T> Class<T> wrap(@NotNull Class<T> type) {
//         Class<?> result =
//                (type == boolean.class) ? Boolean.class
//              : (type == byte.class)    ? Byte.class
//              : (type == char.class)    ? Character.class
//              : (type == short.class)   ? Short.class
//              : (type == int.class)     ? Integer.class
//              : (type == long.class)    ? Long.class
//              : (type == float.class)   ? Float.class
//              : (type == double.class)  ? Double.class
//              : type;
// 
//         @SuppressWarnings("unchecked")
//         Class<T> casted = (Class<T>) result;
//         return casted;
//     }
// 
//     public static @NotNull Type wrap(@NotNull Type type) {
//         if (type instanceof Class<?>)
//             return wrap((Class<?>) type);
//         else
//             return type;
//     }
// 
//     /**
//      * Returns the corresponding primitive type for a wrapper type, or the type itself if it is not a wrapper.
//      */
//     public static @NotNull <T> Class<T> unwrap(@NotNull Class<T> type) {
//         Class<?> result =
//                (type == Boolean.class)   ? boolean.class
//              : (type == Byte.class)      ? byte.class
//              : (type == Character.class) ? char.class
//              : (type == Short.class)     ? short.class
//              : (type == Integer.class)   ? int.class
//              : (type == Long.class)      ? long.class
//              : (type == Float.class)     ? float.class
//              : (type == Double.class)    ? double.class
//              : type;
// 
//         @SuppressWarnings("unchecked")
//         Class<T> casted = (Class<T>) result;
//         return casted;
//     }
// 
//     public static @NotNull Object[] arrayAsObjectArray(@NotNull Object o) {
//         Class<?> type = o.getClass();
//         if (!type.isArray()) throw new IllegalArgumentException("not an array: " + o);
// 
//         if (o instanceof Object[])
//             return (Object[]) o;
// 
//         int length = Array.getLength(o);
//         Object[] result = (Object[]) Array.newInstance(wrap(type.getComponentType()), length);
// 
//         for (int i = 0; i < length; i++)
//             result[i] = Array.get(o, i);
// 
//         return result;
//     }
// }
// 
// Path: dalesbred/src/main/java/org/dalesbred/result/ResultSetProcessor.java
// @FunctionalInterface
// public interface ResultSetProcessor<T> {
//     T process(@NotNull ResultSet resultSet) throws SQLException;
// }
// 
// Path: dalesbred/src/main/java/org/dalesbred/result/UnexpectedResultException.java
// public class UnexpectedResultException extends DatabaseException {
//     public UnexpectedResultException(@NotNull String message) {
//         super(message);
//     }
// }


import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.Map;
import static java.util.Objects.requireNonNull;
import org.dalesbred.internal.instantiation.*;
import org.dalesbred.internal.jdbc.ResultSetUtils;
import org.dalesbred.internal.utils.Primitives;
import org.dalesbred.result.ResultSetProcessor;
import org.dalesbred.result.UnexpectedResultException;
import org.jetbrains.annotations.NotNull;
import java.sql.ResultSet;
/*
 * Copyright (c) 2017 Evident Solutions Oy
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package org.dalesbred.internal.result;




/**
 * ResultSetProcessor that expects results with two columns and creates map from them.
 *
 * <p>This processor updates the map in order in which the rows are read. Therefore, if
 * the keys of the result are not distinct, the result contains the last binding of given key.
 */
public final class MapResultSetProcessor<K,V> implements ResultSetProcessor<Map<K,V>> {

    private final @NotNull Class<K> keyType;

    private final @NotNull Class<V> valueType;

    private final @NotNull InstantiatorProvider instantiatorRegistry;

    public MapResultSetProcessor(@NotNull Class<K> keyType,
                                 @NotNull Class<V> valueType,
                                 @NotNull InstantiatorProvider instantiatorRegistry) {
// Path: dalesbred/src/main/java/org/dalesbred/internal/jdbc/ResultSetUtils.java
// public final class ResultSetUtils {
// 
//     private ResultSetUtils() { }
// 
//     public static @NotNull NamedTypeList getTypes(@NotNull ResultSetMetaData metaData) throws SQLException {
//         int columns = metaData.getColumnCount();
// 
//         NamedTypeList.Builder result = NamedTypeList.builder(columns);
// 
//         for (int i = 0; i < columns; i++)
//             result.add(metaData.getColumnLabel(i+1), getColumnType(metaData, i + 1));
// 
//         return result.build();
//     }
// 
//     public static @NotNull Type getColumnType(@NotNull ResultSetMetaData metaData, int column) throws SQLException {
//         String className = metaData.getColumnClassName(column);
//         try {
//             return Class.forName(className);
//         } catch (ClassNotFoundException e) {
//             throw new DatabaseException("Could not find class '" + className + "' specified by ResultSet.", e);
//         }
//     }
// }
// 
// Path: dalesbred/src/main/java/org/dalesbred/internal/utils/Primitives.java
// public final class Primitives {
// 
//     private Primitives() { }
// 
//     /**
//      * Returns the corresponding wrapper type for a primitive type, or the type itself if it is not a primitive type.
//      */
//     public static @NotNull <T> Class<T> wrap(@NotNull Class<T> type) {
//         Class<?> result =
//                (type == boolean.class) ? Boolean.class
//              : (type == byte.class)    ? Byte.class
//              : (type == char.class)    ? Character.class
//              : (type == short.class)   ? Short.class
//              : (type == int.class)     ? Integer.class
//              : (type == long.class)    ? Long.class
//              : (type == float.class)   ? Float.class
//              : (type == double.class)  ? Double.class
//              : type;
// 
//         @SuppressWarnings("unchecked")
//         Class<T> casted = (Class<T>) result;
//         return casted;
//     }
// 
//     public static @NotNull Type wrap(@NotNull Type type) {
//         if (type instanceof Class<?>)
//             return wrap((Class<?>) type);
//         else
//             return type;
//     }
// 
//     /**
//      * Returns the corresponding primitive type for a wrapper type, or the type itself if it is not a wrapper.
//      */
//     public static @NotNull <T> Class<T> unwrap(@NotNull Class<T> type) {
//         Class<?> result =
//                (type == Boolean.class)   ? boolean.class
//              : (type == Byte.class)      ? byte.class
//              : (type == Character.class) ? char.class
//              : (type == Short.class)     ? short.class
//              : (type == Integer.class)   ? int.class
//              : (type == Long.class)      ? long.class
//              : (type == Float.class)     ? float.class
//              : (type == Double.class)    ? double.class
//              : type;
// 
//         @SuppressWarnings("unchecked")
//         Class<T> casted = (Class<T>) result;
//         return casted;
//     }
// 
//     public static @NotNull Object[] arrayAsObjectArray(@NotNull Object o) {
//         Class<?> type = o.getClass();
//         if (!type.isArray()) throw new IllegalArgumentException("not an array: " + o);
// 
//         if (o instanceof Object[])
//             return (Object[]) o;
// 
//         int length = Array.getLength(o);
//         Object[] result = (Object[]) Array.newInstance(wrap(type.getComponentType()), length);
// 
//         for (int i = 0; i < length; i++)
//             result[i] = Array.get(o, i);
// 
//         return result;
//     }
// }
// 
// Path: dalesbred/src/main/java/org/dalesbred/result/ResultSetProcessor.java
// @FunctionalInterface
// public interface ResultSetProcessor<T> {
//     T process(@NotNull ResultSet resultSet) throws SQLException;
// }
// 
// Path: dalesbred/src/main/java/org/dalesbred/result/UnexpectedResultException.java
// public class UnexpectedResultException extends DatabaseException {
//     public UnexpectedResultException(@NotNull String message) {
//         super(message);
//     }
// }


// Path: dalesbred/src/main/java/org/dalesbred/internal/result/MapResultSetProcessor.java
import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.Map;
import static java.util.Objects.requireNonNull;
import org.dalesbred.internal.instantiation.*;
import org.dalesbred.internal.jdbc.ResultSetUtils;
import org.dalesbred.internal.utils.Primitives;
import org.dalesbred.result.ResultSetProcessor;
import org.dalesbred.result.UnexpectedResultException;
import org.jetbrains.annotations.NotNull;
import java.sql.ResultSet;

/*
 * Copyright (c) 2017 Evident Solutions Oy
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package org.dalesbred.internal.result;




/**
 * ResultSetProcessor that expects results with two columns and creates map from them.
 *
 * <p>This processor updates the map in order in which the rows are read. Therefore, if
 * the keys of the result are not distinct, the result contains the last binding of given key.
 */
public final class MapResultSetProcessor<K,V> implements ResultSetProcessor<Map<K,V>> {

    private final @NotNull Class<K> keyType;

    private final @NotNull Class<V> valueType;

    private final @NotNull InstantiatorProvider instantiatorRegistry;

    public MapResultSetProcessor(@NotNull Class<K> keyType,
                                 @NotNull Class<V> valueType,
                                 @NotNull InstantiatorProvider instantiatorRegistry) {
        this.keyType = Primitives.wrap(requireNonNull(keyType));