/*
 * The MIT License
 *
 *   Copyright (c) 2016, Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com)
 *
 *   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 io.github.benas.randombeans;

import io.github.benas.randombeans.api.Randomizer;

import java.lang.reflect.Array;

import static java.lang.Math.abs;

/**
 * Random array populator.
 *
 * @author Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com)
 */
class ArrayPopulator {

    private final EnhancedRandomImpl enhancedRandom;

    private final RandomizerProvider randomizerProvider;

    ArrayPopulator(final EnhancedRandomImpl enhancedRandom, final RandomizerProvider randomizerProvider) {
        this.enhancedRandom = enhancedRandom;
        this.randomizerProvider = randomizerProvider;
    }

    @SuppressWarnings("unchecked")
    <T> Object getRandomArray(final Class<?> fieldType, final PopulatorContext context) {
        Class<?> componentType = fieldType.getComponentType();
        if (componentType.isPrimitive()) {
            return getRandomPrimitiveArray(componentType);
        }
        int randomSize = abs((byte) enhancedRandom.nextInt());
        T[] itemsList = (T[]) Array.newInstance(componentType, randomSize);
        for (int i = 0; i < randomSize; i++) {
            itemsList[i] = (T) enhancedRandom.doPopulateBean(fieldType.getComponentType(), context);
        }
        return itemsList;
    }

    Object getRandomPrimitiveArray(final Class<?> primitiveType) {
        int randomSize = abs((byte) enhancedRandom.nextInt());
        // TODO A bounty will be offered to anybody that comes with a generic template method for that..
        Randomizer randomizer = randomizerProvider.getRandomizerByType(primitiveType);
        if (primitiveType.equals(Byte.TYPE)) {
            byte[] result = new byte[randomSize];
            for (int index = 0; index < randomSize; index++) {
                result[index] = (byte) randomizer.getRandomValue();
            }
            return result;
        }
        if (primitiveType.equals(Short.TYPE)) {
            short[] result = new short[randomSize];
            for (int index = 0; index < randomSize; index++) {
                result[index] = (short) randomizer.getRandomValue();
            }
            return result;
        }
        if (primitiveType.equals(Integer.TYPE)) {
            int[] result = new int[randomSize];
            for (int index = 0; index < randomSize; index++) {
                result[index] = (int) randomizer.getRandomValue();
            }
            return result;
        }
        if (primitiveType.equals(Long.TYPE)) {
            long[] result = new long[randomSize];
            for (int index = 0; index < randomSize; index++) {
                result[index] = (long) randomizer.getRandomValue();
            }
            return result;
        }
        if (primitiveType.equals(Float.TYPE)) {
            float[] result = new float[randomSize];
            for (int index = 0; index < randomSize; index++) {
                result[index] = (float) randomizer.getRandomValue();
            }
            return result;
        }
        if (primitiveType.equals(Double.TYPE)) {
            double[] result = new double[randomSize];
            for (int index = 0; index < randomSize; index++) {
                result[index] = (double) randomizer.getRandomValue();
            }
            return result;
        }
        if (primitiveType.equals(Character.TYPE)) {
            char[] result = new char[randomSize];
            for (int index = 0; index < randomSize; index++) {
                result[index] = (char) randomizer.getRandomValue();
            }
            return result;
        }
        if (primitiveType.equals(Boolean.TYPE)) {
            boolean[] result = new boolean[randomSize];
            for (int index = 0; index < randomSize; index++) {
                result[index] = (boolean) randomizer.getRandomValue();
            }
            return result;
        }
        return null;
    }
}

@@ -2511,32 +2511,38 @@
 Type) %7B%0A        
+final 
 int randomSize =
@@ -2592,2410 +2592,295 @@
     
-// TODO A bounty will be offered to anybody that comes with a generic template method for that..%0A        Randomizer randomizer = randomizerProvider.getRandomizerByType(primitiveType);%0A        if (primitiveType.equals(Byte.TYPE)) %7B%0A            byte%5B%5D result = new byte%5BrandomSize%5D;%0A            for (int index = 0; index %3C randomSize; index++) %7B%0A                result%5Bindex%5D = (byte) randomizer.getRandomValue();%0A            %7D%0A            return result;%0A        %7D%0A        if (primitiveType.equals(Short.TYPE)) %7B%0A            short%5B%5D result = new short%5BrandomSize%5D;%0A            for (int index = 0; index %3C randomSize; index++) %7B%0A                result%5Bindex%5D = (short) randomizer.getRandomValue();%0A            %7D%0A            return result;%0A        %7D%0A        if (primitiveType.equals(Integer.TYPE)) %7B%0A            int%5B%5D result = new int%5BrandomSize%5D;%0A            for (int index = 0; index %3C randomSize; index++) %7B%0A                result%5Bindex%5D = (int) randomizer.getRandomValue();%0A            %7D%0A            return result;%0A        %7D%0A        if (primitiveType.equals(Long.TYPE)) %7B%0A            long%5B%5D result = new long%5BrandomSize%5D;%0A            for (int index = 0; index %3C randomSize; index++) %7B%0A                result%5Bindex%5D = (long) randomizer.getRandomValue();%0A            %7D%0A            return result;%0A        %7D%0A        if (primitiveType.equals(Float.TYPE)) %7B%0A            float%5B%5D result = new float%5BrandomSize%5D;%0A            for (int index = 0; index %3C randomSize; index++) %7B%0A                result%5Bindex%5D = (float) randomizer.getRandomValue();%0A            %7D%0A            return result;%0A        %7D%0A        if (primitiveType.equals(Double.TYPE)) %7B%0A            double%5B%5D result = new double%5BrandomSize%5D;%0A            for (int index = 0; index %3C randomSize; index++) %7B%0A                result%5Bindex%5D = (double) randomizer.getRandomValue();%0A            %7D%0A            return result;%0A        %7D%0A        if (primitiveType.equals(Character.TYPE)) %7B%0A            char%5B%5D result = new char%5BrandomSize%5D;%0A            for (int index = 0; index %3C randomSize; index++) %7B%0A                result%5Bindex%5D = (char) randomizer.getRandomValue();%0A            %7D%0A            return result;%0A        %7D%0A        if (primitiveType.equals(Boolean.TYPE)) %7B%0A            boolean%5B%5D result = new boolean%5BrandomSize%5D;%0A            for (int index = 0; index %3C randomSize; index++) %7B%0A                result%5Bindex%5D = (boolean) randomizer.getRandomValue();%0A    
+final Randomizer randomizer = randomizerProvider.getRandomizerByType(primitiveType);%0A        final Object result = Array.newInstance(primitiveType, randomSize);%0A        for (int index = 0; index %3C randomSize; index++) %7B%0A            Array.set(result, index, randomizer.getRandomValue());%0A
     
@@ -2889,28 +2889,24 @@
   %7D%0A        
- 
-   
 return resul
@@ -2912,39 +2912,8 @@
 lt;%0A
-        %7D%0A        return null;%0A
     

Use java.lang.reflect.Array.* for primitive array "generic" operations