Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public final class Permuter
- {
- private final String characters;
- private List<Character> current;
- public Permuter()
- {
- this("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
- }
- public Permuter(final String characters)
- {
- this.characters = characters;
- this.current = new ArrayList<>();
- this.current.add(characters.charAt(0));
- }
- public String next()
- {
- final String result = this.build();
- this.advance();
- return result;
- }
- private String build()
- {
- String result = "";
- for (final char c : this.current)
- result += c;
- return result;
- }
- private void advance()
- {
- this.advance(0);
- }
- private void advance(final int index)
- {
- if (index >= this.current.size())
- {
- this.current.add(this.characters.charAt(0));
- }
- else
- {
- final char current = this.current.get(index);
- final int indexOfCurrent = this.characters.indexOf(current);
- final int indexOfNext = indexOfCurrent + 1;
- if (indexOfNext >= this.characters.length())
- {
- this.current.set(index, this.characters.charAt(0));
- this.advance(index + 1);
- }
- else
- {
- this.current.set(index, this.characters.charAt(indexOfNext));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement