From fab9e4b32ff7c57a678ceff0269d906f54b96ce8 Mon Sep 17 00:00:00 2001 From: Dennis Schroer Date: Mon, 10 Apr 2017 15:35:58 +0200 Subject: [PATCH 1/3] Add methods to select a random element from arrays, items or enumerations --- .../javafaker/service/RandomService.java | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/main/java/com/github/javafaker/service/RandomService.java b/src/main/java/com/github/javafaker/service/RandomService.java index 7ad4a276..967c8d86 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,40 @@ 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 enumberation to take a random value from + * @param The type of the enumeration + * @return A randomly selected emumeration value + */ + public > E nextEnumValue(Class enumeration) + { + return nextElement(enumeration.getEnumConstants()); + } } From 1f8c8b2457be6bf0878355e6df107fdbe2634142 Mon Sep 17 00:00:00 2001 From: Dennis Schroer Date: Mon, 10 Apr 2017 15:46:00 +0200 Subject: [PATCH 2/3] Add test for random collection element methods --- .../javafaker/service/RandomServiceTest.java | 40 +++++++++++++++++-- 1 file changed, 36 insertions(+), 4 deletions(-) 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 + } } From 97bc261e2911abc79b245f43100729619a50ad81 Mon Sep 17 00:00:00 2001 From: Dennis Schroer Date: Wed, 12 Apr 2017 09:41:02 +0200 Subject: [PATCH 3/3] Fix code formatting --- .../com/github/javafaker/service/RandomService.java | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/github/javafaker/service/RandomService.java b/src/main/java/com/github/javafaker/service/RandomService.java index 967c8d86..5b8d97df 100644 --- a/src/main/java/com/github/javafaker/service/RandomService.java +++ b/src/main/java/com/github/javafaker/service/RandomService.java @@ -59,8 +59,7 @@ public class RandomService { * @param The type of the elements in the array. * @return A randomly selected element from the array. */ - public E nextElement(E[] array) - { + public E nextElement(E[] array) { return array[this.nextInt(array.length)]; } @@ -71,20 +70,18 @@ public class RandomService { * @param The type of the elements in the list. * @return A randomly selected element from the list. */ - public E nextElement(List list) - { + public E nextElement(List list) { return list.get(this.nextInt(list.size())); } /** * Returns a random enumeration value * - * @param enumeration The enumberation to take a random value from + * @param enumeration The enumeration to take a random value from * @param The type of the enumeration - * @return A randomly selected emumeration value + * @return A randomly selected enumeration value */ - public > E nextEnumValue(Class enumeration) - { + public > E nextEnumValue(Class enumeration) { return nextElement(enumeration.getEnumConstants()); } }