@Test public void testCarthesian() { System.out.println("===[ testCarthesian ]==="); Collection<Collection<String>> dimensions = new ArrayList<Collection<String>>(); Collection<String> dim1 = new ArrayList<String>(); dim1.add("a"); dim1.add("b"); dimensions.add(dim1); Collection<String> dim2 = new ArrayList<String>(); dim2.add("1"); dim2.add("2"); dim2.add("3"); dim2.add("4"); dimensions.add(dim2); Collection<String> dim3 = new ArrayList<String>(); dim3.add("x"); dim3.add("y"); dim3.add("z"); dimensions.add(dim3); final List<String> combinations = new ArrayList<String>(); Processor<Collection<String>> processor = new Processor<Collection<String>>() { @Override public void process(Collection<String> s) { System.out.println(s); combinations.add(MiscUtil.concat(s)); } }; MiscUtil.carthesian(dimensions, processor); System.out.println(combinations); assertEquals("Wrong number of results", 24, combinations.size()); }
public static <T> void carthesian(Collection<Collection<T>> dimensions, Processor<Collection<T>> processor) { List<Collection<T>> dimensionList = new ArrayList<Collection<T>>(dimensions.size()); dimensionList.addAll(dimensions); carthesian(new ArrayList<T>(dimensions.size()), dimensionList, 0, processor); }
MiscUtil { public static <T> void carthesian(Collection<Collection<T>> dimensions, Processor<Collection<T>> processor) { List<Collection<T>> dimensionList = new ArrayList<Collection<T>>(dimensions.size()); dimensionList.addAll(dimensions); carthesian(new ArrayList<T>(dimensions.size()), dimensionList, 0, processor); } }
MiscUtil { public static <T> void carthesian(Collection<Collection<T>> dimensions, Processor<Collection<T>> processor) { List<Collection<T>> dimensionList = new ArrayList<Collection<T>>(dimensions.size()); dimensionList.addAll(dimensions); carthesian(new ArrayList<T>(dimensions.size()), dimensionList, 0, processor); }  }
MiscUtil { public static <T> void carthesian(Collection<Collection<T>> dimensions, Processor<Collection<T>> processor) { List<Collection<T>> dimensionList = new ArrayList<Collection<T>>(dimensions.size()); dimensionList.addAll(dimensions); carthesian(new ArrayList<T>(dimensions.size()), dimensionList, 0, processor); }  @NotNull static Collection<T> union(Collection<T>... sets); static Collection<? extends T> unionExtends(Collection<? extends T>... sets); static boolean listEquals(List<T> a, List<T> b); static boolean unorderedCollectionEquals(Collection a, Collection b); static boolean unorderedCollectionCompare(Collection<T> a, Collection<T> b, final Comparator<T> comparator); static boolean unorderedCollectionEquals(Collection<A> a, Collection<B> b, HeteroComparator<A,B> comparator); static boolean unorderedArrayEquals(T[] a, T[] b); static boolean unorderedArrayEquals(T[] a, T[] b, Comparator<T> comparator); static int unorderedCollectionHashcode(Collection<T> collection, Predicate<T> filter); static String readFile(File file); static void copyFile(File sourceFile, File destFile); static void copyDirectory(File src, File dst); static Collection<T> createCollection(T... items); static Boolean and(Boolean... operands); static boolean equals(Object a, Object b); static XMLGregorianCalendar asXMLGregorianCalendar(java.util.Date date); static XMLGregorianCalendar asXMLGregorianCalendar(Long timeInMilis); static java.util.Date asDate(XMLGregorianCalendar xgc); static Long asLong(XMLGregorianCalendar xgc); static java.util.Date asDate(int year, int month, int date, int hrs, int min, int sec); static void carthesian(Collection<Collection<T>> dimensions, Processor<Collection<T>> processor); static String concat(Collection<String> stringCollection); static boolean isAllNull(Collection<?> collection); static String getValueWithClass(Object object); static boolean isNoValue(Collection<?> collection); static boolean hasNoValue(Collection<?> collection); static Map<K,V> cloneMap(Map<K, V> orig); static String toString(Object o); static List<String> splitLines(String string); static boolean isBetween(XMLGregorianCalendar date, XMLGregorianCalendar start, XMLGregorianCalendar end); static boolean contains(T element, T[] array); static String stripHtmlMarkup(String htmlString); static Collection<T> getValuesFromDisplayableValues(Collection<? extends DisplayableValue<T>> disps); static String binaryToHex(byte[] bytes); static byte[] hexToBinary(String hex); static void addAllIfNotPresent(List<T> receivingList, List<T> supplyingList); static void addIfNotPresent(List<T> receivingList, T supplyingElement); static boolean nullableCollectionsEqual(Collection<?> c1, Collection<?> c2); static String getObjectName(Object o); static Integer compareNullLast(Object o1, Object o2); @SafeVarargs static T getFirstNonNull(T... values); static String getFirstNonNullString(Object... values); static T extractSingleton(Collection<T> collection); static boolean isCollectionOf(Object object, @NotNull Class<?> memberClass); static Function<Object, Stream<E>> instancesOf(Class<E> cls); static T last(List<T> list); static String emptyIfNull(String s); static boolean hasDuplicates(Collection<T> collection); static String formatExceptionMessageWithCause(Throwable t); static String formatExceptionMessageWithCause(Throwable t, int indent); }
MiscUtil { public static <T> void carthesian(Collection<Collection<T>> dimensions, Processor<Collection<T>> processor) { List<Collection<T>> dimensionList = new ArrayList<Collection<T>>(dimensions.size()); dimensionList.addAll(dimensions); carthesian(new ArrayList<T>(dimensions.size()), dimensionList, 0, processor); }  @NotNull static Collection<T> union(Collection<T>... sets); static Collection<? extends T> unionExtends(Collection<? extends T>... sets); static boolean listEquals(List<T> a, List<T> b); static boolean unorderedCollectionEquals(Collection a, Collection b); static boolean unorderedCollectionCompare(Collection<T> a, Collection<T> b, final Comparator<T> comparator); static boolean unorderedCollectionEquals(Collection<A> a, Collection<B> b, HeteroComparator<A,B> comparator); static boolean unorderedArrayEquals(T[] a, T[] b); static boolean unorderedArrayEquals(T[] a, T[] b, Comparator<T> comparator); static int unorderedCollectionHashcode(Collection<T> collection, Predicate<T> filter); static String readFile(File file); static void copyFile(File sourceFile, File destFile); static void copyDirectory(File src, File dst); static Collection<T> createCollection(T... items); static Boolean and(Boolean... operands); static boolean equals(Object a, Object b); static XMLGregorianCalendar asXMLGregorianCalendar(java.util.Date date); static XMLGregorianCalendar asXMLGregorianCalendar(Long timeInMilis); static java.util.Date asDate(XMLGregorianCalendar xgc); static Long asLong(XMLGregorianCalendar xgc); static java.util.Date asDate(int year, int month, int date, int hrs, int min, int sec); static void carthesian(Collection<Collection<T>> dimensions, Processor<Collection<T>> processor); static String concat(Collection<String> stringCollection); static boolean isAllNull(Collection<?> collection); static String getValueWithClass(Object object); static boolean isNoValue(Collection<?> collection); static boolean hasNoValue(Collection<?> collection); static Map<K,V> cloneMap(Map<K, V> orig); static String toString(Object o); static List<String> splitLines(String string); static boolean isBetween(XMLGregorianCalendar date, XMLGregorianCalendar start, XMLGregorianCalendar end); static boolean contains(T element, T[] array); static String stripHtmlMarkup(String htmlString); static Collection<T> getValuesFromDisplayableValues(Collection<? extends DisplayableValue<T>> disps); static String binaryToHex(byte[] bytes); static byte[] hexToBinary(String hex); static void addAllIfNotPresent(List<T> receivingList, List<T> supplyingList); static void addIfNotPresent(List<T> receivingList, T supplyingElement); static boolean nullableCollectionsEqual(Collection<?> c1, Collection<?> c2); static String getObjectName(Object o); static Integer compareNullLast(Object o1, Object o2); @SafeVarargs static T getFirstNonNull(T... values); static String getFirstNonNullString(Object... values); static T extractSingleton(Collection<T> collection); static boolean isCollectionOf(Object object, @NotNull Class<?> memberClass); static Function<Object, Stream<E>> instancesOf(Class<E> cls); static T last(List<T> list); static String emptyIfNull(String s); static boolean hasDuplicates(Collection<T> collection); static String formatExceptionMessageWithCause(Throwable t); static String formatExceptionMessageWithCause(Throwable t, int indent);  }