Last active
May 12, 2017 18:47
-
-
Save rastadrian/ca466f6bcfd3598ecb60aff11d8409c2 to your computer and use it in GitHub Desktop.
Permuterator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Permuterator { | |
| public void permute(List<String> values, int depth, Validator validator) { | |
| values.parallelStream().forEach(value -> { | |
| //add fist value | |
| final String[] permutation = new String[depth]; | |
| permutation[0] = value; | |
| final int indexLimit = depth - 1; | |
| traverse(permutation, values, indexLimit, 0, validator); | |
| }); | |
| } | |
| private void traverse(String[] permutation, List<String> values, int indexLimit, int index, Validator validator) { | |
| index ++; | |
| if (index > indexLimit) { | |
| //finished a permutation, ready for validation | |
| validator.validatePermutation(permutation); | |
| return; | |
| } | |
| for (String value : values) { | |
| permutation[index] = value; | |
| traverse(permutation, values, indexLimit, index, validator); | |
| } | |
| } | |
| interface Validator { | |
| void validatePermutation(String[] permutation); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment