From 7820011d63ee33150f0d034228d3e22127c6db55 Mon Sep 17 00:00:00 2001 From: Jiri Danek Date: Thu, 25 Apr 2019 21:42:17 +0200 Subject: [PATCH] ARTEMIS-2320 Fix MathAbsoluteRandom errorprone warning Math.abs does not always give a positive result. Please consider other methods for positive random numbers. --- .../utils/collections/ConcurrentLongHashSetTest.java | 10 +++++----- .../utils/collections/PriorityCollectionTest.java | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/artemis-commons/src/test/java/org/apache/activemq/artemis/utils/collections/ConcurrentLongHashSetTest.java b/artemis-commons/src/test/java/org/apache/activemq/artemis/utils/collections/ConcurrentLongHashSetTest.java index d294243d08..ffe7adeef8 100644 --- a/artemis-commons/src/test/java/org/apache/activemq/artemis/utils/collections/ConcurrentLongHashSetTest.java +++ b/artemis-commons/src/test/java/org/apache/activemq/artemis/utils/collections/ConcurrentLongHashSetTest.java @@ -25,10 +25,10 @@ import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.List; -import java.util.Random; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; +import java.util.concurrent.ThreadLocalRandom; import org.junit.Test; @@ -131,10 +131,10 @@ public class ConcurrentLongHashSetTest { final int threadIdx = i; futures.add(executor.submit(() -> { - Random random = new Random(); + final ThreadLocalRandom random = ThreadLocalRandom.current(); for (int j = 0; j < N; j++) { - long key = Math.abs(random.nextLong()); + long key = random.nextLong(Long.MAX_VALUE); // Ensure keys are unique key -= key % (threadIdx + 1); @@ -165,10 +165,10 @@ public class ConcurrentLongHashSetTest { final int threadIdx = i; futures.add(executor.submit(() -> { - Random random = new Random(); + final ThreadLocalRandom random = ThreadLocalRandom.current(); for (int j = 0; j < N; j++) { - long key = Math.abs(random.nextLong()); + long key = random.nextLong(Long.MAX_VALUE); // Ensure keys are unique key -= key % (threadIdx + 1); diff --git a/artemis-commons/src/test/java/org/apache/activemq/artemis/utils/collections/PriorityCollectionTest.java b/artemis-commons/src/test/java/org/apache/activemq/artemis/utils/collections/PriorityCollectionTest.java index 97b1a6d34d..cab7804a33 100644 --- a/artemis-commons/src/test/java/org/apache/activemq/artemis/utils/collections/PriorityCollectionTest.java +++ b/artemis-commons/src/test/java/org/apache/activemq/artemis/utils/collections/PriorityCollectionTest.java @@ -27,11 +27,11 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Objects; -import java.util.Random; import java.util.concurrent.CopyOnWriteArraySet; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; +import java.util.concurrent.ThreadLocalRandom; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -94,10 +94,10 @@ public class PriorityCollectionTest { final int threadIdx = i; futures.add(executor.submit(() -> { - Random random = new Random(); + final ThreadLocalRandom random = ThreadLocalRandom.current(); for (int j = 0; j < N; j++) { - long key = Math.abs(random.nextLong()); + long key = random.nextLong(Long.MAX_VALUE); // Ensure keys are unique key -= key % (threadIdx + 1); @@ -128,10 +128,10 @@ public class PriorityCollectionTest { final int threadIdx = i; futures.add(executor.submit(() -> { - Random random = new Random(); + ThreadLocalRandom random = ThreadLocalRandom.current(); for (int j = 0; j < N; j++) { - long key = Math.abs(random.nextLong()); + long key = random.nextLong(Long.MAX_VALUE); // Ensure keys are unique key -= key % (threadIdx + 1);