moved files to article-specific folder

This commit is contained in:
Christian Jaimes 2022-04-27 06:51:13 -04:00
parent 7093bdc456
commit 4495b45d0b
4 changed files with 40 additions and 19 deletions

View File

@ -1,25 +1,11 @@
package com.baeldung.enums; package com.baeldung.enums;
import java.util.Random;
/** /**
* Represents directions. * Represents directions.
*/ */
public enum Direction { public enum Direction {
EAST, WEST, SOUTH, NORTH; 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. * Finds direction by name.
* *

View File

@ -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)];
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.enums.randomenum;
import java.util.Random;
public class RandomEnumGenerator<T extends Enum<T>> {
private static final Random PRNG = new Random();
private final T[] values;
public RandomEnumGenerator(Class<T> e) {
values = e.getEnumConstants();
}
public T randomEnum() {
return values[PRNG.nextInt(values.length)];
}
}

View File

@ -1,12 +1,8 @@
package com.baeldung.enums; package com.baeldung.enums.randomenum;
import org.assertj.core.api.Assertions;
import org.junit.Test; import org.junit.Test;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
public class RandomEnumUnitTest { public class RandomEnumUnitTest {