ARTEMIS-2320 Fix MathAbsoluteRandom errorprone warning

Math.abs does not always give a positive result. Please consider other methods for positive random numbers.
This commit is contained in:
Jiri Danek 2019-04-25 21:42:17 +02:00 committed by Michael Andre Pearce
parent f7a36300ef
commit 7820011d63
2 changed files with 10 additions and 10 deletions

View File

@ -25,10 +25,10 @@ import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.concurrent.ThreadLocalRandom;
import org.junit.Test; import org.junit.Test;
@ -131,10 +131,10 @@ public class ConcurrentLongHashSetTest {
final int threadIdx = i; final int threadIdx = i;
futures.add(executor.submit(() -> { futures.add(executor.submit(() -> {
Random random = new Random(); final ThreadLocalRandom random = ThreadLocalRandom.current();
for (int j = 0; j < N; j++) { for (int j = 0; j < N; j++) {
long key = Math.abs(random.nextLong()); long key = random.nextLong(Long.MAX_VALUE);
// Ensure keys are unique // Ensure keys are unique
key -= key % (threadIdx + 1); key -= key % (threadIdx + 1);
@ -165,10 +165,10 @@ public class ConcurrentLongHashSetTest {
final int threadIdx = i; final int threadIdx = i;
futures.add(executor.submit(() -> { futures.add(executor.submit(() -> {
Random random = new Random(); final ThreadLocalRandom random = ThreadLocalRandom.current();
for (int j = 0; j < N; j++) { for (int j = 0; j < N; j++) {
long key = Math.abs(random.nextLong()); long key = random.nextLong(Long.MAX_VALUE);
// Ensure keys are unique // Ensure keys are unique
key -= key % (threadIdx + 1); key -= key % (threadIdx + 1);

View File

@ -27,11 +27,11 @@ import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Random;
import java.util.concurrent.CopyOnWriteArraySet; import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.concurrent.ThreadLocalRandom;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
@ -94,10 +94,10 @@ public class PriorityCollectionTest {
final int threadIdx = i; final int threadIdx = i;
futures.add(executor.submit(() -> { futures.add(executor.submit(() -> {
Random random = new Random(); final ThreadLocalRandom random = ThreadLocalRandom.current();
for (int j = 0; j < N; j++) { for (int j = 0; j < N; j++) {
long key = Math.abs(random.nextLong()); long key = random.nextLong(Long.MAX_VALUE);
// Ensure keys are unique // Ensure keys are unique
key -= key % (threadIdx + 1); key -= key % (threadIdx + 1);
@ -128,10 +128,10 @@ public class PriorityCollectionTest {
final int threadIdx = i; final int threadIdx = i;
futures.add(executor.submit(() -> { futures.add(executor.submit(() -> {
Random random = new Random(); ThreadLocalRandom random = ThreadLocalRandom.current();
for (int j = 0; j < N; j++) { for (int j = 0; j < N; j++) {
long key = Math.abs(random.nextLong()); long key = random.nextLong(Long.MAX_VALUE);
// Ensure keys are unique // Ensure keys are unique
key -= key % (threadIdx + 1); key -= key % (threadIdx + 1);