// Path: dalesbred/src/main/java/org/dalesbred/internal/utils/TypeUtils.java
// public final class TypeUtils {
// 
//     private TypeUtils() { }
// 
//     public static @NotNull Class<?> rawType(@NotNull Type type) {
//         if (type instanceof Class<?>) {
//             return (Class<?>) type;
// 
//         } else if (type instanceof ParameterizedType) {
//             return rawType(((ParameterizedType) type).getRawType());
// 
//         } else if (type instanceof TypeVariable<?>) {
//             // We could return one of the bounds, but it won't work if there are multiple bounds.
//             // Therefore just return object.
//             return Object.class;
// 
//         } else if (type instanceof WildcardType) {
//             return rawType(((WildcardType) type).getUpperBounds()[0]);
// 
//         } else if (type instanceof GenericArrayType) {
//             return arrayType(rawType(((GenericArrayType) type).getGenericComponentType()));
// 
//         } else {
//             throw new IllegalArgumentException("unexpected type: " + type);
//         }
//     }
// 
//     public static @NotNull Type typeParameter(@NotNull Type type) {
//         if (type instanceof ParameterizedType)
//             return ((ParameterizedType) type).getActualTypeArguments()[0];
// 
//         return Object.class;
//     }
// 
//     public static boolean isEnum(@NotNull Type type) {
//         return (type instanceof Class<?>) && ((Class<?>) type).isEnum();
//     }
// 
//     public static boolean isPrimitive(@NotNull Type type) {
//         return (type instanceof Class<?>) && ((Class<?>) type).isPrimitive();
//     }
// 
//     public static @NotNull Class<?> arrayType(@NotNull Class<?> type) {
//         return Array.newInstance(type, 0).getClass();
//     }
// 
//     public static @Nullable Type genericSuperClass(@NotNull Type type) {
//         return rawType(type).getGenericSuperclass();
//     }
// 
//     public static @NotNull Type[] genericInterfaces(@NotNull Type type) {
//         return rawType(type).getGenericInterfaces();
//     }
// 
//     public static boolean isAssignable(@NotNull Type target, @NotNull Type source) {
//         // TODO: implement proper rules for generic types
//         return isAssignableByBoxing(rawType(target), rawType(source));
//     }
// 
//     private static boolean isAssignableByBoxing(@NotNull Class<?> target, @NotNull Class<?> source) {
//         return Primitives.wrap(target).isAssignableFrom(Primitives.wrap(source));
//     }
// }
// 
// Path: dalesbred/src/main/java/org/dalesbred/internal/utils/CollectionUtils.java
// public static @NotNull <A,B> List<B> mapToList(@NotNull Collection<? extends A> xs, @NotNull Function<? super A, ? extends B> mapper) {
//     return xs.stream().map(mapper).collect(toListWithCapacity(xs.size()));
// }


import static java.util.Collections.unmodifiableList;
import static java.util.Objects.requireNonNull;
import static org.dalesbred.internal.utils.CollectionUtils.mapToList;
import org.dalesbred.internal.utils.TypeUtils;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/*
 * Copyright (c) 2018 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.result;




/**
 * Represents the results of the query along with its metadata. Basically a detached
 * version of {@link java.sql.ResultSet}.
 */
public final class ResultTable implements Iterable<ResultTable.ResultRow> {

    private final @NotNull List<ColumnMetadata> columns;

    private final @NotNull List<ResultRow> rows;

    private ResultTable(@NotNull List<ColumnMetadata> columns, @NotNull List<ResultRow> rows) {
        this.columns = unmodifiableList(columns);
        this.rows = unmodifiableList(rows);
    }

    public int getRowCount() {
        return rows.size();
    }

    public int getColumnCount() {
        return columns.size();
    }

    /**
     * Returns the value of given column of given row. Both indices are zero-based.
     */
    public Object get(int row, int column) {
        return rows.get(row).get(column);
    }

    /**
     * Returns the value of given named column of given row.
     */
    public Object get(int row, @NotNull String column) {
        return rows.get(row).get(column);
    }

    public @NotNull List<ResultRow> getRows() {
        return rows;
    }

    public @NotNull List<ColumnMetadata> getColumns() {
        return columns;
    }

    public @NotNull List<String> getColumnNames() {
// Path: dalesbred/src/main/java/org/dalesbred/internal/utils/TypeUtils.java
// public final class TypeUtils {
// 
//     private TypeUtils() { }
// 
//     public static @NotNull Class<?> rawType(@NotNull Type type) {
//         if (type instanceof Class<?>) {
//             return (Class<?>) type;
// 
//         } else if (type instanceof ParameterizedType) {
//             return rawType(((ParameterizedType) type).getRawType());
// 
//         } else if (type instanceof TypeVariable<?>) {
//             // We could return one of the bounds, but it won't work if there are multiple bounds.
//             // Therefore just return object.
//             return Object.class;
// 
//         } else if (type instanceof WildcardType) {
//             return rawType(((WildcardType) type).getUpperBounds()[0]);
// 
//         } else if (type instanceof GenericArrayType) {
//             return arrayType(rawType(((GenericArrayType) type).getGenericComponentType()));
// 
//         } else {
//             throw new IllegalArgumentException("unexpected type: " + type);
//         }
//     }
// 
//     public static @NotNull Type typeParameter(@NotNull Type type) {
//         if (type instanceof ParameterizedType)
//             return ((ParameterizedType) type).getActualTypeArguments()[0];
// 
//         return Object.class;
//     }
// 
//     public static boolean isEnum(@NotNull Type type) {
//         return (type instanceof Class<?>) && ((Class<?>) type).isEnum();
//     }
// 
//     public static boolean isPrimitive(@NotNull Type type) {
//         return (type instanceof Class<?>) && ((Class<?>) type).isPrimitive();
//     }
// 
//     public static @NotNull Class<?> arrayType(@NotNull Class<?> type) {
//         return Array.newInstance(type, 0).getClass();
//     }
// 
//     public static @Nullable Type genericSuperClass(@NotNull Type type) {
//         return rawType(type).getGenericSuperclass();
//     }
// 
//     public static @NotNull Type[] genericInterfaces(@NotNull Type type) {
//         return rawType(type).getGenericInterfaces();
//     }
// 
//     public static boolean isAssignable(@NotNull Type target, @NotNull Type source) {
//         // TODO: implement proper rules for generic types
//         return isAssignableByBoxing(rawType(target), rawType(source));
//     }
// 
//     private static boolean isAssignableByBoxing(@NotNull Class<?> target, @NotNull Class<?> source) {
//         return Primitives.wrap(target).isAssignableFrom(Primitives.wrap(source));
//     }
// }
// 
// Path: dalesbred/src/main/java/org/dalesbred/internal/utils/CollectionUtils.java
// public static @NotNull <A,B> List<B> mapToList(@NotNull Collection<? extends A> xs, @NotNull Function<? super A, ? extends B> mapper) {
//     return xs.stream().map(mapper).collect(toListWithCapacity(xs.size()));
// }


// Path: dalesbred/src/main/java/org/dalesbred/result/ResultTable.java
import static java.util.Collections.unmodifiableList;
import static java.util.Objects.requireNonNull;
import static org.dalesbred.internal.utils.CollectionUtils.mapToList;
import org.dalesbred.internal.utils.TypeUtils;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/*
 * Copyright (c) 2018 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.result;




/**
 * Represents the results of the query along with its metadata. Basically a detached
 * version of {@link java.sql.ResultSet}.
 */
public final class ResultTable implements Iterable<ResultTable.ResultRow> {

    private final @NotNull List<ColumnMetadata> columns;

    private final @NotNull List<ResultRow> rows;

    private ResultTable(@NotNull List<ColumnMetadata> columns, @NotNull List<ResultRow> rows) {
        this.columns = unmodifiableList(columns);
        this.rows = unmodifiableList(rows);
    }

    public int getRowCount() {
        return rows.size();
    }

    public int getColumnCount() {
        return columns.size();
    }

    /**
     * Returns the value of given column of given row. Both indices are zero-based.
     */
    public Object get(int row, int column) {
        return rows.get(row).get(column);
    }

    /**
     * Returns the value of given named column of given row.
     */
    public Object get(int row, @NotNull String column) {
        return rows.get(row).get(column);
    }

    public @NotNull List<ResultRow> getRows() {
        return rows;
    }

    public @NotNull List<ColumnMetadata> getColumns() {
        return columns;
    }

    public @NotNull List<String> getColumnNames() {
        return mapToList(columns, ColumnMetadata::getName);