Java Code Examples for com.google.common.collect.Sets#powerSet()

The following examples show how to use com.google.common.collect.Sets#powerSet() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TypeMetadataProviderTest.java    From datawave with Apache License 2.0 6 votes vote down vote up
@Test
public void showPowerSet() {
    
    Set<String> auths = Sets.newHashSet("AUTHA", "AUTHB", "BAR");
    Set<Set<String>> powerset = Sets.powerSet(auths);
    for (Set<String> s : powerset) {
        log.debug("powerset has " + s);
    }
    log.debug("got " + powerset);
    Assert.assertTrue(powerset.contains(Sets.newHashSet()));
    Assert.assertTrue(powerset.contains(Sets.newHashSet("AUTHA")));
    Assert.assertTrue(powerset.contains(Sets.newHashSet("AUTHB")));
    Assert.assertTrue(powerset.contains(Sets.newHashSet("BAR")));
    Assert.assertTrue(powerset.contains(Sets.newHashSet("AUTHA", "AUTHB")));
    Assert.assertTrue(powerset.contains(Sets.newHashSet("AUTHB", "BAR")));
    Assert.assertTrue(powerset.contains(Sets.newHashSet("AUTHA", "BAR")));
    Assert.assertTrue(powerset.contains(Sets.newHashSet("AUTHA", "AUTHB", "BAR")));
}
 
Example 2
Source File: OrderedMergeTest.java    From rxjava-extras with Apache License 2.0 6 votes vote down vote up
private void checkAllCombinationsFromPowerSet(Scheduler scheduler) {
	// this test covers everything!
	for (int n = 0; n <= 10; n++) {
		Set<Integer> numbers = Sets.newTreeSet();
		for (int i = 1; i <= n; i++) {
			numbers.add(i);
		}
		for (Set<Integer> a : Sets.powerSet(numbers)) {
			TreeSet<Integer> x = Sets.newTreeSet(a);
			TreeSet<Integer> y = Sets.newTreeSet(Sets.difference(numbers, x));
			Observable<Integer> o1 = from(x).subscribeOn(scheduler);
			Observable<Integer> o2 = from(y).subscribeOn(scheduler);
			List<Integer> list = o1.compose(Transformers.orderedMergeWith(o2, comparator)).toList().toBlocking()
					.single();
			// System.out.println(x + " " + y);
			assertEquals(Lists.newArrayList(numbers), list);
		}
	}
}
 
Example 3
Source File: PowerSetUtilityUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenSet_WhenGuavaLibraryGeneratePowerSet_ThenItContainsAllSubsets() {
    ImmutableSet<String> set = ImmutableSet.of("APPLE", "ORANGE", "MANGO");
    Set<Set<String>> powerSet = Sets.powerSet(set);
    Assertions.assertEquals((1 << set.size()), powerSet.size());
    MatcherAssert.assertThat(powerSet, Matchers.containsInAnyOrder(
      ImmutableSet.of(),
      ImmutableSet.of("APPLE"),
      ImmutableSet.of("ORANGE"),
      ImmutableSet.of("APPLE", "ORANGE"),
      ImmutableSet.of("MANGO"),
      ImmutableSet.of("APPLE", "MANGO"),
      ImmutableSet.of("ORANGE", "MANGO"),
      ImmutableSet.of("APPLE", "ORANGE", "MANGO")
    ));
}
 
Example 4
Source File: ParameterConfigHelper.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
public static Set<Parameter> getAssignedParameters(AbstractElement element, int parameterConfig) {
	if (parameterConfig != 0) {
		ParserRule parserRule = GrammarUtil.containingParserRule(element);
		Set<Parameter> allParameters = ImmutableSet.copyOf(parserRule.getParameters());
		int seen = 0;
		for(Set<Parameter> candidate: Sets.powerSet(allParameters)) {
			if (seen == parameterConfig) {
				return candidate;
			}
			seen++;
		}
	}
	return Collections.emptySet();
}
 
Example 5
Source File: ParameterConfigHelper.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
public static int getParameterConfig(Set<Parameter> params, ParserRule context) {
	if (params.isEmpty()) {
		return 0;
	}
	Set<Parameter> allParameters = ImmutableSet.copyOf(context.getParameters());
	int result = 0;
	for(Set<Parameter> candidate: Sets.powerSet(allParameters)) {
		if (params.equals(candidate)) {
			return result;
		}
		result++;
	}
	throw new IllegalStateException();
}
 
Example 6
Source File: CombinationGenerator.java    From levelup-java-exercises with Apache License 2.0 5 votes vote down vote up
/**
 * Method will generate possible combinations of 
 * elements based on parameters
 * 
 * @param from
 * @param size
 * 
 * @return possible combinations
 * 
 */
public static Set<Set<Integer>> generateCombinations(
		Set<Integer> from,
		int size) {

	Set<Set<Integer>> elements = Sets.powerSet(from);

	Set<Set<Integer>> possibleCombinations = elements.stream().filter(p -> p.size() == size)
			.collect(Collectors.toSet());

	return possibleCombinations;
}
 
Example 7
Source File: DexArchiveAspect.java    From bazel with Apache License 2.0 4 votes vote down vote up
private static Set<Set<String>> aspectDexopts(RuleContext ruleContext) {
  return Sets.powerSet(
      normalizeDexopts(getAndroidConfig(ruleContext).getDexoptsSupportedInIncrementalDexing()));
}