/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jdbi.v3.core.array;

import java.lang.reflect.Array;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Collection;

import org.jdbi.v3.core.StatementContext;
import org.jdbi.v3.core.argument.Argument;

class SqlArrayArgument<T> implements Argument {

    private final String typeName;
    private final Object[] array;

    @SuppressWarnings("unchecked")
    SqlArrayArgument(SqlArrayType<T> arrayType, Object newArray) {
        this.typeName = arrayType.getTypeName();
        int length = Array.getLength(newArray);
        this.array = new Object[length];
        for (int i = 0; i < length; i++) {
            array[i] = arrayType.convertArrayElement((T) Array.get(newArray, i));
        }
    }

    SqlArrayArgument(SqlArrayType<T> arrayType, Collection<T> list) {
        this.typeName = arrayType.getTypeName();
        this.array = list.stream().map(arrayType::convertArrayElement).toArray();
    }

    @Override
    public void apply(int position, PreparedStatement statement, StatementContext ctx) throws SQLException {
        SqlArrayArgumentStrategy argumentStyle = ctx.getSqlArrayArgumentStrategy();
        switch(argumentStyle) {
            case SQL_ARRAY:
                java.sql.Array sqlArray = statement.getConnection().createArrayOf(typeName, array);
                ctx.addCleanable(sqlArray::free);
                statement.setArray(position, sqlArray);
                break;
            case OBJECT_ARRAY:
                statement.setObject(position, array);
                break;
            default:
                throw new UnsupportedOperationException("Unknown array argument style " + argumentStyle);
        }
    }
}

@@ -698,26 +698,77 @@
 va.util.
-Collection
+ArrayList;%0Aimport java.util.Collection;%0Aimport java.util.List
 ;%0A%0Aimpor
@@ -846,16 +846,70 @@
 rgument;
+%0Aimport org.jdbi.v3.core.util.ReflectionArrayIterator;
 %0A%0Aclass 
@@ -1172,24 +1172,25 @@
 Name();%0A
+%0A
         
 int leng
@@ -1185,223 +1185,302 @@
     
-int length = Array.getLength(newArray);%0A        this.array = new Object%5Blength%5D;%0A        for (int i = 0; i %3C length; i++) %7B%0A            array%5Bi%5D = arrayType.convertArrayElement((T) Array.get(newArray, i));%0A        %7D
+List%3CObject%3E elements = new ArrayList%3C%3E(%0A                newArray.getClass().isArray() ? Array.getLength(newArray) : 10);%0A        ReflectionArrayIterator.of(newArray).forEachRemaining(%0A                e -%3E elements.add(arrayType.convertArrayElement((T) e)));%0A        array = elements.toArray();
 %0A   

use ReflectionArrayIterator so you can bind iterator types