Permalink
Browse files

Exposed the N-Ary cartesian product.

  • Loading branch information...
akiellor committed Apr 5, 2012
1 parent e89fa13 commit 8078b2e7e027dde1c79a88ad3283461dbe2de148
@@ -216,7 +216,7 @@ public Boolean map(Pair<T, T> input) {
return map(cartesianProduct(listWith(first, second, third, fourth)), Mappers.<S, T, U, V>toQuadruple());
}
- private static Iterable<? extends Iterable<?>> cartesianProduct(final Iterable<? extends Iterable<?>> iterables) {
+ public static Iterable<? extends Iterable<?>> cartesianProduct(final Iterable<? extends Iterable<?>> iterables) {
return cartesianProduct(listFrom(iterables));
}
@@ -96,6 +96,33 @@ public void shouldReturnTheCartesianProductOfFourSuppliedIterablesAsAnIterableOf
assertThat(actualCartesianProduct, hasOnlyItemsInAnyOrder(expectedCartesianProduct));
}
+ @Test
+ @SuppressWarnings("unchecked")
+ public void shouldReturnTheCartesianProductOfNSuppliedIterablesAsAnIterableOfIterables() throws Exception {
+ // Given
+ Iterable<Integer> input1 = listWith(1, 2);
+ Iterable<String> input2 = listWith("a", "b", "c");
+ Iterable<Long> input3 = listWith(1L, 2L, 3L);
+
+ Collection expectedCartesianProduct = Literals.listWith(
+ listWith(1, "a", 1L), listWith(2, "a", 1L),
+ listWith(1, "b", 1L), listWith(2, "b", 1L),
+ listWith(1, "c", 1L), listWith(2, "c", 1L),
+ listWith(1, "a", 2L), listWith(2, "a", 2L),
+ listWith(1, "b", 2L), listWith(2, "b", 2L),
+ listWith(1, "c", 2L), listWith(2, "c", 2L),
+ listWith(1, "a", 3L), listWith(2, "a", 3L),
+ listWith(1, "b", 3L), listWith(2, "b", 3L),
+ listWith(1, "c", 3L), listWith(2, "c", 3L)
+ );
+
+ // When
+ Collection actualCartesianProduct = materialize(Lazily.cartesianProduct(listWith(input1, input2, input3)));
+
+ // Then
+ assertThat(actualCartesianProduct, hasOnlyItemsInAnyOrder(expectedCartesianProduct));
+ }
+
@Test
public void shouldAllowIteratorToBeCalledMultipleTimesReturningDifferentIterators() throws Exception {
// Given

0 comments on commit 8078b2e

Please sign in to comment.