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
|
* @param args command line args
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("squid:S2189")
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
//Create an object of monitoring service which makes both local and remote calls
|
//Create an object of monitoring service which makes both local and remote calls
|
||||||
var obj = new MonitoringService();
|
var obj = new MonitoringService();
|
||||||
|
@ -44,6 +44,7 @@ public class Order { //can store all transactions ids also
|
|||||||
public final String id;
|
public final String id;
|
||||||
final float price;
|
final float price;
|
||||||
final long createdTime;
|
final long createdTime;
|
||||||
|
private static final Random RANDOM = new Random();
|
||||||
private static final String ALL_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
|
private static final String ALL_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
|
||||||
private static final Hashtable<String, Boolean> USED_IDS = new Hashtable<String, Boolean>();
|
private static final Hashtable<String, Boolean> USED_IDS = new Hashtable<String, Boolean>();
|
||||||
PaymentStatus paid;
|
PaymentStatus paid;
|
||||||
@ -70,9 +71,8 @@ public class Order { //can store all transactions ids also
|
|||||||
|
|
||||||
String createUniqueId() {
|
String createUniqueId() {
|
||||||
StringBuilder random = new StringBuilder();
|
StringBuilder random = new StringBuilder();
|
||||||
Random rand = new Random();
|
|
||||||
while (random.length() < 12) { // length of the random string.
|
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));
|
random.append(ALL_CHARS.charAt(index));
|
||||||
}
|
}
|
||||||
return random.toString();
|
return random.toString();
|
||||||
|
@ -51,6 +51,8 @@ public class Retry<T> {
|
|||||||
public interface HandleErrorIssue<T> {
|
public interface HandleErrorIssue<T> {
|
||||||
void handleIssue(T obj, Exception e);
|
void handleIssue(T obj, Exception e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static final Random RANDOM = new Random();
|
||||||
|
|
||||||
private final Operation op;
|
private final Operation op;
|
||||||
private final HandleErrorIssue<T> handleError;
|
private final HandleErrorIssue<T> handleError;
|
||||||
@ -89,8 +91,7 @@ public class Retry<T> {
|
|||||||
return; //return here...dont go further
|
return; //return here...dont go further
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
Random rand = new Random();
|
long testDelay = (long) Math.pow(2, this.attempts.intValue()) * 1000 + RANDOM.nextInt(1000);
|
||||||
long testDelay = (long) Math.pow(2, this.attempts.intValue()) * 1000 + rand.nextInt(1000);
|
|
||||||
long delay = testDelay < this.maxDelay ? testDelay : maxDelay;
|
long delay = testDelay < this.maxDelay ? testDelay : maxDelay;
|
||||||
Thread.sleep(delay);
|
Thread.sleep(delay);
|
||||||
} catch (InterruptedException f) {
|
} catch (InterruptedException f) {
|
||||||
|
@ -42,6 +42,7 @@ public abstract class Service {
|
|||||||
|
|
||||||
protected final Database database;
|
protected final Database database;
|
||||||
public ArrayList<Exception> exceptionsList;
|
public ArrayList<Exception> exceptionsList;
|
||||||
|
private static final Random RANDOM = new Random();
|
||||||
private static final String ALL_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
|
private static final String ALL_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
|
||||||
private static final Hashtable<String, Boolean> USED_IDS = new Hashtable<String, Boolean>();
|
private static final Hashtable<String, Boolean> USED_IDS = new Hashtable<String, Boolean>();
|
||||||
|
|
||||||
@ -55,9 +56,8 @@ public abstract class Service {
|
|||||||
|
|
||||||
protected String generateId() {
|
protected String generateId() {
|
||||||
StringBuilder random = new StringBuilder();
|
StringBuilder random = new StringBuilder();
|
||||||
Random rand = new Random();
|
|
||||||
while (random.length() < 12) { // length of the random string.
|
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));
|
random.append(ALL_CHARS.charAt(index));
|
||||||
}
|
}
|
||||||
String id = random.toString();
|
String id = random.toString();
|
||||||
|
@ -40,6 +40,7 @@ import java.util.Random;
|
|||||||
public class SampleData {
|
public class SampleData {
|
||||||
|
|
||||||
private static final List<PlayerDetails> PLAYERS;
|
private static final List<PlayerDetails> PLAYERS;
|
||||||
|
private static final Random RANDOM = new Random();
|
||||||
|
|
||||||
static {
|
static {
|
||||||
PLAYERS = new ArrayList<>();
|
PLAYERS = new ArrayList<>();
|
||||||
@ -83,10 +84,9 @@ public class SampleData {
|
|||||||
PLAYERS.add(new PlayerDetails("xavier@google.com", "143-947", "+375245"));
|
PLAYERS.add(new PlayerDetails("xavier@google.com", "143-947", "+375245"));
|
||||||
PLAYERS.add(new PlayerDetails("harriet@google.com", "842-404", "+131243252"));
|
PLAYERS.add(new PlayerDetails("harriet@google.com", "842-404", "+131243252"));
|
||||||
InMemoryBank wireTransfers = new InMemoryBank();
|
InMemoryBank wireTransfers = new InMemoryBank();
|
||||||
Random random = new Random();
|
|
||||||
for (PlayerDetails player : PLAYERS) {
|
for (PlayerDetails player : PLAYERS) {
|
||||||
wireTransfers.setFunds(player.getBankAccount(),
|
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.
|
* The instance will execute the message in its message queue periodically once it is alive.
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
|
@SuppressWarnings("squid:S2189")
|
||||||
public void run() {
|
public void run() {
|
||||||
while (true) {
|
while (true) {
|
||||||
if (!this.messageQueue.isEmpty()) {
|
if (!this.messageQueue.isEmpty()) {
|
||||||
|
@ -34,6 +34,8 @@ import java.util.Random;
|
|||||||
public class ArrayUtilityMethods {
|
public class ArrayUtilityMethods {
|
||||||
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(ArrayUtilityMethods.class);
|
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
|
* Method arraysSame compares 2 arrays @param a1 and @param a2
|
||||||
* and @return whether their values are equal (boolean).
|
* and @return whether their values are equal (boolean).
|
||||||
@ -86,11 +88,10 @@ public class ArrayUtilityMethods {
|
|||||||
|
|
||||||
public static int[][] createRandomIntMatrix(int rows, int columns) {
|
public static int[][] createRandomIntMatrix(int rows, int columns) {
|
||||||
int[][] matrix = new int[rows][columns];
|
int[][] matrix = new int[rows][columns];
|
||||||
Random rand = new Random();
|
|
||||||
for (int i = 0; i < rows; i++) {
|
for (int i = 0; i < rows; i++) {
|
||||||
for (int j = 0; j < columns; j++) {
|
for (int j = 0; j < columns; j++) {
|
||||||
//filling cells in matrix
|
//filling cells in matrix
|
||||||
matrix[i][j] = rand.nextInt(10);
|
matrix[i][j] = RANDOM.nextInt(10);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return matrix;
|
return matrix;
|
||||||
|
@ -41,6 +41,7 @@ public class Worker {
|
|||||||
/**
|
/**
|
||||||
* Keep checking queue for message
|
* Keep checking queue for message
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("squid:S2189")
|
||||||
public void run() throws Exception {
|
public void run() throws Exception {
|
||||||
while (true) {
|
while (true) {
|
||||||
Message message = queueManager.receiveMessage();
|
Message message = queueManager.receiveMessage();
|
||||||
|
@ -29,6 +29,8 @@ import java.util.Random;
|
|||||||
* to queue
|
* to queue
|
||||||
*/
|
*/
|
||||||
public class Producer {
|
public class Producer {
|
||||||
|
|
||||||
|
private static final Random RANDOM = new Random();
|
||||||
|
|
||||||
private final ItemQueue queue;
|
private final ItemQueue queue;
|
||||||
|
|
||||||
@ -48,7 +50,6 @@ public class Producer {
|
|||||||
|
|
||||||
Item item = new Item(name, itemId++);
|
Item item = new Item(name, itemId++);
|
||||||
queue.put(item);
|
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
|
* @param <T> the remote op's return type
|
||||||
*/
|
*/
|
||||||
public final class RetryExponentialBackoff<T> implements BusinessOperation<T> {
|
public final class RetryExponentialBackoff<T> implements BusinessOperation<T> {
|
||||||
|
private static final Random RANDOM = new Random();
|
||||||
private final BusinessOperation<T> op;
|
private final BusinessOperation<T> op;
|
||||||
private final int maxAttempts;
|
private final int maxAttempts;
|
||||||
private final long maxDelay;
|
private final long maxDelay;
|
||||||
@ -98,8 +99,7 @@ public final class RetryExponentialBackoff<T> implements BusinessOperation<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Random rand = new Random();
|
long testDelay = (long) Math.pow(2, this.attempts()) * 1000 + RANDOM.nextInt(1000);
|
||||||
long testDelay = (long) Math.pow(2, this.attempts()) * 1000 + rand.nextInt(1000);
|
|
||||||
long delay = testDelay < this.maxDelay ? testDelay : maxDelay;
|
long delay = testDelay < this.maxDelay ? testDelay : maxDelay;
|
||||||
Thread.sleep(delay);
|
Thread.sleep(delay);
|
||||||
} catch (InterruptedException f) {
|
} catch (InterruptedException f) {
|
||||||
|
@ -36,6 +36,7 @@ import java.util.Random;
|
|||||||
|
|
||||||
public class Bubble extends Point<Bubble> {
|
public class Bubble extends Point<Bubble> {
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(Bubble.class);
|
private static final Logger LOGGER = LoggerFactory.getLogger(Bubble.class);
|
||||||
|
private static final Random RANDOM = new Random();
|
||||||
|
|
||||||
final int radius;
|
final int radius;
|
||||||
|
|
||||||
@ -45,10 +46,9 @@ public class Bubble extends Point<Bubble> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void move() {
|
void move() {
|
||||||
Random rand = new Random();
|
|
||||||
//moves by 1 unit in either direction
|
//moves by 1 unit in either direction
|
||||||
this.x += rand.nextInt(3) - 1;
|
this.x += RANDOM.nextInt(3) - 1;
|
||||||
this.y += rand.nextInt(3) - 1;
|
this.y += RANDOM.nextInt(3) - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean touches(Bubble b) {
|
boolean touches(Bubble b) {
|
||||||
|
@ -37,6 +37,7 @@ import com.iluwatar.typeobject.Candy.Type;
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
public class CellPool {
|
public class CellPool {
|
||||||
|
private static final Random RANDOM = new Random();
|
||||||
ArrayList<Cell> pool;
|
ArrayList<Cell> pool;
|
||||||
int pointer;
|
int pointer;
|
||||||
Candy[] randomCode;
|
Candy[] randomCode;
|
||||||
@ -57,8 +58,7 @@ public class CellPool {
|
|||||||
}
|
}
|
||||||
for (int i = 0; i < num; i++) {
|
for (int i = 0; i < num; i++) {
|
||||||
Cell c = new Cell();
|
Cell c = new Cell();
|
||||||
Random rand = new Random();
|
c.candy = randomCode[RANDOM.nextInt(randomCode.length)];
|
||||||
c.candy = randomCode[rand.nextInt(randomCode.length)];
|
|
||||||
this.pool.add(c);
|
this.pool.add(c);
|
||||||
}
|
}
|
||||||
this.pointer = num - 1;
|
this.pointer = num - 1;
|
||||||
|
Reference in New Issue
Block a user