You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ah...@apache.org on 2021/08/17 07:08:18 UTC
[commons-rng] 10/13: Use diamond operator
This is an automated email from the ASF dual-hosted git repository.
aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-rng.git
commit 6767b5cc0c5cf3340a3cafbf9c1f267d4a01d066
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Mon Aug 16 22:10:12 2021 +0100
Use diamond operator
---
.../commons/rng/sampling/ListSamplerTest.java | 64 +++++++++++-----------
1 file changed, 32 insertions(+), 32 deletions(-)
diff --git a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/ListSamplerTest.java b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/ListSamplerTest.java
index 1c15f74..76f2e10 100644
--- a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/ListSamplerTest.java
+++ b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/ListSamplerTest.java
@@ -49,21 +49,21 @@ public class ListSamplerTest {
final long[] observed = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
final double[] expected = {100, 100, 100, 100, 100, 100, 100, 100, 100, 100};
- final HashSet<String> cPop = new HashSet<String>(); // {0, 1, 2, 3, 4}.
+ final HashSet<String> cPop = new HashSet<>(); // {0, 1, 2, 3, 4}.
for (int i = 0; i < 5; i++) {
cPop.add(Integer.toString(i));
}
- final List<Set<String>> sets = new ArrayList<Set<String>>(); // 2-sets from 5.
+ final List<Set<String>> sets = new ArrayList<>(); // 2-sets from 5.
for (int i = 0; i < 10; i++) {
- final HashSet<String> hs = new HashSet<String>();
+ final HashSet<String> hs = new HashSet<>();
hs.add(c[i][0]);
hs.add(c[i][1]);
sets.add(hs);
}
for (int i = 0; i < 1000; i++) {
- observed[findSample(sets, ListSampler.sample(rng, new ArrayList<String>(cPop), 2))]++;
+ observed[findSample(sets, ListSampler.sample(rng, new ArrayList<>(cPop), 2))]++;
}
// Pass if we cannot reject null hypothesis that distributions are the same.
@@ -73,7 +73,7 @@ public class ListSamplerTest {
@Test
public void testSampleWhole() {
// Sample of size = size of collection must return the same collection.
- final List<String> list = new ArrayList<String>();
+ final List<String> list = new ArrayList<>();
list.add("one");
final List<String> one = ListSampler.sample(rng, list, 1);
@@ -84,7 +84,7 @@ public class ListSamplerTest {
@Test
public void testSamplePrecondition1() {
// Must fail for sample size > collection size.
- final List<String> list = new ArrayList<String>();
+ final List<String> list = new ArrayList<>();
list.add("one");
Assertions.assertThrows(IllegalArgumentException.class,
() -> ListSampler.sample(rng, list, 2));
@@ -93,25 +93,25 @@ public class ListSamplerTest {
@Test
public void testSamplePrecondition2() {
// Must fail for empty collection.
- final List<String> list = new ArrayList<String>();
+ final List<String> list = new ArrayList<>();
Assertions.assertThrows(IllegalArgumentException.class,
() -> ListSampler.sample(rng, list, 1));
}
@Test
public void testShuffle() {
- final List<Integer> orig = new ArrayList<Integer>();
+ final List<Integer> orig = new ArrayList<>();
for (int i = 0; i < 10; i++) {
orig.add((i + 1) * rng.nextInt());
}
- final List<Integer> arrayList = new ArrayList<Integer>(orig);
+ final List<Integer> arrayList = new ArrayList<>(orig);
ListSampler.shuffle(rng, arrayList);
// Ensure that at least one entry has moved.
Assertions.assertTrue(compare(orig, arrayList, 0, orig.size(), false), "ArrayList");
- final List<Integer> linkedList = new LinkedList<Integer>(orig);
+ final List<Integer> linkedList = new LinkedList<>(orig);
ListSampler.shuffle(rng, linkedList);
// Ensure that at least one entry has moved.
@@ -120,11 +120,11 @@ public class ListSamplerTest {
@Test
public void testShuffleTail() {
- final List<Integer> orig = new ArrayList<Integer>();
+ final List<Integer> orig = new ArrayList<>();
for (int i = 0; i < 10; i++) {
orig.add((i + 1) * rng.nextInt());
}
- final List<Integer> list = new ArrayList<Integer>(orig);
+ final List<Integer> list = new ArrayList<>(orig);
final int start = 4;
ListSampler.shuffle(rng, list, start, false);
@@ -138,11 +138,11 @@ public class ListSamplerTest {
@Test
public void testShuffleHead() {
- final List<Integer> orig = new ArrayList<Integer>();
+ final List<Integer> orig = new ArrayList<>();
for (int i = 0; i < 10; i++) {
orig.add((i + 1) * rng.nextInt());
}
- final List<Integer> list = new ArrayList<Integer>(orig);
+ final List<Integer> list = new ArrayList<>(orig);
final int start = 4;
ListSampler.shuffle(rng, list, start, true);
@@ -161,13 +161,13 @@ public class ListSamplerTest {
*/
@Test
public void testShuffleMatchesPermutationSamplerShuffle() {
- final List<Integer> orig = new ArrayList<Integer>();
+ final List<Integer> orig = new ArrayList<>();
for (int i = 0; i < 10; i++) {
orig.add((i + 1) * rng.nextInt());
}
- assertShuffleMatchesPermutationSamplerShuffle(new ArrayList<Integer>(orig));
- assertShuffleMatchesPermutationSamplerShuffle(new LinkedList<Integer>(orig));
+ assertShuffleMatchesPermutationSamplerShuffle(new ArrayList<>(orig));
+ assertShuffleMatchesPermutationSamplerShuffle(new LinkedList<>(orig));
}
/**
@@ -177,15 +177,15 @@ public class ListSamplerTest {
*/
@Test
public void testShuffleMatchesPermutationSamplerShuffleDirectional() {
- final List<Integer> orig = new ArrayList<Integer>();
+ final List<Integer> orig = new ArrayList<>();
for (int i = 0; i < 10; i++) {
orig.add((i + 1) * rng.nextInt());
}
- assertShuffleMatchesPermutationSamplerShuffle(new ArrayList<Integer>(orig), 4, true);
- assertShuffleMatchesPermutationSamplerShuffle(new ArrayList<Integer>(orig), 4, false);
- assertShuffleMatchesPermutationSamplerShuffle(new LinkedList<Integer>(orig), 4, true);
- assertShuffleMatchesPermutationSamplerShuffle(new LinkedList<Integer>(orig), 4, false);
+ assertShuffleMatchesPermutationSamplerShuffle(new ArrayList<>(orig), 4, true);
+ assertShuffleMatchesPermutationSamplerShuffle(new ArrayList<>(orig), 4, false);
+ assertShuffleMatchesPermutationSamplerShuffle(new LinkedList<>(orig), 4, true);
+ assertShuffleMatchesPermutationSamplerShuffle(new LinkedList<>(orig), 4, false);
}
/**
@@ -195,7 +195,7 @@ public class ListSamplerTest {
@Test
public void testShuffleWithSmallLinkedList() {
final int size = 3;
- final List<Integer> orig = new ArrayList<Integer>();
+ final List<Integer> orig = new ArrayList<>();
for (int i = 0; i < size; i++) {
orig.add((i + 1) * rng.nextInt());
}
@@ -205,7 +205,7 @@ public class ListSamplerTest {
// So repeat test that the small shuffle matches the PermutationSampler.
// 10 times is (1/6)^10 or 1 in 60,466,176 of no change.
for (int i = 0; i < 10; i++) {
- assertShuffleMatchesPermutationSamplerShuffle(new LinkedList<Integer>(orig), size - 1, true);
+ assertShuffleMatchesPermutationSamplerShuffle(new LinkedList<>(orig), size - 1, true);
}
}
@@ -216,11 +216,11 @@ public class ListSamplerTest {
* the same; if {@code same == false}, return {@code true} if at
* least one entry is different.
*/
- private <T> boolean compare(List<T> orig,
- List<T> list,
- int start,
- int end,
- boolean same) {
+ private static <T> boolean compare(List<T> orig,
+ List<T> list,
+ int start,
+ int end,
+ boolean same) {
for (int i = start; i < end; i++) {
if (!orig.get(i).equals(list.get(i))) {
return same ? false : true;
@@ -229,12 +229,12 @@ public class ListSamplerTest {
return same ? true : false;
}
- private <T extends Set<String>> int findSample(List<T> u,
- Collection<String> sampList) {
+ private static <T extends Set<String>> int findSample(List<T> u,
+ Collection<String> sampList) {
final String[] samp = sampList.toArray(new String[sampList.size()]);
for (int i = 0; i < u.size(); i++) {
final T set = u.get(i);
- final HashSet<String> sampSet = new HashSet<String>();
+ final HashSet<String> sampSet = new HashSet<>();
for (int j = 0; j < samp.length; j++) {
sampSet.add(samp[j]);
}