Skip to content

Commit

Permalink
Add randomArrayElement to NameGenerator and update subclasses to …
Browse files Browse the repository at this point in the history
…use it
  • Loading branch information
Valkryst committed Dec 19, 2021
1 parent 84297d2 commit 23fd837
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import lombok.NonNull;

import java.util.concurrent.ThreadLocalRandom;

public final class CombinatorialGenerator extends NameGenerator {
/** A set of name beginnings. */
private String[] beginnings;
Expand Down Expand Up @@ -52,19 +50,18 @@ public String generate(int maxLength) {
maxLength = super.randomizeMaxLength(maxLength);

final var stringBuilder = new StringBuilder();
final var threadLocalRandom = ThreadLocalRandom.current();

final var beginning = beginnings[threadLocalRandom.nextInt(beginnings.length)];
final var beginning = super.randomArrayElement(beginnings);
stringBuilder.append(beginning);

if (middles.length != 0) {
while (stringBuilder.length() < maxLength) {
stringBuilder.append(middles[threadLocalRandom.nextInt(middles.length)]);
stringBuilder.append(super.randomArrayElement(middles));
}
}

if (maxLength > 1) {
final var temp = endings[threadLocalRandom.nextInt(endings.length)];
final var temp = super.randomArrayElement(endings);
stringBuilder.replace(maxLength - temp.length(), maxLength, temp);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import lombok.NonNull;

import java.util.concurrent.ThreadLocalRandom;

public final class ConsonantVowelGenerator extends NameGenerator {
/** A set of consonants. */
private String[] consonants;
Expand All @@ -27,14 +25,13 @@ public String generate(int maxLength) {
maxLength = super.randomizeMaxLength(maxLength);

final var stringBuilder = new StringBuilder();
final var threadLocalRandom = ThreadLocalRandom.current();

String temp;
while (stringBuilder.length() < maxLength) {
if (maxLength % 2 == 0) {
temp = vowels[threadLocalRandom.nextInt(vowels.length)];
temp = super.randomArrayElement(vowels);
} else {
temp = consonants[threadLocalRandom.nextInt(consonants.length)];
temp = super.randomArrayElement(consonants);
}

stringBuilder.append(temp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@ protected StringBuilder clean(final StringBuilder sb) {
return sb;
}

/**
* Returns a random element from the given array.
*
* @param array An array.
* @return A random element from the given array.
*/
protected String randomArrayElement(final String[] array) {
if (array.length == 0) {
throw new ArrayIndexOutOfBoundsException("The array is empty.");
}

return array[ThreadLocalRandom.current().nextInt(array.length)];
}

/**
* Produces a random max length which is guaranteed to be between
* <i>(0.5 * maxLength)</i> and <i>maxLength</i>.
Expand Down

0 comments on commit 23fd837

Please sign in to comment.