From 4495b45d0b450d6531bd49d882fc7a5bf26666d3 Mon Sep 17 00:00:00 2001 From: Christian Jaimes Date: Wed, 27 Apr 2022 06:51:13 -0400 Subject: [PATCH] moved files to article-specific folder --- .../java/com/baeldung/enums/Direction.java | 14 ------------ .../baeldung/enums/randomenum/Direction.java | 22 +++++++++++++++++++ .../enums/randomenum/RandomEnumGenerator.java | 17 ++++++++++++++ .../{ => randomenum}/RandomEnumUnitTest.java | 6 +---- 4 files changed, 40 insertions(+), 19 deletions(-) create mode 100644 core-java-modules/core-java-lang-oop-types-2/src/main/java/com/baeldung/enums/randomenum/Direction.java create mode 100644 core-java-modules/core-java-lang-oop-types-2/src/main/java/com/baeldung/enums/randomenum/RandomEnumGenerator.java rename core-java-modules/core-java-lang-oop-types-2/src/test/java/com/baeldung/enums/{ => randomenum}/RandomEnumUnitTest.java (79%) diff --git a/core-java-modules/core-java-lang-oop-types-2/src/main/java/com/baeldung/enums/Direction.java b/core-java-modules/core-java-lang-oop-types-2/src/main/java/com/baeldung/enums/Direction.java index 76b63bdf50..935aca4d65 100644 --- a/core-java-modules/core-java-lang-oop-types-2/src/main/java/com/baeldung/enums/Direction.java +++ b/core-java-modules/core-java-lang-oop-types-2/src/main/java/com/baeldung/enums/Direction.java @@ -1,25 +1,11 @@ package com.baeldung.enums; -import java.util.Random; - /** * Represents directions. */ public enum Direction { EAST, WEST, SOUTH, NORTH; - private static final Random PRNG = new Random(); - - /** - * Generate a random direction. - * - * @return a random direction - */ - public static Direction randomDirection() { - Direction[] directions = values(); - return directions[PRNG.nextInt(directions.length)]; - } - /** * Finds direction by name. * diff --git a/core-java-modules/core-java-lang-oop-types-2/src/main/java/com/baeldung/enums/randomenum/Direction.java b/core-java-modules/core-java-lang-oop-types-2/src/main/java/com/baeldung/enums/randomenum/Direction.java new file mode 100644 index 0000000000..05b398371c --- /dev/null +++ b/core-java-modules/core-java-lang-oop-types-2/src/main/java/com/baeldung/enums/randomenum/Direction.java @@ -0,0 +1,22 @@ +package com.baeldung.enums.randomenum; + +import java.util.Random; + +/** + * Represents directions. + */ +public enum Direction { + EAST, WEST, SOUTH, NORTH; + + private static final Random PRNG = new Random(); + + /** + * Generate a random direction. + * + * @return a random direction + */ + public static Direction randomDirection() { + Direction[] directions = values(); + return directions[PRNG.nextInt(directions.length)]; + } +} diff --git a/core-java-modules/core-java-lang-oop-types-2/src/main/java/com/baeldung/enums/randomenum/RandomEnumGenerator.java b/core-java-modules/core-java-lang-oop-types-2/src/main/java/com/baeldung/enums/randomenum/RandomEnumGenerator.java new file mode 100644 index 0000000000..33d793062d --- /dev/null +++ b/core-java-modules/core-java-lang-oop-types-2/src/main/java/com/baeldung/enums/randomenum/RandomEnumGenerator.java @@ -0,0 +1,17 @@ +package com.baeldung.enums.randomenum; + +import java.util.Random; + +public class RandomEnumGenerator> { + + private static final Random PRNG = new Random(); + private final T[] values; + + public RandomEnumGenerator(Class e) { + values = e.getEnumConstants(); + } + + public T randomEnum() { + return values[PRNG.nextInt(values.length)]; + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-lang-oop-types-2/src/test/java/com/baeldung/enums/RandomEnumUnitTest.java b/core-java-modules/core-java-lang-oop-types-2/src/test/java/com/baeldung/enums/randomenum/RandomEnumUnitTest.java similarity index 79% rename from core-java-modules/core-java-lang-oop-types-2/src/test/java/com/baeldung/enums/RandomEnumUnitTest.java rename to core-java-modules/core-java-lang-oop-types-2/src/test/java/com/baeldung/enums/randomenum/RandomEnumUnitTest.java index 937b376c29..76b4bf1e74 100644 --- a/core-java-modules/core-java-lang-oop-types-2/src/test/java/com/baeldung/enums/RandomEnumUnitTest.java +++ b/core-java-modules/core-java-lang-oop-types-2/src/test/java/com/baeldung/enums/randomenum/RandomEnumUnitTest.java @@ -1,12 +1,8 @@ -package com.baeldung.enums; +package com.baeldung.enums.randomenum; -import org.assertj.core.api.Assertions; import org.junit.Test; -import java.util.Optional; - import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; public class RandomEnumUnitTest {