Repository: commons-text Updated Branches: refs/heads/master 569dbc094 -> 5e479dcd7
TEXT-93: RandomStringGenerator accepts a list of valid characters Project: http://git-wip-us.apache.org/repos/asf/commons-text/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-text/commit/278f1e0f Tree: http://git-wip-us.apache.org/repos/asf/commons-text/tree/278f1e0f Diff: http://git-wip-us.apache.org/repos/asf/commons-text/diff/278f1e0f Branch: refs/heads/master Commit: 278f1e0fb553fc2d2d6f5563d9e61899776cd80d Parents: e85959f Author: Amey Jadiye <ameyjad...@gmail.com> Authored: Fri Jun 23 02:48:11 2017 +0530 Committer: Amey Jadiye <ameyjad...@gmail.com> Committed: Fri Jun 23 02:48:11 2017 +0530 ---------------------------------------------------------------------- .../commons/text/RandomStringGenerator.java | 102 +++++++++++++++++-- .../commons/text/RandomStringGeneratorTest.java | 31 ++++++ 2 files changed, 122 insertions(+), 11 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-text/blob/278f1e0f/src/main/java/org/apache/commons/text/RandomStringGenerator.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/text/RandomStringGenerator.java b/src/main/java/org/apache/commons/text/RandomStringGenerator.java index 6aa6806..d3d8567 100644 --- a/src/main/java/org/apache/commons/text/RandomStringGenerator.java +++ b/src/main/java/org/apache/commons/text/RandomStringGenerator.java @@ -16,12 +16,14 @@ */ package org.apache.commons.text; +import org.apache.commons.lang3.Validate; + +import java.util.ArrayList; import java.util.HashSet; +import java.util.List; import java.util.Set; import java.util.concurrent.ThreadLocalRandom; -import org.apache.commons.lang3.Validate; - /** * <p> * Generates random Unicode strings containing the specified number of code points. @@ -73,23 +75,29 @@ public final class RandomStringGenerator { private final TextRandomProvider random; /** + * The source of provided charachters. + */ + private final List<Character> characterList; + + /** * Constructs the generator. - * - * @param minimumCodePoint + * @param minimumCodePoint * smallest allowed code point (inclusive) * @param maximumCodePoint * largest allowed code point (inclusive) * @param inclusivePredicates - * filters for code points - * @param random - * source of randomness + * filters for code points + * @param random random generator + * @param characterList list of predefined set of characters */ private RandomStringGenerator(int minimumCodePoint, int maximumCodePoint, - Set<CharacterPredicate> inclusivePredicates, TextRandomProvider random) { + Set<CharacterPredicate> inclusivePredicates, TextRandomProvider random, + List<Character> characterList) { this.minimumCodePoint = minimumCodePoint; this.maximumCodePoint = maximumCodePoint; this.inclusivePredicates = inclusivePredicates; this.random = random; + this.characterList = characterList; } /** @@ -109,6 +117,23 @@ public final class RandomStringGenerator { return ThreadLocalRandom.current().nextInt(minInclusive, maxInclusive + 1); } + + + /** + * Generates a random number within a range, using a {@link ThreadLocalRandom} instance + * or the user-supplied source of randomness. + * + * @param characterList predefined char list. + * @return the random number. + */ + private int generateRandomNumber(final List<Character> characterList) { + int listSize = characterList.size(); + if (random != null) { + return String.valueOf(characterList.get(random.nextInt(listSize))).codePointAt(0); + } + return String.valueOf(characterList.get(ThreadLocalRandom.current().nextInt(0, listSize))).codePointAt(0); + } + /** * <p> * Generates a random string, containing the specified number of code points. @@ -142,8 +167,12 @@ public final class RandomStringGenerator { long remaining = length; do { - int codePoint = generateRandomNumber(minimumCodePoint, maximumCodePoint); - + int codePoint; + if (characterList != null && characterList.size() > 0) { + codePoint = generateRandomNumber(characterList); + } else { + codePoint = generateRandomNumber(minimumCodePoint, maximumCodePoint); + } switch (Character.getType(codePoint)) { case Character.UNASSIGNED: case Character.PRIVATE_USE: @@ -234,6 +263,11 @@ public final class RandomStringGenerator { private TextRandomProvider random; /** + * The source of provided charachters. + */ + private List<Character> characterList; + + /** * <p> * Specifies the minimum and maximum code points allowed in the * generated string. @@ -336,12 +370,58 @@ public final class RandomStringGenerator { } /** + * <p> + * Limits the characters in the generated string to those who match at + * supplied list of Character. + * </p> + * + * <p> + * Passing {@code null} or an empty array to this method will revert to the + * default behaviour of allowing any character. Multiple calls to this + * method will replace the previously stored Character. + * </p> + * + * @param chars set of preefined Characters for random string generation + * the Character can be, may be {@code null} or empty + * @return {@code this}, to allow method chaining + */ + public Builder selectFromList(char[] chars) { + characterList = new ArrayList<Character>(); + for (char c : chars) { + characterList.add(c); + } + return this; + } + + /** + * <p> + * Limits the characters in the generated string to those who match at + * supplied list of Character. + * </p> + * + * <p> + * Passing {@code null} or an empty array to this method will revert to the + * default behaviour of allowing any character. Multiple calls to this + * method will replace the previously stored Character. + * </p> + * + * @param characterList set of preefined Characters for random string generation + * the Character can be, may be {@code null} or empty + * @return {@code this}, to allow method chaining + */ + public Builder selectFromList(List<Character> characterList) { + this.characterList = characterList; + return this; + } + + /** * <p>Builds the {@code RandomStringGenerator} using the properties specified.</p> * @return the configured {@code RandomStringGenerator} */ @Override public RandomStringGenerator build() { - return new RandomStringGenerator(minimumCodePoint, maximumCodePoint, inclusivePredicates, random); + return new RandomStringGenerator(minimumCodePoint, maximumCodePoint, inclusivePredicates, + random, characterList); } } } http://git-wip-us.apache.org/repos/asf/commons-text/blob/278f1e0f/src/test/java/org/apache/commons/text/RandomStringGeneratorTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/RandomStringGeneratorTest.java b/src/test/java/org/apache/commons/text/RandomStringGeneratorTest.java index 86537fa..463e767 100644 --- a/src/test/java/org/apache/commons/text/RandomStringGeneratorTest.java +++ b/src/test/java/org/apache/commons/text/RandomStringGeneratorTest.java @@ -18,6 +18,9 @@ package org.apache.commons.text; import org.junit.Test; +import java.util.ArrayList; +import java.util.List; + import static org.junit.Assert.*; /** @@ -203,4 +206,32 @@ public class RandomStringGeneratorTest { RandomStringGenerator generator = new RandomStringGenerator.Builder().build(); assertEquals("", generator.generate(0)); } + + @Test + public void testSelectFromList() { + List<Character> list = new ArrayList<Character>(); + list.add('a'); + list.add('b'); + list.add('c'); + RandomStringGenerator generator = new RandomStringGenerator.Builder().selectFromList(list).build(); + + String randomText = generator.generate(5); + + for (char c : randomText.toCharArray()) { + assertTrue(list.contains(c)); + } + } + + @Test + public void testSelectFromCharArray() { + String str = "abc"; + char[] charArray = str.toCharArray(); + RandomStringGenerator generator = new RandomStringGenerator.Builder().selectFromList(charArray).build(); + + String randomText = generator.generate(5); + + for (char c : randomText.toCharArray()) { + assertTrue(str.indexOf(c) != -1); + } + } }