1010: Fixed all of the blocking and critical Sonarcloud errors (#1020)
* 1011: Added SuppressWarnings for SonarCloud errors All of these files are causing SonarCloud to report the following error: Loops should not be infinite Since these instances all require an infinite loop that will never end, these warnings should be disabled so that SonarCloud no longer reports them as error. The rule is: squid:S2189 * 1011: Made all of the randoms static and final According to SonarCloud rule: "Random" objects should be reused, randoms should not be recreated. This commit has taken all of the Randoms and made them constant variables in the files that are using them.
This commit is contained in:
committed by
Ilkka Seppälä
parent
8a4844792f
commit
7c5d5f6b0d
@ -66,6 +66,7 @@ public class App {
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
@SuppressWarnings("squid:S2189")
|
||||
public static void main(String[] args) {
|
||||
//Create an object of monitoring service which makes both local and remote calls
|
||||
var obj = new MonitoringService();
|
||||
|
@ -44,6 +44,7 @@ public class Order { //can store all transactions ids also
|
||||
public final String id;
|
||||
final float price;
|
||||
final long createdTime;
|
||||
private static final Random RANDOM = new Random();
|
||||
private static final String ALL_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
|
||||
private static final Hashtable<String, Boolean> USED_IDS = new Hashtable<String, Boolean>();
|
||||
PaymentStatus paid;
|
||||
@ -70,9 +71,8 @@ public class Order { //can store all transactions ids also
|
||||
|
||||
String createUniqueId() {
|
||||
StringBuilder random = new StringBuilder();
|
||||
Random rand = new Random();
|
||||
while (random.length() < 12) { // length of the random string.
|
||||
int index = (int) (rand.nextFloat() * ALL_CHARS.length());
|
||||
int index = (int) (RANDOM.nextFloat() * ALL_CHARS.length());
|
||||
random.append(ALL_CHARS.charAt(index));
|
||||
}
|
||||
return random.toString();
|
||||
|
@ -52,6 +52,8 @@ public class Retry<T> {
|
||||
void handleIssue(T obj, Exception e);
|
||||
}
|
||||
|
||||
private static final Random RANDOM = new Random();
|
||||
|
||||
private final Operation op;
|
||||
private final HandleErrorIssue<T> handleError;
|
||||
private final int maxAttempts;
|
||||
@ -89,8 +91,7 @@ public class Retry<T> {
|
||||
return; //return here...dont go further
|
||||
}
|
||||
try {
|
||||
Random rand = new Random();
|
||||
long testDelay = (long) Math.pow(2, this.attempts.intValue()) * 1000 + rand.nextInt(1000);
|
||||
long testDelay = (long) Math.pow(2, this.attempts.intValue()) * 1000 + RANDOM.nextInt(1000);
|
||||
long delay = testDelay < this.maxDelay ? testDelay : maxDelay;
|
||||
Thread.sleep(delay);
|
||||
} catch (InterruptedException f) {
|
||||
|
@ -42,6 +42,7 @@ public abstract class Service {
|
||||
|
||||
protected final Database database;
|
||||
public ArrayList<Exception> exceptionsList;
|
||||
private static final Random RANDOM = new Random();
|
||||
private static final String ALL_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
|
||||
private static final Hashtable<String, Boolean> USED_IDS = new Hashtable<String, Boolean>();
|
||||
|
||||
@ -55,9 +56,8 @@ public abstract class Service {
|
||||
|
||||
protected String generateId() {
|
||||
StringBuilder random = new StringBuilder();
|
||||
Random rand = new Random();
|
||||
while (random.length() < 12) { // length of the random string.
|
||||
int index = (int) (rand.nextFloat() * ALL_CHARS.length());
|
||||
int index = (int) (RANDOM.nextFloat() * ALL_CHARS.length());
|
||||
random.append(ALL_CHARS.charAt(index));
|
||||
}
|
||||
String id = random.toString();
|
||||
|
@ -40,6 +40,7 @@ import java.util.Random;
|
||||
public class SampleData {
|
||||
|
||||
private static final List<PlayerDetails> PLAYERS;
|
||||
private static final Random RANDOM = new Random();
|
||||
|
||||
static {
|
||||
PLAYERS = new ArrayList<>();
|
||||
@ -83,10 +84,9 @@ public class SampleData {
|
||||
PLAYERS.add(new PlayerDetails("xavier@google.com", "143-947", "+375245"));
|
||||
PLAYERS.add(new PlayerDetails("harriet@google.com", "842-404", "+131243252"));
|
||||
InMemoryBank wireTransfers = new InMemoryBank();
|
||||
Random random = new Random();
|
||||
for (PlayerDetails player : PLAYERS) {
|
||||
wireTransfers.setFunds(player.getBankAccount(),
|
||||
random.nextInt(LotteryConstants.PLAYER_MAX_BALANCE));
|
||||
RANDOM.nextInt(LotteryConstants.PLAYER_MAX_BALANCE));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -58,6 +58,7 @@ public abstract class AbstractInstance implements Instance, Runnable {
|
||||
* The instance will execute the message in its message queue periodically once it is alive.
|
||||
*/
|
||||
@Override
|
||||
@SuppressWarnings("squid:S2189")
|
||||
public void run() {
|
||||
while (true) {
|
||||
if (!this.messageQueue.isEmpty()) {
|
||||
|
@ -34,6 +34,8 @@ import java.util.Random;
|
||||
public class ArrayUtilityMethods {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(ArrayUtilityMethods.class);
|
||||
|
||||
private static final Random RANDOM = new Random();
|
||||
/**
|
||||
* Method arraysSame compares 2 arrays @param a1 and @param a2
|
||||
* and @return whether their values are equal (boolean).
|
||||
@ -86,11 +88,10 @@ public class ArrayUtilityMethods {
|
||||
|
||||
public static int[][] createRandomIntMatrix(int rows, int columns) {
|
||||
int[][] matrix = new int[rows][columns];
|
||||
Random rand = new Random();
|
||||
for (int i = 0; i < rows; i++) {
|
||||
for (int j = 0; j < columns; j++) {
|
||||
//filling cells in matrix
|
||||
matrix[i][j] = rand.nextInt(10);
|
||||
matrix[i][j] = RANDOM.nextInt(10);
|
||||
}
|
||||
}
|
||||
return matrix;
|
||||
|
@ -41,6 +41,7 @@ public class Worker {
|
||||
/**
|
||||
* Keep checking queue for message
|
||||
*/
|
||||
@SuppressWarnings("squid:S2189")
|
||||
public void run() throws Exception {
|
||||
while (true) {
|
||||
Message message = queueManager.receiveMessage();
|
||||
|
@ -30,6 +30,8 @@ import java.util.Random;
|
||||
*/
|
||||
public class Producer {
|
||||
|
||||
private static final Random RANDOM = new Random();
|
||||
|
||||
private final ItemQueue queue;
|
||||
|
||||
private final String name;
|
||||
@ -48,7 +50,6 @@ public class Producer {
|
||||
|
||||
Item item = new Item(name, itemId++);
|
||||
queue.put(item);
|
||||
Random random = new Random();
|
||||
Thread.sleep(random.nextInt(2000));
|
||||
Thread.sleep(RANDOM.nextInt(2000));
|
||||
}
|
||||
}
|
||||
|
@ -37,6 +37,7 @@ import java.util.function.Predicate;
|
||||
* @param <T> the remote op's return type
|
||||
*/
|
||||
public final class RetryExponentialBackoff<T> implements BusinessOperation<T> {
|
||||
private static final Random RANDOM = new Random();
|
||||
private final BusinessOperation<T> op;
|
||||
private final int maxAttempts;
|
||||
private final long maxDelay;
|
||||
@ -98,8 +99,7 @@ public final class RetryExponentialBackoff<T> implements BusinessOperation<T> {
|
||||
}
|
||||
|
||||
try {
|
||||
Random rand = new Random();
|
||||
long testDelay = (long) Math.pow(2, this.attempts()) * 1000 + rand.nextInt(1000);
|
||||
long testDelay = (long) Math.pow(2, this.attempts()) * 1000 + RANDOM.nextInt(1000);
|
||||
long delay = testDelay < this.maxDelay ? testDelay : maxDelay;
|
||||
Thread.sleep(delay);
|
||||
} catch (InterruptedException f) {
|
||||
|
@ -36,6 +36,7 @@ import java.util.Random;
|
||||
|
||||
public class Bubble extends Point<Bubble> {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(Bubble.class);
|
||||
private static final Random RANDOM = new Random();
|
||||
|
||||
final int radius;
|
||||
|
||||
@ -45,10 +46,9 @@ public class Bubble extends Point<Bubble> {
|
||||
}
|
||||
|
||||
void move() {
|
||||
Random rand = new Random();
|
||||
//moves by 1 unit in either direction
|
||||
this.x += rand.nextInt(3) - 1;
|
||||
this.y += rand.nextInt(3) - 1;
|
||||
this.x += RANDOM.nextInt(3) - 1;
|
||||
this.y += RANDOM.nextInt(3) - 1;
|
||||
}
|
||||
|
||||
boolean touches(Bubble b) {
|
||||
|
@ -37,6 +37,7 @@ import com.iluwatar.typeobject.Candy.Type;
|
||||
*/
|
||||
|
||||
public class CellPool {
|
||||
private static final Random RANDOM = new Random();
|
||||
ArrayList<Cell> pool;
|
||||
int pointer;
|
||||
Candy[] randomCode;
|
||||
@ -57,8 +58,7 @@ public class CellPool {
|
||||
}
|
||||
for (int i = 0; i < num; i++) {
|
||||
Cell c = new Cell();
|
||||
Random rand = new Random();
|
||||
c.candy = randomCode[rand.nextInt(randomCode.length)];
|
||||
c.candy = randomCode[RANDOM.nextInt(randomCode.length)];
|
||||
this.pool.add(c);
|
||||
}
|
||||
this.pointer = num - 1;
|
||||
|
Reference in New Issue
Block a user