Skip to content

Commit

Permalink
yegor256#790: Create RandomText - replace int with Scalar<Integer>
Browse files Browse the repository at this point in the history
  • Loading branch information
proshin-roman committed May 8, 2018
1 parent 2b934bd commit e28766b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
40 changes: 33 additions & 7 deletions src/main/java/org/cactoos/text/RandomText.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -53,7 +55,7 @@ public final class RandomText implements Text {
/**
* Length of generated text.
*/
private final int length;
private final Scalar<Integer> length;

/**
* Characters index randomizer.
Expand All @@ -64,14 +66,26 @@ 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
);
}

/**
* Ctor.
* @param len Length of generated text.
*/
public RandomText(final Integer len) {
this(() -> len);
}

/**
* Ctor.
* @param len Length of generated text.
*/
public RandomText(final Scalar<Integer> len) {
this(
new ListOf<>(
'!', '"', '#', '$', '%', '&', '\'', '(', ')', '*',
Expand Down Expand Up @@ -104,7 +118,9 @@ public RandomText(final Character... chrs) {
public RandomText(final List<Character> chrs) {
this(
chrs,
new SecureRandom().nextInt(RandomText.MAX_RANDOM_LENGTH - 1) + 1
() -> new SecureRandom().nextInt(
RandomText.MAX_RANDOM_LENGTH - 1
) + 1
);
}

Expand All @@ -114,6 +130,15 @@ public RandomText(final List<Character> 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<Integer> len, final Character... chrs) {
this(new ListOf<>(chrs), len);
}

Expand All @@ -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<Character> chrs, final int len) {
public RandomText(final List<Character> chrs, final Scalar<Integer> len) {
this(chrs, len, new SecureRandom());
}

Expand All @@ -132,7 +157,7 @@ public RandomText(final List<Character> chrs, final int len) {
* @param len Length of generated text.
* @param rnd Characters index randomizer.
*/
public RandomText(final List<Character> chrs, final int len,
public RandomText(final List<Character> chrs, final Scalar<Integer> len,
final Random rnd) {
this.characters = chrs;
this.length = len;
Expand All @@ -141,9 +166,10 @@ public RandomText(final List<Character> 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();
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/org/cactoos/text/RandomTextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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(),
Expand All @@ -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')
Expand Down

0 comments on commit e28766b

Please sign in to comment.