@SuppressWarnings("unchecked") // varargs! public void testCartesianProduct_binary0x0() { Set<Integer> mt = emptySet(); assertEmpty(Sets.cartesianProduct(mt, mt)); }
private List<CassandraPartition> getCassandraPartitions(CassandraTable table, TupleDomain<ColumnHandle> tupleDomain) { if (tupleDomain.isNone()) { return ImmutableList.of(); } List<Set<Object>> partitionKeysList = getPartitionKeysList(table, tupleDomain); Set<List<Object>> filterList = Sets.cartesianProduct(partitionKeysList); // empty filters means, all partitions if (filterList.isEmpty()) { return cassandraSession.getPartitions(table, ImmutableList.of()); } return cassandraSession.getPartitions(table, partitionKeysList); }
/** * Sets.cartesianProduct doesn't allow sets that contain null, but we want null to mean "don't * call the associated CacheBuilder method" - that is, get the default CacheBuilder behavior. This * method wraps the elements in the input sets (which may contain null) as Optionals, calls * Sets.cartesianProduct with those, then transforms the result to unwrap the Optionals. */ private Iterable<List<Object>> buildCartesianProduct(Set<?>... sets) { List<Set<Optional<?>>> optionalSets = Lists.newArrayListWithExpectedSize(sets.length); for (Set<?> set : sets) { Set<Optional<?>> optionalSet = Sets.newLinkedHashSet(Iterables.transform(set, NULLABLE_TO_OPTIONAL)); optionalSets.add(optionalSet); } Set<List<Optional<?>>> cartesianProduct = Sets.cartesianProduct(optionalSets); return Iterables.transform( cartesianProduct, new Function<List<Optional<?>>, List<Object>>() { @Override public List<Object> apply(List<Optional<?>> objs) { return Lists.transform(objs, OPTIONAL_TO_NULLABLE); } }); }
@Override public RoutingResult route() { RoutingResult result = new RoutingResult(); for (Entry<String, Set<String>> entry : getDataSourceLogicTablesMap().entrySet()) { List<Set<String>> actualTableGroups = getActualTableGroups(entry.getKey(), entry.getValue()); List<Set<RoutingTable>> routingTableGroups = toRoutingTableGroups(entry.getKey(), actualTableGroups); result.getTableUnits().getTableUnits().addAll(getTableUnits(entry.getKey(), Sets.cartesianProduct(routingTableGroups)).getTableUnits()); } return result; }
/** The 0-ary cartesian product is a single empty list. */ @SuppressWarnings("unchecked") // varargs! public void testCartesianProduct_zeroary() { assertThat(Sets.cartesianProduct()).containsExactly(list()); }
@SuppressWarnings("unchecked") // varargs! public void testCartesianProduct_binary0x1() { Set<Integer> mt = emptySet(); assertEmpty(Sets.cartesianProduct(mt, set(1))); }
@SuppressWarnings("unchecked") // varargs! public void testCartesianProduct_binary1x0() { Set<Integer> mt = emptySet(); assertEmpty(Sets.cartesianProduct(set(1), mt)); }
@Parameterized.Parameters public static Iterable<Object[]> constructorFeeder() { return Iterables.transform( Sets.cartesianProduct( ImmutableList.of( ImmutableSet.of(true, false), ImmutableSet.of("", "MISSING VALUE"), ImmutableSet.of(Optional.of(true), Optional.of(false), Optional.empty()) ) ), List::toArray ); }
@Parameterized.Parameters(name = "{index}: compression={0}, byteOrder={1}") public static Iterable<Object[]> compressionStrategiesAndByteOrders() { Set<List<Object>> combinations = Sets.cartesianProduct( Sets.newHashSet(CompressionStrategy.noNoneValues()), Sets.newHashSet(ByteOrder.BIG_ENDIAN, ByteOrder.LITTLE_ENDIAN) ); return Iterables.transform( combinations, (Function<List, Object[]>) input -> new Object[]{input.get(0), input.get(1)} ); }
@Parameterized.Parameters(name = "{index}: compression={0}, byteOrder={1}") public static Iterable<Object[]> compressionStrategiesAndByteOrders() { Set<List<Object>> combinations = Sets.cartesianProduct( Sets.newHashSet(CompressionStrategy.noNoneValues()), Sets.newHashSet(ByteOrder.BIG_ENDIAN, ByteOrder.LITTLE_ENDIAN) ); return Iterables.transform( combinations, (Function<List, Object[]>) input -> new Object[]{input.get(0), input.get(1)} ); }
@Parameterized.Parameters(name = "{index}: compression={0}, byteOrder={1}") public static Iterable<Object[]> compressionStrategiesAndByteOrders() { Set<List<Object>> combinations = Sets.cartesianProduct( Sets.newHashSet(CompressionStrategy.noNoneValues()), Sets.newHashSet(ByteOrder.BIG_ENDIAN, ByteOrder.LITTLE_ENDIAN) ); return Iterables.transform( combinations, (Function<List, Object[]>) input -> new Object[]{input.get(0), input.get(1)} ); }
@SuppressWarnings("unchecked") // varargs! public void testCartesianProductTooBig() { Set<Integer> set = ContiguousSet.create(Range.closed(0, 10000), DiscreteDomain.integers()); try { Sets.cartesianProduct(set, set, set, set, set); fail("Expected IAE"); } catch (IllegalArgumentException expected) { } }
/** A unary cartesian product is one list of size 1 for each element in the input set. */ @SuppressWarnings("unchecked") // varargs! public void testCartesianProduct_unary() { assertThat(Sets.cartesianProduct(set(1, 2))).containsExactly(list(1), list(2)); }
@SuppressWarnings("unchecked") // varargs! public void testCartesianProduct_binary1x1() { assertThat(Sets.cartesianProduct(set(1), set(2))).contains(list(1, 2)); }
@SuppressWarnings("unchecked") // varargs! public void testCartesianProduct_hashCode() { // Run through the same cartesian products we tested above Set<List<Integer>> degenerate = Sets.cartesianProduct(); checkHashCode(degenerate); checkHashCode(Sets.cartesianProduct(set(1, 2))); int num = Integer.MAX_VALUE / 3 * 2; // tickle overflow-related problems checkHashCode(Sets.cartesianProduct(set(1, 2, num))); Set<Integer> mt = emptySet(); checkHashCode(Sets.cartesianProduct(mt, mt)); checkHashCode(Sets.cartesianProduct(mt, set(num))); checkHashCode(Sets.cartesianProduct(set(num), mt)); checkHashCode(Sets.cartesianProduct(set(num), set(1))); checkHashCode(Sets.cartesianProduct(set(1), set(2, num))); checkHashCode(Sets.cartesianProduct(set(1, num), set(2, num - 1))); checkHashCode(Sets.cartesianProduct(set(1, num), set(2, num - 1), set(3, num + 1))); // a bigger one checkHashCode( Sets.cartesianProduct(set(1, num, num + 1), set(2), set(3, num + 2), set(4, 5, 6, 7, 8))); }
@SuppressWarnings("unchecked") // varargs! public void testCartesianProduct_contains() { Set<List<Integer>> actual = Sets.cartesianProduct(set(1, 2), set(3, 4)); assertTrue(actual.contains(list(1, 3))); assertTrue(actual.contains(list(1, 4))); assertTrue(actual.contains(list(2, 3))); assertTrue(actual.contains(list(2, 4))); assertFalse(actual.contains(list(3, 1))); }
@SuppressWarnings("unchecked") // varargs! public void testCartesianProduct_binary1x2() { assertThat(Sets.cartesianProduct(set(1), set(2, 3))) .containsExactly(list(1, 2), list(1, 3)) .inOrder(); }
@SuppressWarnings("unchecked") // varargs! public void testCartesianProduct_binary2x2() { assertThat(Sets.cartesianProduct(set(1, 2), set(3, 4))) .containsExactly(list(1, 3), list(1, 4), list(2, 3), list(2, 4)) .inOrder(); }
@SuppressWarnings("unchecked") // varargs! public void testCartesianProduct_unrelatedTypes() { Set<Integer> x = set(1, 2); Set<String> y = set("3", "4"); List<Object> exp1 = list((Object) 1, "3"); List<Object> exp2 = list((Object) 1, "4"); List<Object> exp3 = list((Object) 2, "3"); List<Object> exp4 = list((Object) 2, "4"); assertThat(Sets.<Object>cartesianProduct(x, y)) .containsExactly(exp1, exp2, exp3, exp4) .inOrder(); }
@SuppressWarnings("unchecked") // varargs! public void testCartesianProduct_2x2x2() { assertThat(Sets.cartesianProduct(set(0, 1), set(0, 1), set(0, 1))) .containsExactly( list(0, 0, 0), list(0, 0, 1), list(0, 1, 0), list(0, 1, 1), list(1, 0, 0), list(1, 0, 1), list(1, 1, 0), list(1, 1, 1)) .inOrder(); }