1 package junitparams.custom.combined; 2 3 import java.util.Arrays; 4 import java.util.List; 5 6 class Cartesian { 7 8 static Object[] getCartesianProductOf(List<Object[]> array) { 9 if (array == null || array.size() == 0) { 10 return new Object[]{}; 11 } 12 13 for (int i = 0; i < array.size() - 1; i++) { 14 Object[] arrayOne = array.get(i); 15 Object[] arrayTwo = array.get(i + 1); 16 array.set(i + 1, cartesianProduct(arrayOne, arrayTwo)); 17 } 18 19 return array.get(array.size() - 1); 20 } 21 22 private static Object[] cartesianProduct(Object[] arrayOne, Object[] arrayTwo) { 23 int numberOfCombinations = arrayOne.length * arrayTwo.length; 24 Object[] resultArray = new Object[numberOfCombinations][2]; 25 26 int i = 0; 27 for (Object firstElement : arrayOne) { 28 for (Object secondElement : arrayTwo) { 29 resultArray[i] = getCartesianOfTwoElements(firstElement, secondElement); 30 i++; 31 } 32 } 33 34 return resultArray; 35 } 36 37 private static Object getCartesianOfTwoElements(Object objectOne, Object objectTwo) { 38 if (!objectOne.getClass().isArray()) { 39 return new Object[]{objectOne, objectTwo}; 40 } 41 Object[] initialArray = (Object[]) objectOne; 42 Object[] newArray = Arrays.copyOf(initialArray, initialArray.length + 1); 43 newArray[newArray.length - 1] = objectTwo; 44 return newArray; 45 } 46 } 47