commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From chtom...@apache.org
Subject [1/4] [text] RandomStringGenerator able to pass multiple ranges to .withinRange()
Date Tue, 05 Sep 2017 18:43:25 GMT
Repository: commons-text
Updated Branches:
  refs/heads/master bc051a360 -> 30a29e2cf


RandomStringGenerator able to pass multiple ranges to .withinRange()


Project: http://git-wip-us.apache.org/repos/asf/commons-text/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-text/commit/beb0b461
Tree: http://git-wip-us.apache.org/repos/asf/commons-text/tree/beb0b461
Diff: http://git-wip-us.apache.org/repos/asf/commons-text/diff/beb0b461

Branch: refs/heads/master
Commit: beb0b4615a94420cc86595c1f060711dd999af91
Parents: 9db7708
Author: Amey Jadiye <ameyjadiye@gmail.com>
Authored: Sat Jul 1 20:25:35 2017 +0530
Committer: Amey Jadiye <ameyjadiye@gmail.com>
Committed: Sat Jul 1 20:25:35 2017 +0530

----------------------------------------------------------------------
 .../commons/text/RandomStringGenerator.java     | 30 ++++++++++++++++++++
 .../commons/text/RandomStringGeneratorTest.java | 26 +++++++++++++++++
 2 files changed, 56 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-text/blob/beb0b461/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 0dcf2a1..541bc57 100644
--- a/src/main/java/org/apache/commons/text/RandomStringGenerator.java
+++ b/src/main/java/org/apache/commons/text/RandomStringGenerator.java
@@ -326,6 +326,36 @@ public final class RandomStringGenerator {
 
         /**
          * <p>
+         * Specifies the array of minimum and maximum char allowed in the
+         * generated string.
+         * </p>
+         *
+         * Ex.
+         * char [][] pairs = {{'0','9'}};
+         * char [][] pairs = {{'a','z'}};
+         * char [][] pairs = {{'a','z'},{'0','9'}};
+         *
+         * @param pairs array of charachters array, expected is to pass min, max pairs through
this arg.
+         * @return {@code this}, to allow method chaining.
+         */
+        public Builder withinRange(final char[] ... pairs) {
+            characterList = new ArrayList<Character>();
+            for (char[] pair :  pairs) {
+                final int minimumCodePoint = pair[0];
+                final int maximumCodePoint = pair[1];
+                Validate.isTrue(minimumCodePoint <= maximumCodePoint,
+                    "Minimum code point %d is larger than maximum code point %d", minimumCodePoint,
maximumCodePoint);
+
+                for (int index = minimumCodePoint; index <= maximumCodePoint; index++)
{
+                    characterList.add((char) index);
+                }
+            }
+            return this;
+
+        }
+
+        /**
+         * <p>
          * Limits the characters in the generated string to those that match at
          * least one of the predicates supplied.
          * </p>

http://git-wip-us.apache.org/repos/asf/commons-text/blob/beb0b461/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 bf00b5c..c157857 100644
--- a/src/test/java/org/apache/commons/text/RandomStringGeneratorTest.java
+++ b/src/test/java/org/apache/commons/text/RandomStringGeneratorTest.java
@@ -112,6 +112,32 @@ public class RandomStringGeneratorTest {
     }
 
     @Test
+    public void testWithinMultipleRanges() {
+        final int length = 5000;
+        final char [][] pairs = {{'a','z'},{'0','9'}};
+        RandomStringGenerator generator = new RandomStringGenerator.Builder().withinRange(pairs).build();
+        String str = generator.generate(length);
+
+        int minimumCodePoint = 0, maximumCodePoint = 0;
+
+        for (char [] pair : pairs ) {
+            minimumCodePoint = Math.min(minimumCodePoint, pair[0]);
+            maximumCodePoint = Math.max(maximumCodePoint, pair[1]);
+        }
+
+        for (char [] pair : pairs ) {
+            int i = 0;
+            do {
+                int codePoint = str.codePointAt(i);
+                assertTrue(codePoint >= minimumCodePoint && codePoint <= maximumCodePoint);
+                i += Character.charCount(codePoint);
+            } while (i < str.length());
+        }
+    }
+
+
+
+    @Test
     public void testNoLoneSurrogates() {
         final int length = 5000;
         String str = new RandomStringGenerator.Builder().build().generate(length);


Mime
View raw message