diff --git a/src/main/java/com/valkryst/VNameGenerator/generator/CombinatorialGenerator.java b/src/main/java/com/valkryst/VNameGenerator/generator/CombinatorialGenerator.java index f09ce8f..e441d0f 100644 --- a/src/main/java/com/valkryst/VNameGenerator/generator/CombinatorialGenerator.java +++ b/src/main/java/com/valkryst/VNameGenerator/generator/CombinatorialGenerator.java @@ -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; @@ -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); } diff --git a/src/main/java/com/valkryst/VNameGenerator/generator/ConsonantVowelGenerator.java b/src/main/java/com/valkryst/VNameGenerator/generator/ConsonantVowelGenerator.java index efcf2cf..8589876 100644 --- a/src/main/java/com/valkryst/VNameGenerator/generator/ConsonantVowelGenerator.java +++ b/src/main/java/com/valkryst/VNameGenerator/generator/ConsonantVowelGenerator.java @@ -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; @@ -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); diff --git a/src/main/java/com/valkryst/VNameGenerator/generator/NameGenerator.java b/src/main/java/com/valkryst/VNameGenerator/generator/NameGenerator.java index 5d23a61..0c82204 100644 --- a/src/main/java/com/valkryst/VNameGenerator/generator/NameGenerator.java +++ b/src/main/java/com/valkryst/VNameGenerator/generator/NameGenerator.java @@ -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 * (0.5 * maxLength) and maxLength.