Skip to content

Commit 7c5d5f6

Browse files
oconnelciluwatar
authored andcommitted
1010: Fixed all of the blocking and critical Sonarcloud errors (iluwatar#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.
1 parent 8a48447 commit 7c5d5f6

File tree

12 files changed

+25
-19
lines changed

12 files changed

+25
-19
lines changed

circuit-breaker/src/main/java/com/iluwatar/circuitbreaker/App.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public class App {
6666
*
6767
* @param args command line args
6868
*/
69+
@SuppressWarnings("squid:S2189")
6970
public static void main(String[] args) {
7071
//Create an object of monitoring service which makes both local and remote calls
7172
var obj = new MonitoringService();

commander/src/main/java/com/iluwatar/commander/Order.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ enum MessageSent {
4444
public final String id;
4545
final float price;
4646
final long createdTime;
47+
private static final Random RANDOM = new Random();
4748
private static final String ALL_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
4849
private static final Hashtable<String, Boolean> USED_IDS = new Hashtable<String, Boolean>();
4950
PaymentStatus paid;
@@ -70,9 +71,8 @@ enum MessageSent {
7071

7172
String createUniqueId() {
7273
StringBuilder random = new StringBuilder();
73-
Random rand = new Random();
7474
while (random.length() < 12) { // length of the random string.
75-
int index = (int) (rand.nextFloat() * ALL_CHARS.length());
75+
int index = (int) (RANDOM.nextFloat() * ALL_CHARS.length());
7676
random.append(ALL_CHARS.charAt(index));
7777
}
7878
return random.toString();

commander/src/main/java/com/iluwatar/commander/Retry.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public interface Operation {
5151
public interface HandleErrorIssue<T> {
5252
void handleIssue(T obj, Exception e);
5353
}
54+
55+
private static final Random RANDOM = new Random();
5456

5557
private final Operation op;
5658
private final HandleErrorIssue<T> handleError;
@@ -89,8 +91,7 @@ public void perform(ArrayList<Exception> list, T obj) throws Exception {
8991
return; //return here...dont go further
9092
}
9193
try {
92-
Random rand = new Random();
93-
long testDelay = (long) Math.pow(2, this.attempts.intValue()) * 1000 + rand.nextInt(1000);
94+
long testDelay = (long) Math.pow(2, this.attempts.intValue()) * 1000 + RANDOM.nextInt(1000);
9495
long delay = testDelay < this.maxDelay ? testDelay : maxDelay;
9596
Thread.sleep(delay);
9697
} catch (InterruptedException f) {

commander/src/main/java/com/iluwatar/commander/Service.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public abstract class Service {
4242

4343
protected final Database database;
4444
public ArrayList<Exception> exceptionsList;
45+
private static final Random RANDOM = new Random();
4546
private static final String ALL_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
4647
private static final Hashtable<String, Boolean> USED_IDS = new Hashtable<String, Boolean>();
4748

@@ -55,9 +56,8 @@ protected Service(Database db, Exception...exc) {
5556

5657
protected String generateId() {
5758
StringBuilder random = new StringBuilder();
58-
Random rand = new Random();
5959
while (random.length() < 12) { // length of the random string.
60-
int index = (int) (rand.nextFloat() * ALL_CHARS.length());
60+
int index = (int) (RANDOM.nextFloat() * ALL_CHARS.length());
6161
random.append(ALL_CHARS.charAt(index));
6262
}
6363
String id = random.toString();

hexagonal/src/main/java/com/iluwatar/hexagonal/sampledata/SampleData.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
public class SampleData {
4141

4242
private static final List<PlayerDetails> PLAYERS;
43+
private static final Random RANDOM = new Random();
4344

4445
static {
4546
PLAYERS = new ArrayList<>();
@@ -83,10 +84,9 @@ public class SampleData {
8384
PLAYERS.add(new PlayerDetails("xavier@google.com", "143-947", "+375245"));
8485
PLAYERS.add(new PlayerDetails("harriet@google.com", "842-404", "+131243252"));
8586
InMemoryBank wireTransfers = new InMemoryBank();
86-
Random random = new Random();
8787
for (PlayerDetails player : PLAYERS) {
8888
wireTransfers.setFunds(player.getBankAccount(),
89-
random.nextInt(LotteryConstants.PLAYER_MAX_BALANCE));
89+
RANDOM.nextInt(LotteryConstants.PLAYER_MAX_BALANCE));
9090
}
9191
}
9292

leader-election/src/main/java/com/iluwatar/leaderelection/AbstractInstance.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public AbstractInstance(MessageManager messageManager, int localId, int leaderId
5858
* The instance will execute the message in its message queue periodically once it is alive.
5959
*/
6060
@Override
61+
@SuppressWarnings("squid:S2189")
6162
public void run() {
6263
while (true) {
6364
if (!this.messageQueue.isEmpty()) {

master-worker-pattern/src/main/java/com/iluwatar/masterworker/ArrayUtilityMethods.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
public class ArrayUtilityMethods {
3535

3636
private static final Logger LOGGER = LoggerFactory.getLogger(ArrayUtilityMethods.class);
37+
38+
private static final Random RANDOM = new Random();
3739
/**
3840
* Method arraysSame compares 2 arrays @param a1 and @param a2
3941
* and @return whether their values are equal (boolean).
@@ -86,11 +88,10 @@ public static boolean matricesSame(int[][] m1, int[][] m2) {
8688

8789
public static int[][] createRandomIntMatrix(int rows, int columns) {
8890
int[][] matrix = new int[rows][columns];
89-
Random rand = new Random();
9091
for (int i = 0; i < rows; i++) {
9192
for (int j = 0; j < columns; j++) {
9293
//filling cells in matrix
93-
matrix[i][j] = rand.nextInt(10);
94+
matrix[i][j] = RANDOM.nextInt(10);
9495
}
9596
}
9697
return matrix;

priority-queue/src/main/java/com/iluwatar/priority/queue/Worker.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public Worker(QueueManager queueManager) {
4141
/**
4242
* Keep checking queue for message
4343
*/
44+
@SuppressWarnings("squid:S2189")
4445
public void run() throws Exception {
4546
while (true) {
4647
Message message = queueManager.receiveMessage();

producer-consumer/src/main/java/com/iluwatar/producer/consumer/Producer.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
* to queue
3030
*/
3131
public class Producer {
32+
33+
private static final Random RANDOM = new Random();
3234

3335
private final ItemQueue queue;
3436

@@ -48,7 +50,6 @@ public void produce() throws InterruptedException {
4850

4951
Item item = new Item(name, itemId++);
5052
queue.put(item);
51-
Random random = new Random();
52-
Thread.sleep(random.nextInt(2000));
53+
Thread.sleep(RANDOM.nextInt(2000));
5354
}
5455
}

retry/src/main/java/com/iluwatar/retry/RetryExponentialBackoff.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
* @param <T> the remote op's return type
3838
*/
3939
public final class RetryExponentialBackoff<T> implements BusinessOperation<T> {
40+
private static final Random RANDOM = new Random();
4041
private final BusinessOperation<T> op;
4142
private final int maxAttempts;
4243
private final long maxDelay;
@@ -98,8 +99,7 @@ public T perform() throws BusinessException {
9899
}
99100

100101
try {
101-
Random rand = new Random();
102-
long testDelay = (long) Math.pow(2, this.attempts()) * 1000 + rand.nextInt(1000);
102+
long testDelay = (long) Math.pow(2, this.attempts()) * 1000 + RANDOM.nextInt(1000);
103103
long delay = testDelay < this.maxDelay ? testDelay : maxDelay;
104104
Thread.sleep(delay);
105105
} catch (InterruptedException f) {

spatial-partition/src/main/java/com/iluwatar/spatialpartition/Bubble.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
public class Bubble extends Point<Bubble> {
3838
private static final Logger LOGGER = LoggerFactory.getLogger(Bubble.class);
39+
private static final Random RANDOM = new Random();
3940

4041
final int radius;
4142

@@ -45,10 +46,9 @@ public class Bubble extends Point<Bubble> {
4546
}
4647

4748
void move() {
48-
Random rand = new Random();
4949
//moves by 1 unit in either direction
50-
this.x += rand.nextInt(3) - 1;
51-
this.y += rand.nextInt(3) - 1;
50+
this.x += RANDOM.nextInt(3) - 1;
51+
this.y += RANDOM.nextInt(3) - 1;
5252
}
5353

5454
boolean touches(Bubble b) {

typeobjectpattern/src/main/java/com/iluwatar/typeobject/CellPool.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
*/
3838

3939
public class CellPool {
40+
private static final Random RANDOM = new Random();
4041
ArrayList<Cell> pool;
4142
int pointer;
4243
Candy[] randomCode;
@@ -57,8 +58,7 @@ public class CellPool {
5758
}
5859
for (int i = 0; i < num; i++) {
5960
Cell c = new Cell();
60-
Random rand = new Random();
61-
c.candy = randomCode[rand.nextInt(randomCode.length)];
61+
c.candy = randomCode[RANDOM.nextInt(randomCode.length)];
6262
this.pool.add(c);
6363
}
6464
this.pointer = num - 1;

0 commit comments

Comments
 (0)