com.google.common.collect.Collections2#orderedPermutations ( )源码实例Demo

下面列出了com.google.common.collect.Collections2#orderedPermutations ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

@Test
public void ordered_permutations () {
	
	List<Integer> vals = Lists.newArrayList(1, 2, 3);
	
	Collection<List<Integer>> orderPerm = 
			Collections2.orderedPermutations(vals);
	
	for (List<Integer> val : orderPerm) {
		logger.info(val);
	}

	assertEquals(6, orderPerm.size());
}
 
源代码2 项目: bazel   文件: MultisetSemaphoreTest.java
@Test
public void testConcurrentRace_AllPermuations() throws Exception {
  // When we have N values
  int n = 6;
  ArrayList<String> vals = new ArrayList<>();
  for (int i = 0; i < n; i++) {
    vals.add("val-" + i);
  }
  // And we have all permutations of these N values
  Collection<List<String>> permutations = Collections2.orderedPermutations(vals);
  int numPermutations = permutations.size();
  // And we have a MultisetSemaphore
  final MultisetSemaphore<String> multisetSemaphore = MultisetSemaphore.newBuilder()
      // with N max num unique values,
      .maxNumUniqueValues(n)
      .build();
  // And a ExecutorService with N! threads,
  ExecutorService executorService = Executors.newFixedThreadPool(numPermutations);
  // And a recorder for thrown exceptions,
  ThrowableRecordingRunnableWrapper wrapper =
      new ThrowableRecordingRunnableWrapper("testConcurrentRace_AllPermuations");
  for (List<String> orderedVals : permutations) {
    final Set<String> orderedSet = new LinkedHashSet<>(orderedVals);
    // And we submit N! Runnables, each of which
    @SuppressWarnings("unused")
    Future<?> possiblyIgnoredError =
        executorService.submit(
            wrapper.wrap(
                new Runnable() {
                  @Override
                  public void run() {
                    try {
                      // Tries to acquire permits for the set of N values, with a unique
                      // iteration order (across all the N! different permutations),
                      multisetSemaphore.acquireAll(orderedSet);
                      // And then immediately releases the permits.
                      multisetSemaphore.releaseAll(orderedSet);
                    } catch (InterruptedException e) {
                      throw new IllegalStateException(e);
                    }
                  }
                }));
  }
  // Then all of our Runnables completed (without deadlock!), as expected,
  boolean interrupted = ExecutorUtil.interruptibleShutdown(executorService);
  // And also none of them threw any Exceptions.
  assertThat(wrapper.getFirstThrownError()).isNull();
  if (interrupted) {
    Thread.currentThread().interrupt();
    throw new InterruptedException();
  }
}