From 7093bdc456b4932883f2bb5150ec3c297c0ca535 Mon Sep 17 00:00:00 2001 From: Christian Jaimes Date: Thu, 21 Apr 2022 09:29:37 -0400 Subject: [PATCH 1/3] added randomDirection method to Enum. Created RandomEnumGenerator. Added corresponding Unit Tests --- .../java/com/baeldung/enums/Direction.java | 14 ++++++++++ .../baeldung/enums/RandomEnumGenerator.java | 17 ++++++++++++ .../baeldung/enums/RandomEnumUnitTest.java | 27 +++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 core-java-modules/core-java-lang-oop-types-2/src/main/java/com/baeldung/enums/RandomEnumGenerator.java create mode 100644 core-java-modules/core-java-lang-oop-types-2/src/test/java/com/baeldung/enums/RandomEnumUnitTest.java 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 935aca4d65..76b63bdf50 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,11 +1,25 @@ 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/RandomEnumGenerator.java b/core-java-modules/core-java-lang-oop-types-2/src/main/java/com/baeldung/enums/RandomEnumGenerator.java new file mode 100644 index 0000000000..ea27286044 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-types-2/src/main/java/com/baeldung/enums/RandomEnumGenerator.java @@ -0,0 +1,17 @@ +package com.baeldung.enums; + +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/RandomEnumUnitTest.java new file mode 100644 index 0000000000..937b376c29 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-types-2/src/test/java/com/baeldung/enums/RandomEnumUnitTest.java @@ -0,0 +1,27 @@ +package com.baeldung.enums; + +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 { + + @Test + public void givenEnumType_whenUsingStaticMethod_valueIsRandomlyGenerated() { + Direction direction = Direction.randomDirection(); + assertThat(direction).isNotNull(); + assertThat(direction instanceof Direction); + } + + @Test + public void givenEnumType_whenGeneratingRandomValue_valueIsOfClassAndNotNull() { + RandomEnumGenerator reg = new RandomEnumGenerator(Direction.class); + Object direction = reg.randomEnum(); + assertThat(direction).isNotNull(); + assertThat(direction instanceof Direction); + } +} From 4495b45d0b450d6531bd49d882fc7a5bf26666d3 Mon Sep 17 00:00:00 2001 From: Christian Jaimes Date: Wed, 27 Apr 2022 06:51:13 -0400 Subject: [PATCH 2/3] 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 { From fdf79fdd879183bc363597d58adcd2460a6959ba Mon Sep 17 00:00:00 2001 From: Christian Jaimes Date: Wed, 27 Apr 2022 06:54:46 -0400 Subject: [PATCH 3/3] removed unnecessary file --- .../com/baeldung/enums/RandomEnumGenerator.java | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 core-java-modules/core-java-lang-oop-types-2/src/main/java/com/baeldung/enums/RandomEnumGenerator.java diff --git a/core-java-modules/core-java-lang-oop-types-2/src/main/java/com/baeldung/enums/RandomEnumGenerator.java b/core-java-modules/core-java-lang-oop-types-2/src/main/java/com/baeldung/enums/RandomEnumGenerator.java deleted file mode 100644 index ea27286044..0000000000 --- a/core-java-modules/core-java-lang-oop-types-2/src/main/java/com/baeldung/enums/RandomEnumGenerator.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.baeldung.enums; - -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