From e28766b78369373c2a8ff4fc9919163cc9579216 Mon Sep 17 00:00:00 2001 From: Roman Proshin Date: Tue, 8 May 2018 21:39:16 +0300 Subject: [PATCH] #790: Create RandomText - replace int with Scalar --- .../java/org/cactoos/text/RandomText.java | 40 +++++++++++++++---- .../java/org/cactoos/text/RandomTextTest.java | 6 +-- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/cactoos/text/RandomText.java b/src/main/java/org/cactoos/text/RandomText.java index 1322a94208..00b01f6ec3 100644 --- a/src/main/java/org/cactoos/text/RandomText.java +++ b/src/main/java/org/cactoos/text/RandomText.java @@ -26,8 +26,10 @@ import java.security.SecureRandom; import java.util.List; import java.util.Random; +import org.cactoos.Scalar; import org.cactoos.Text; import org.cactoos.list.ListOf; +import org.cactoos.scalar.UncheckedScalar; /** * Random text. @@ -53,7 +55,7 @@ public final class RandomText implements Text { /** * Length of generated text. */ - private final int length; + private final Scalar length; /** * Characters index randomizer. @@ -64,7 +66,11 @@ public final class RandomText implements Text { * Ctor. */ public RandomText() { - this(new SecureRandom().nextInt(RandomText.MAX_RANDOM_LENGTH - 1) + 1); + this( + () -> new SecureRandom().nextInt( + RandomText.MAX_RANDOM_LENGTH - 1 + ) + 1 + ); } /** @@ -72,6 +78,14 @@ public RandomText() { * @param len Length of generated text. */ public RandomText(final Integer len) { + this(() -> len); + } + + /** + * Ctor. + * @param len Length of generated text. + */ + public RandomText(final Scalar len) { this( new ListOf<>( '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', @@ -104,7 +118,9 @@ public RandomText(final Character... chrs) { public RandomText(final List chrs) { this( chrs, - new SecureRandom().nextInt(RandomText.MAX_RANDOM_LENGTH - 1) + 1 + () -> new SecureRandom().nextInt( + RandomText.MAX_RANDOM_LENGTH - 1 + ) + 1 ); } @@ -114,6 +130,15 @@ public RandomText(final List chrs) { * @param chrs Array of characters allowed for generating. */ public RandomText(final Integer len, final Character... chrs) { + this(() -> len, chrs); + } + + /** + * Ctor. + * @param len Length of generated text. + * @param chrs Array of characters allowed for generating. + */ + public RandomText(final Scalar len, final Character... chrs) { this(new ListOf<>(chrs), len); } @@ -122,7 +147,7 @@ public RandomText(final Integer len, final Character... chrs) { * @param chrs List of characters allowed for generating. * @param len Length of generated text. */ - public RandomText(final List chrs, final int len) { + public RandomText(final List chrs, final Scalar len) { this(chrs, len, new SecureRandom()); } @@ -132,7 +157,7 @@ public RandomText(final List chrs, final int len) { * @param len Length of generated text. * @param rnd Characters index randomizer. */ - public RandomText(final List chrs, final int len, + public RandomText(final List chrs, final Scalar len, final Random rnd) { this.characters = chrs; this.length = len; @@ -141,9 +166,10 @@ public RandomText(final List chrs, final int len, @Override public String asString() { - final StringBuilder builder = new StringBuilder(this.length); + final int len = new UncheckedScalar<>(this.length).value(); + final StringBuilder builder = new StringBuilder(len); final int bound = this.characters.size(); - for (int index = 0; index < this.length; index = index + 1) { + for (int index = 0; index < len; index = index + 1) { builder.append(this.characters.get(this.random.nextInt(bound))); } return builder.toString(); diff --git a/src/test/java/org/cactoos/text/RandomTextTest.java b/src/test/java/org/cactoos/text/RandomTextTest.java index 71db5b446f..2cf5929d59 100644 --- a/src/test/java/org/cactoos/text/RandomTextTest.java +++ b/src/test/java/org/cactoos/text/RandomTextTest.java @@ -43,7 +43,7 @@ public final class RandomTextTest { @Test - public void generatesRandomTextOfRandomLength() throws Exception { + public void generatesRandomTextOfRandomLength() { MatcherAssert.assertThat( "Generated text is empty", new RandomText().asString().length(), @@ -52,7 +52,7 @@ public void generatesRandomTextOfRandomLength() throws Exception { } @Test - public void generatesRandomTextOfSpecifiedLength() throws Exception { + public void generatesRandomTextOfSpecifiedLength() { MatcherAssert.assertThat( "Generated text has incorrect length", new RandomText(512).asString().length(), @@ -61,7 +61,7 @@ public void generatesRandomTextOfSpecifiedLength() throws Exception { } @Test - public void generatesRandomTextOfSpecifiedChars() throws Exception { + public void generatesRandomTextOfSpecifiedChars() { MatcherAssert.assertThat( "Generated text contains not allowed characters", new RandomText('a')