diff --git a/src/main/java/com/github/javafaker/service/RandomService.java b/src/main/java/com/github/javafaker/service/RandomService.java index 7ad4a276..5b8d97df 100644 --- a/src/main/java/com/github/javafaker/service/RandomService.java +++ b/src/main/java/com/github/javafaker/service/RandomService.java @@ -1,5 +1,6 @@ package com.github.javafaker.service; +import java.util.List; import java.util.Random; public class RandomService { @@ -50,4 +51,37 @@ public class RandomService { public Boolean nextBoolean() { return random.nextBoolean(); } + + /** + * Returns a random element from an array. + * + * @param array The array to take a random element fom. + * @param The type of the elements in the array. + * @return A randomly selected element from the array. + */ + public E nextElement(E[] array) { + return array[this.nextInt(array.length)]; + } + + /** + * Returns a random element from a list. + * + * @param list The list to take a random element fom. + * @param The type of the elements in the list. + * @return A randomly selected element from the list. + */ + public E nextElement(List list) { + return list.get(this.nextInt(list.size())); + } + + /** + * Returns a random enumeration value + * + * @param enumeration The enumeration to take a random value from + * @param The type of the enumeration + * @return A randomly selected enumeration value + */ + public > E nextEnumValue(Class enumeration) { + return nextElement(enumeration.getEnumConstants()); + } } diff --git a/src/test/java/com/github/javafaker/service/RandomServiceTest.java b/src/test/java/com/github/javafaker/service/RandomServiceTest.java index bff74d7c..7e4e4602 100644 --- a/src/test/java/com/github/javafaker/service/RandomServiceTest.java +++ b/src/test/java/com/github/javafaker/service/RandomServiceTest.java @@ -1,16 +1,17 @@ package com.github.javafaker.service; import com.github.javafaker.AbstractFakerTest; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import java.util.Random; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; -import java.util.Arrays; -import java.util.Collection; -import java.util.Random; - import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.Matchers.greaterThan; +import static org.hamcrest.Matchers.isIn; import static org.hamcrest.Matchers.lessThan; import static org.junit.Assert.assertThat; @@ -55,4 +56,35 @@ public class RandomServiceTest extends AbstractFakerTest { assertThat(randomService.nextLong(Long.MAX_VALUE), greaterThan(0L)); assertThat(randomService.nextLong(Long.MAX_VALUE), lessThan(Long.MAX_VALUE)); } + + @Test + public void testNextArrayElement() { + Integer[] array = new Integer[] { 1, 2, 3, 5, 8, 13, 21 }; + + for (int i = 1; i < 10; i++) { + assertThat(randomService.nextElement(array), isIn(array)); + } + } + + @Test + public void testNextListElement() { + List list = Arrays.asList(new Integer[] { 1, 2, 3, 5, 8, 13, 21 }); + + for (int i = 1; i < 10; i++) { + assertThat(randomService.nextElement(list), isIn(list)); + } + } + + @Test + public void testNextEnumValue() { + for (int i = 1; i < 10; i++) { + assertThat(randomService.nextEnumValue(TestEnum.class), isIn(TestEnum.values())); + } + } + + private enum TestEnum { + ONE, + TWO, + THREE + } }