Skip to content

Commit 4192550

Browse files
committed
Style: Fix naming and formatting across multiple algorithm files
1 parent 31caee6 commit 4192550

8 files changed

Lines changed: 66 additions & 16 deletions

File tree

src/main/java/com/thealgorithms/datastructures/caches/FIFOCache.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import java.util.concurrent.locks.Lock;
1010
import java.util.concurrent.locks.ReentrantLock;
1111
import java.util.function.BiConsumer;
12+
import java.util.logging.Level;
13+
import java.util.logging.Logger;
1214

1315
/**
1416
* A thread-safe generic cache implementation using the First-In-First-Out eviction policy.
@@ -35,6 +37,8 @@
3537
*/
3638
public final class FIFOCache<K, V> {
3739

40+
private static final Logger LOGGER = Logger.getLogger(FIFOCache.class.getName());
41+
3842
private final int capacity;
3943
private final long defaultTTL;
4044
private final Map<K, CacheEntry<V>> cache;
@@ -255,8 +259,8 @@ private void notifyEviction(K key, V value) {
255259
if (evictionListener != null) {
256260
try {
257261
evictionListener.accept(key, value);
258-
} catch (Exception e) {
259-
System.err.println("Eviction listener failed: " + e.getMessage());
262+
} catch (IllegalArgumentException | IllegalStateException e) {
263+
LOGGER.log(Level.WARNING, "Eviction listener failed for key: " + key, e);
260264
}
261265
}
262266
}

src/main/java/com/thealgorithms/datastructures/caches/LIFOCache.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import java.util.concurrent.locks.Lock;
1313
import java.util.concurrent.locks.ReentrantLock;
1414
import java.util.function.BiConsumer;
15+
import java.util.logging.Level;
16+
import java.util.logging.Logger;
1517

1618
/**
1719
* A thread-safe generic cache implementation using the Last-In-First-Out eviction policy.
@@ -38,6 +40,8 @@
3840
*/
3941
public final class LIFOCache<K, V> {
4042

43+
private static final Logger LOGGER = Logger.getLogger(LIFOCache.class.getName());
44+
4145
private final int capacity;
4246
private final long defaultTTL;
4347
private final Map<K, CacheEntry<V>> cache;
@@ -268,8 +272,8 @@ private void notifyEviction(K key, V value) {
268272
if (evictionListener != null) {
269273
try {
270274
evictionListener.accept(key, value);
271-
} catch (Exception e) {
272-
System.err.println("Eviction listener failed: " + e.getMessage());
275+
} catch (IllegalArgumentException | IllegalStateException e) {
276+
LOGGER.log(Level.WARNING, "Eviction listener failed for key: " + key, e);
273277
}
274278
}
275279
}

src/main/java/com/thealgorithms/datastructures/lists/RandomNode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import java.util.ArrayList;
44
import java.util.List;
5-
import java.util.Random;
5+
import java.security.SecureRandom;
66

77
/**
88
* @author <a href="https://github.com/skmodi649">Suraj Kumar</a>
@@ -31,7 +31,7 @@ public class RandomNode {
3131

3232
private final List<Integer> list;
3333
private int size;
34-
private static final Random RAND = new Random();
34+
private static final SecureRandom RAND = new SecureRandom();
3535

3636
static class ListNode {
3737

src/main/java/com/thealgorithms/datastructures/lists/SkipList.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import java.util.Collections;
66
import java.util.List;
77
import java.util.Objects;
8-
import java.util.Random;
8+
import java.security.SecureRandom;
99
import java.util.stream.Collectors;
1010
import java.util.stream.IntStream;
1111

@@ -292,7 +292,7 @@ public static class BernoulliHeightStrategy implements HeightStrategy {
292292
private final double probability;
293293

294294
private static final double DEFAULT_PROBABILITY = 0.5;
295-
private static final Random RANDOM = new Random();
295+
private static final SecureRandom RANDOM = new SecureRandom();
296296

297297
public BernoulliHeightStrategy() {
298298
this.probability = DEFAULT_PROBABILITY;

src/main/java/com/thealgorithms/scheduling/FCFSScheduling.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,22 @@ private void evaluateWaitingTime() {
2727
return;
2828
}
2929

30-
int waitingTime = 0;
3130
int burstTime = processes.get(0).getBurstTime();
31+
if (burstTime < 0) {
32+
throw new IllegalArgumentException("Burst time cannot be negative for process: " + processes.get(0).getProcessId());
33+
}
3234

35+
int waitingTime = 0;
3336
processes.get(0).setWaitingTime(waitingTime); // for the first process, waiting time will be 0.
3437

3538
for (int i = 1; i < processesNumber; i++) {
39+
int currentBurstTime = processes.get(i).getBurstTime();
40+
if (currentBurstTime < 0) {
41+
throw new IllegalArgumentException("Burst time cannot be negative for process: " + processes.get(i).getProcessId());
42+
}
3643
processes.get(i).setWaitingTime(waitingTime + burstTime);
3744
waitingTime = processes.get(i).getWaitingTime();
38-
burstTime = processes.get(i).getBurstTime();
45+
burstTime = currentBurstTime;
3946
}
4047
}
4148

src/main/java/com/thealgorithms/scheduling/RRScheduling.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@ public class RRScheduling {
1818
private int quantumTime;
1919

2020
RRScheduling(final List<ProcessDetails> processes, int quantumTime) {
21+
if (quantumTime <= 0) {
22+
throw new IllegalArgumentException("Quantum time must be positive.");
23+
}
24+
for (ProcessDetails process : processes) {
25+
if (process.getBurstTime() < 0) {
26+
throw new IllegalArgumentException("Burst time cannot be negative for process: " + process.getProcessId());
27+
}
28+
if (process.getArrivalTime() < 0) {
29+
throw new IllegalArgumentException("Arrival time cannot be negative for process: " + process.getProcessId());
30+
}
31+
if (process.getProcessId() == null || !process.getProcessId().matches("[a-zA-Z0-9_-]+")) {
32+
throw new IllegalArgumentException("Invalid process ID: " + process.getProcessId());
33+
}
34+
}
2135
this.processes = processes;
2236
this.quantumTime = quantumTime;
2337
}

src/main/java/com/thealgorithms/scheduling/SRTFScheduling.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,27 @@ public SRTFScheduling(ArrayList<ProcessDetails> processes) {
3030
this.processes = processes;
3131
}
3232

33+
private static void validateProcess(ProcessDetails process) {
34+
if (process.getBurstTime() < 0) {
35+
throw new IllegalArgumentException("Burst time cannot be negative for process: " + process.getProcessId());
36+
}
37+
if (process.getArrivalTime() < 0) {
38+
throw new IllegalArgumentException("Arrival time cannot be negative for process: " + process.getProcessId());
39+
}
40+
if (process.getProcessId() == null || !process.getProcessId().matches("[a-zA-Z0-9_-]+")) {
41+
throw new IllegalArgumentException("Invalid process ID: " + process.getProcessId());
42+
}
43+
}
44+
3345
public void evaluateScheduling() {
3446
int time = 0;
3547
int cr = 0; // cr=current running process, time= units of time
3648
int n = processes.size();
3749
int[] remainingTime = new int[n];
3850

39-
/* calculating remaining time of every process and total units of time */
51+
/* validating and calculating remaining time of every process and total units of time */
4052
for (int i = 0; i < n; i++) {
53+
validateProcess(processes.get(i));
4154
remainingTime[i] = processes.get(i).getBurstTime();
4255
time += processes.get(i).getBurstTime();
4356
}

src/main/java/com/thealgorithms/stacks/StackPostfixNotation.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,17 @@ private static void consumeExpression(Stack<Integer> s, final String exp) {
6464
public static int postfixEvaluate(final String exp) {
6565
Stack<Integer> s = new Stack<>();
6666
consumeExpression(s, exp);
67-
if (s.size() != 1) {
68-
throw new IllegalArgumentException("exp is not a proper postfix expression.");
69-
}
70-
return s.pop();
71-
}
67+
// Inside the evaluation loop when an operator is found:
68+
if (s.size() < 2) {
69+
throw new IllegalArgumentException("Invalid expression: not enough operands.");
70+
}
71+
int b = s.pop();
72+
int a = s.pop();
73+
// perform operation and push result
74+
75+
// ... after the loop finishes ...
76+
if (s.size() != 1) {
77+
throw new IllegalArgumentException("exp is not a proper postfix expression.");
78+
}
79+
return s.pop(); }
7280
}

0 commit comments

Comments
 (0)