Java Code Examples for com.google.common.collect.Collections2#orderedPermutations()
The following examples show how to use
com.google.common.collect.Collections2#orderedPermutations() .
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: Collections2Example.java From levelup-java-examples with Apache License 2.0 | 5 votes |
@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()); }
Example 2
Source File: MultisetSemaphoreTest.java From bazel with Apache License 2.0 | 4 votes |
@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(); } }