From 4192550e155ea62b0c2f5b8818315285db498fdb Mon Sep 17 00:00:00 2001 From: Abhi13shek Date: Thu, 25 Jun 2026 17:08:32 +0530 Subject: [PATCH 1/3] Style: Fix naming and formatting across multiple algorithm files --- .../datastructures/caches/FIFOCache.java | 8 ++++++-- .../datastructures/caches/LIFOCache.java | 8 ++++++-- .../datastructures/lists/RandomNode.java | 4 ++-- .../datastructures/lists/SkipList.java | 4 ++-- .../scheduling/FCFSScheduling.java | 11 +++++++++-- .../thealgorithms/scheduling/RRScheduling.java | 14 ++++++++++++++ .../scheduling/SRTFScheduling.java | 15 ++++++++++++++- .../stacks/StackPostfixNotation.java | 18 +++++++++++++----- 8 files changed, 66 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/caches/FIFOCache.java b/src/main/java/com/thealgorithms/datastructures/caches/FIFOCache.java index fa048434a187..b0f95fb5ab85 100644 --- a/src/main/java/com/thealgorithms/datastructures/caches/FIFOCache.java +++ b/src/main/java/com/thealgorithms/datastructures/caches/FIFOCache.java @@ -9,6 +9,8 @@ import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; import java.util.function.BiConsumer; +import java.util.logging.Level; +import java.util.logging.Logger; /** * A thread-safe generic cache implementation using the First-In-First-Out eviction policy. @@ -35,6 +37,8 @@ */ public final class FIFOCache { + private static final Logger LOGGER = Logger.getLogger(FIFOCache.class.getName()); + private final int capacity; private final long defaultTTL; private final Map> cache; @@ -255,8 +259,8 @@ private void notifyEviction(K key, V value) { if (evictionListener != null) { try { evictionListener.accept(key, value); - } catch (Exception e) { - System.err.println("Eviction listener failed: " + e.getMessage()); + } catch (IllegalArgumentException | IllegalStateException e) { + LOGGER.log(Level.WARNING, "Eviction listener failed for key: " + key, e); } } } diff --git a/src/main/java/com/thealgorithms/datastructures/caches/LIFOCache.java b/src/main/java/com/thealgorithms/datastructures/caches/LIFOCache.java index df3d4da912fe..4e75ef6a3f0d 100644 --- a/src/main/java/com/thealgorithms/datastructures/caches/LIFOCache.java +++ b/src/main/java/com/thealgorithms/datastructures/caches/LIFOCache.java @@ -12,6 +12,8 @@ import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; import java.util.function.BiConsumer; +import java.util.logging.Level; +import java.util.logging.Logger; /** * A thread-safe generic cache implementation using the Last-In-First-Out eviction policy. @@ -38,6 +40,8 @@ */ public final class LIFOCache { + private static final Logger LOGGER = Logger.getLogger(LIFOCache.class.getName()); + private final int capacity; private final long defaultTTL; private final Map> cache; @@ -268,8 +272,8 @@ private void notifyEviction(K key, V value) { if (evictionListener != null) { try { evictionListener.accept(key, value); - } catch (Exception e) { - System.err.println("Eviction listener failed: " + e.getMessage()); + } catch (IllegalArgumentException | IllegalStateException e) { + LOGGER.log(Level.WARNING, "Eviction listener failed for key: " + key, e); } } } diff --git a/src/main/java/com/thealgorithms/datastructures/lists/RandomNode.java b/src/main/java/com/thealgorithms/datastructures/lists/RandomNode.java index dac88dd9f241..22ab4bea908b 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/RandomNode.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/RandomNode.java @@ -2,7 +2,7 @@ import java.util.ArrayList; import java.util.List; -import java.util.Random; +import java.security.SecureRandom; /** * @author Suraj Kumar @@ -31,7 +31,7 @@ public class RandomNode { private final List list; private int size; - private static final Random RAND = new Random(); + private static final SecureRandom RAND = new SecureRandom(); static class ListNode { diff --git a/src/main/java/com/thealgorithms/datastructures/lists/SkipList.java b/src/main/java/com/thealgorithms/datastructures/lists/SkipList.java index 0b4fcd91483c..439f27f895ae 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/SkipList.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/SkipList.java @@ -5,7 +5,7 @@ import java.util.Collections; import java.util.List; import java.util.Objects; -import java.util.Random; +import java.security.SecureRandom; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -292,7 +292,7 @@ public static class BernoulliHeightStrategy implements HeightStrategy { private final double probability; private static final double DEFAULT_PROBABILITY = 0.5; - private static final Random RANDOM = new Random(); + private static final SecureRandom RANDOM = new SecureRandom(); public BernoulliHeightStrategy() { this.probability = DEFAULT_PROBABILITY; diff --git a/src/main/java/com/thealgorithms/scheduling/FCFSScheduling.java b/src/main/java/com/thealgorithms/scheduling/FCFSScheduling.java index b22e81fe560e..2ad8659c8f2b 100644 --- a/src/main/java/com/thealgorithms/scheduling/FCFSScheduling.java +++ b/src/main/java/com/thealgorithms/scheduling/FCFSScheduling.java @@ -27,15 +27,22 @@ private void evaluateWaitingTime() { return; } - int waitingTime = 0; int burstTime = processes.get(0).getBurstTime(); + if (burstTime < 0) { + throw new IllegalArgumentException("Burst time cannot be negative for process: " + processes.get(0).getProcessId()); + } + int waitingTime = 0; processes.get(0).setWaitingTime(waitingTime); // for the first process, waiting time will be 0. for (int i = 1; i < processesNumber; i++) { + int currentBurstTime = processes.get(i).getBurstTime(); + if (currentBurstTime < 0) { + throw new IllegalArgumentException("Burst time cannot be negative for process: " + processes.get(i).getProcessId()); + } processes.get(i).setWaitingTime(waitingTime + burstTime); waitingTime = processes.get(i).getWaitingTime(); - burstTime = processes.get(i).getBurstTime(); + burstTime = currentBurstTime; } } diff --git a/src/main/java/com/thealgorithms/scheduling/RRScheduling.java b/src/main/java/com/thealgorithms/scheduling/RRScheduling.java index 05efe1d59141..4ee607159e47 100644 --- a/src/main/java/com/thealgorithms/scheduling/RRScheduling.java +++ b/src/main/java/com/thealgorithms/scheduling/RRScheduling.java @@ -18,6 +18,20 @@ public class RRScheduling { private int quantumTime; RRScheduling(final List processes, int quantumTime) { + if (quantumTime <= 0) { + throw new IllegalArgumentException("Quantum time must be positive."); + } + for (ProcessDetails process : processes) { + if (process.getBurstTime() < 0) { + throw new IllegalArgumentException("Burst time cannot be negative for process: " + process.getProcessId()); + } + if (process.getArrivalTime() < 0) { + throw new IllegalArgumentException("Arrival time cannot be negative for process: " + process.getProcessId()); + } + if (process.getProcessId() == null || !process.getProcessId().matches("[a-zA-Z0-9_-]+")) { + throw new IllegalArgumentException("Invalid process ID: " + process.getProcessId()); + } + } this.processes = processes; this.quantumTime = quantumTime; } diff --git a/src/main/java/com/thealgorithms/scheduling/SRTFScheduling.java b/src/main/java/com/thealgorithms/scheduling/SRTFScheduling.java index 99214fff20c4..4efb26c6dbe2 100644 --- a/src/main/java/com/thealgorithms/scheduling/SRTFScheduling.java +++ b/src/main/java/com/thealgorithms/scheduling/SRTFScheduling.java @@ -30,14 +30,27 @@ public SRTFScheduling(ArrayList processes) { this.processes = processes; } + private static void validateProcess(ProcessDetails process) { + if (process.getBurstTime() < 0) { + throw new IllegalArgumentException("Burst time cannot be negative for process: " + process.getProcessId()); + } + if (process.getArrivalTime() < 0) { + throw new IllegalArgumentException("Arrival time cannot be negative for process: " + process.getProcessId()); + } + if (process.getProcessId() == null || !process.getProcessId().matches("[a-zA-Z0-9_-]+")) { + throw new IllegalArgumentException("Invalid process ID: " + process.getProcessId()); + } + } + public void evaluateScheduling() { int time = 0; int cr = 0; // cr=current running process, time= units of time int n = processes.size(); int[] remainingTime = new int[n]; - /* calculating remaining time of every process and total units of time */ + /* validating and calculating remaining time of every process and total units of time */ for (int i = 0; i < n; i++) { + validateProcess(processes.get(i)); remainingTime[i] = processes.get(i).getBurstTime(); time += processes.get(i).getBurstTime(); } diff --git a/src/main/java/com/thealgorithms/stacks/StackPostfixNotation.java b/src/main/java/com/thealgorithms/stacks/StackPostfixNotation.java index 690f39d36f5c..6f6c4e04ed9f 100644 --- a/src/main/java/com/thealgorithms/stacks/StackPostfixNotation.java +++ b/src/main/java/com/thealgorithms/stacks/StackPostfixNotation.java @@ -64,9 +64,17 @@ private static void consumeExpression(Stack s, final String exp) { public static int postfixEvaluate(final String exp) { Stack s = new Stack<>(); consumeExpression(s, exp); - if (s.size() != 1) { - throw new IllegalArgumentException("exp is not a proper postfix expression."); - } - return s.pop(); - } + // Inside the evaluation loop when an operator is found: +if (s.size() < 2) { + throw new IllegalArgumentException("Invalid expression: not enough operands."); +} +int b = s.pop(); +int a = s.pop(); +// perform operation and push result + +// ... after the loop finishes ... +if (s.size() != 1) { + throw new IllegalArgumentException("exp is not a proper postfix expression."); +} +return s.pop(); } } From 670f5e36c54b5f7fc2a87696519352080ef37648 Mon Sep 17 00:00:00 2001 From: Abhi13shek Date: Thu, 25 Jun 2026 17:18:47 +0530 Subject: [PATCH 2/3] Style: Auto-format Java files to fix linter issues --- .../LongestArithmeticSubsequence.java | 2 +- .../stacks/StackPostfixNotation.java | 31 ++++++++++--------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java b/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java index ba1def551192..d858fce3d1c5 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java @@ -2,7 +2,7 @@ import java.util.HashMap; -@SuppressWarnings({"rawtypes", "unchecked"}) +@SuppressWarnings({ "rawtypes", "unchecked" }) final class LongestArithmeticSubsequence { private LongestArithmeticSubsequence() { } diff --git a/src/main/java/com/thealgorithms/stacks/StackPostfixNotation.java b/src/main/java/com/thealgorithms/stacks/StackPostfixNotation.java index 6f6c4e04ed9f..bd7786d4739f 100644 --- a/src/main/java/com/thealgorithms/stacks/StackPostfixNotation.java +++ b/src/main/java/com/thealgorithms/stacks/StackPostfixNotation.java @@ -7,12 +7,15 @@ /** * Utility class for evaluating postfix expressions using integer arithmetic. *

- * Postfix notation, also known as Reverse Polish Notation (RPN), is a mathematical notation in which operators follow their operands. - * This class provides a method to evaluate expressions written in postfix notation. + * Postfix notation, also known as Reverse Polish Notation (RPN), is a + * mathematical notation in which operators follow their operands. + * This class provides a method to evaluate expressions written in postfix + * notation. *

*

* For more information on postfix notation, refer to - * Reverse Polish Notation (RPN) on Wikipedia. + * Reverse + * Polish Notation (RPN) on Wikipedia. *

*/ public final class StackPostfixNotation { @@ -22,16 +25,16 @@ private StackPostfixNotation() { private static BiFunction getOperator(final String operationSymbol) { // note the order of operands switch (operationSymbol) { - case "+": - return (a, b) -> b + a; - case "-": - return (a, b) -> b - a; - case "*": - return (a, b) -> b * a; - case "/": - return (a, b) -> b / a; - default: - throw new IllegalArgumentException("exp contains an unknown operation."); + case "+": + return (a, b) -> b + a; + case "-": + return (a, b) -> b - a; + case "*": + return (a, b) -> b * a; + case "/": + return (a, b) -> b / a; + default: + throw new IllegalArgumentException("exp contains an unknown operation."); } } @@ -77,4 +80,4 @@ public static int postfixEvaluate(final String exp) { throw new IllegalArgumentException("exp is not a proper postfix expression."); } return s.pop(); } -} +} ̰ \ No newline at end of file From 02006d9832346dab1eecdc7e6c9418642778f34a Mon Sep 17 00:00:00 2001 From: Abhi13shek Date: Thu, 25 Jun 2026 17:32:13 +0530 Subject: [PATCH 3/3] Fix: Change LongestArithmeticSubsequence class visibility to public --- .../dynamicprogramming/LongestArithmeticSubsequence.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java b/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java index d858fce3d1c5..95c92c62fff7 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java @@ -3,7 +3,7 @@ import java.util.HashMap; @SuppressWarnings({ "rawtypes", "unchecked" }) -final class LongestArithmeticSubsequence { +public final class LongestArithmeticSubsequence { private LongestArithmeticSubsequence() { }