Skip to content
Permalink
Browse files
Use diamond operator
  • Loading branch information
aherbert committed Aug 16, 2021
1 parent eef475e commit 6767b5cc0c5cf3340a3cafbf9c1f267d4a01d066
Showing 1 changed file with 32 additions and 32 deletions.
@@ -49,21 +49,21 @@ public void testSample() {
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 void testSample() {
@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 void testSampleWhole() {
@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 void testSamplePrecondition1() {
@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 void testShuffle() {

@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 void testShuffleTail() {

@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 void testShuffleHead() {
*/
@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 void testShuffleMatchesPermutationSamplerShuffle() {
*/
@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 void testShuffleMatchesPermutationSamplerShuffleDirectional() {
@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 void testShuffleWithSmallLinkedList() {
// 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 void testShuffleWithSmallLinkedList() {
* 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 @@ private <T> boolean compare(List<T> orig,
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]);
}

0 comments on commit 6767b5c

Please sign in to comment.