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:
Christopher O'Connell
2019-10-19 13:04:44 -04:00
committed by Ilkka Seppälä
parent 8a4844792f
commit 7c5d5f6b0d
12 changed files with 25 additions and 19 deletions

View File

@ -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();

View File

@ -51,6 +51,8 @@ public class Retry<T> {
public interface HandleErrorIssue<T> {
void handleIssue(T obj, Exception e);
}
private static final Random RANDOM = new Random();
private final Operation op;
private final HandleErrorIssue<T> handleError;
@ -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) {

View File

@ -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();