diff --git a/README.md b/README.md
index 012cfe5..59ddbbe 100644
--- a/README.md
+++ b/README.md
@@ -130,6 +130,7 @@ import static io.qala.datagen.RandomShortApi.*;
|`from("A", "B", "C", "D").sample()` | `sample("A", "B", "C")` | `"C"`
|`from("A", "B", "C", "D").sample(2)` | `sampleMultiple(2, "A", "B", "C")` | `["B", "A"]`
|`from("A", "B").sampleWithReplacement(3)` | | `["A", "A", "B"]`
+|`from("A", "B", "C").sampleWithReplacement(3)` | shuffled("A", "B", "C") | `["C", "A", "B"]`
## Java Date
diff --git a/datagen/pom.xml b/datagen/pom.xml
index 9ce6fd7..2fb094b 100644
--- a/datagen/pom.xml
+++ b/datagen/pom.xml
@@ -5,7 +5,7 @@
qala-datagen-parent
io.qala.datagen
- 2.3.0
+ 2.4.0
4.0.0
diff --git a/datagen/src/main/java/io/qala/datagen/RandomElements.java b/datagen/src/main/java/io/qala/datagen/RandomElements.java
index ce823d5..6fdf92e 100644
--- a/datagen/src/main/java/io/qala/datagen/RandomElements.java
+++ b/datagen/src/main/java/io/qala/datagen/RandomElements.java
@@ -1,9 +1,6 @@
package io.qala.datagen;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.List;
+import java.util.*;
import static io.qala.datagen.RandomShortApi.integer;
@@ -90,6 +87,17 @@ public List sampleWithReplacement(int nToReturn) {
return result;
}
+ /**
+ * Returns a collection with the elements passed in the constructor but with a random order.
+ *
+ * @return new List of the same size, but with elements in random order
+ */
+ public List shuffled() {
+ List result = new ArrayList(this.elements);
+ Collections.shuffle(result);
+ return result;
+ }
+
private int size() {
return elements.size();
}
diff --git a/datagen/src/main/java/io/qala/datagen/RandomShortApi.java b/datagen/src/main/java/io/qala/datagen/RandomShortApi.java
index cc60613..9c46ed0 100644
--- a/datagen/src/main/java/io/qala/datagen/RandomShortApi.java
+++ b/datagen/src/main/java/io/qala/datagen/RandomShortApi.java
@@ -330,6 +330,24 @@ public static List sampleMultiple(int nToReturn, Collection toSampleFr
@SafeVarargs public static List sampleMultiple(int nToReturn, T... toSampleFrom) {
return from(toSampleFrom).sample(nToReturn);
}
+ /**
+ * Returns new collection with the same elements but in random order.
+ *
+ * @param toShuffle collection to shuffle
+ * @return new List of the same size, but with elements in random order
+ */
+ public static List shuffled(Collection toShuffle) {
+ return from(toShuffle).shuffled();
+ }
+ /**
+ * Returns a List with the specified elements in random order.
+ *
+ * @param toShuffle elements to shuffle
+ * @return a List with the specified elements in random order
+ */
+ @SafeVarargs public static List shuffled(T... toShuffle) {
+ return from(toShuffle).shuffled();
+ }
/**
* Invokes one and only one of the specified functions. This is an API for Java8 Lambdas.
diff --git a/datagen/src/test/java/io/qala/datagen/RandomElementsTest.java b/datagen/src/test/java/io/qala/datagen/RandomElementsTest.java
index 62ad477..3bf4fc2 100644
--- a/datagen/src/test/java/io/qala/datagen/RandomElementsTest.java
+++ b/datagen/src/test/java/io/qala/datagen/RandomElementsTest.java
@@ -9,21 +9,20 @@
import static io.qala.datagen.RandomElements.from;
import static io.qala.datagen.RandomShortApi.*;
-import static io.qala.datagen.RandomValue.length;
-import static io.qala.datagen.RandomValue.upTo;
+import static io.qala.datagen.RandomValue.*;
import static java.util.Collections.emptyList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.*;
@DisplayName("Random Elements")
-public class RandomElementsTest {
+class RandomElementsTest {
@Test void canSampleTheOnlyElementOfList() {
assertEquals("ABC", from("ABC").sample());
}
@Test void canSampleOneElementFromList() {
- List list = upTo(10).alphanumerics();
+ List list = between(0, 10).alphanumerics();
assertThat(list, hasItem(from(list).sample()));
assertThat(list, hasItem(sample(list)));
}
@@ -58,21 +57,48 @@ public class RandomElementsTest {
@Test void throwsIfCollectionToSampleFromIsEmpty() {
assertThrows(IllegalArgumentException.class, () -> from().sample(3));
assertThrows(IllegalArgumentException.class, () -> sampleMultiple(0));
- assertThrows(IllegalArgumentException.class, () -> sample());
+ assertThrows(IllegalArgumentException.class, RandomShortApi::sample);
}
@Test void canSampleMultipleElementsFromList() {
- List population = upTo(10).alphanumerics(5, 10);
+ List population = between(0, 10).alphanumerics(5, 10);
List sample = from(population).sample(5);
assertEquals(5, sample.size());
- assertThat(population, hasItems(sample.toArray(new String[sample.size()])));
+ assertThat(population, hasItems(sample.toArray(new String[0])));
}
@Test void samplesDuplicateElements_ifSampleSizeLargerThanPopulation_andSamplingIsWithReplacement() {
- List population = upTo(10).alphanumerics(2);
+ System.out.println(from("A", "B", "C").shuffled());
+ List population = between(0, 10).alphanumerics(2);
List sample = from(population).sampleWithReplacement(5);
assertEquals(5, sample.size());
- assertThat(population, hasItems(sample.toArray(new String[sample.size()])));
+ assertThat(population, hasItems(sample.toArray(new String[0])));
+ }
+ @Test void shuffledCollectionSharesAllElementsWithOriginalCollection() {
+ List original = between(0, 10).alphanumerics(2);
+ List shuffled = from(original).shuffled();
+
+ assertNotSame(original, shuffled);
+ assertEquals(original.size(), shuffled.size());
+ assertTrue(original.containsAll(shuffled));
+ assertTrue(shuffled.containsAll(original));
+ }
+ @Test void shuffledCollectionHasElementsInDifferentOrder_fromOriginalCollection() {
+ List original = length(10).alphanumerics(200);
+ List shuffled = shuffled(original);
+
+ assertNotEquals(original, shuffled);
+ assertTrue(original.containsAll(shuffled));
+ assertTrue(shuffled.containsAll(original));
+ }
+ @Test void shuffledCollectionHasElementsInDifferentOrder_fromOriginalArray() {
+ List original = length(10).alphanumerics(200);
+ String[] originalArray = original.toArray(new String[]{});
+ List shuffled = shuffled(originalArray);
+
+ assertNotEquals(original, shuffled);
+ assertTrue(original.containsAll(shuffled));
+ assertTrue(shuffled.containsAll(original));
}
@Test void nullOrObj_returnsNull_sometimes() {
diff --git a/examples/pom.xml b/examples/pom.xml
index 5ca2aae..7f5ddc9 100644
--- a/examples/pom.xml
+++ b/examples/pom.xml
@@ -5,7 +5,7 @@
qala-datagen-parent
io.qala.datagen
- 2.3.0
+ 2.4.0
4.0.0
diff --git a/java8types/pom.xml b/java8types/pom.xml
index 6854088..c51dfe8 100644
--- a/java8types/pom.xml
+++ b/java8types/pom.xml
@@ -5,7 +5,7 @@
qala-datagen-parent
io.qala.datagen
- 2.3.0
+ 2.4.0
4.0.0
diff --git a/junit5/pom.xml b/junit5/pom.xml
index 1748496..6abdee5 100644
--- a/junit5/pom.xml
+++ b/junit5/pom.xml
@@ -5,7 +5,7 @@
qala-datagen-parent
io.qala.datagen
- 2.3.0
+ 2.4.0
4.0.0
diff --git a/pom.xml b/pom.xml
index 4e4c08d..45b751e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -7,7 +7,7 @@
io.qala.datagen
qala-datagen-parent
pom
- 2.3.0
+ 2.4.0