Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -35,6 +37,8 @@
*/
public final class FIFOCache<K, V> {

private static final Logger LOGGER = Logger.getLogger(FIFOCache.class.getName());

private final int capacity;
private final long defaultTTL;
private final Map<K, CacheEntry<V>> cache;
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -38,6 +40,8 @@
*/
public final class LIFOCache<K, V> {

private static final Logger LOGGER = Logger.getLogger(LIFOCache.class.getName());

private final int capacity;
private final long defaultTTL;
private final Map<K, CacheEntry<V>> cache;
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.security.SecureRandom;

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

private final List<Integer> list;
private int size;
private static final Random RAND = new Random();
private static final SecureRandom RAND = new SecureRandom();

static class ListNode {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import java.util.HashMap;

@SuppressWarnings({"rawtypes", "unchecked"})
final class LongestArithmeticSubsequence {
@SuppressWarnings({ "rawtypes", "unchecked" })
public final class LongestArithmeticSubsequence {
private LongestArithmeticSubsequence() {
}

Expand Down
11 changes: 9 additions & 2 deletions src/main/java/com/thealgorithms/scheduling/FCFSScheduling.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/thealgorithms/scheduling/RRScheduling.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ public class RRScheduling {
private int quantumTime;

RRScheduling(final List<ProcessDetails> 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;
}
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/com/thealgorithms/scheduling/SRTFScheduling.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,27 @@ public SRTFScheduling(ArrayList<ProcessDetails> 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();
}
Expand Down
47 changes: 29 additions & 18 deletions src/main/java/com/thealgorithms/stacks/StackPostfixNotation.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
/**
* Utility class for evaluating postfix expressions using integer arithmetic.
* <p>
* 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.
* </p>
* <p>
* For more information on postfix notation, refer to
* <a href="https://en.wikipedia.org/wiki/Reverse_Polish_notation">Reverse Polish Notation (RPN) on Wikipedia</a>.
* <a href="https://en.wikipedia.org/wiki/Reverse_Polish_notation">Reverse
* Polish Notation (RPN) on Wikipedia</a>.
* </p>
*/
public final class StackPostfixNotation {
Expand All @@ -22,16 +25,16 @@ private StackPostfixNotation() {
private static BiFunction<Integer, Integer, Integer> 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.");
}
}

Expand Down Expand Up @@ -64,9 +67,17 @@ private static void consumeExpression(Stack<Integer> s, final String exp) {
public static int postfixEvaluate(final String exp) {
Stack<Integer> 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(); }
} ̰
Loading