summaryrefslogtreecommitdiff
path: root/src/main/java/core/VSSimulationConfig.java
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2025-06-20 20:04:02 +0300
committerPaul Buetow <paul@buetow.org>2025-06-20 20:04:02 +0300
commitf0b58321ae53f330da86c392661354b87bd9a412 (patch)
treec0be72f57094d13cf8bcb53567614258a6eb43fa /src/main/java/core/VSSimulationConfig.java
parentc3b95267b24d843897b04d6d6a16f62dc8cf1ed2 (diff)
Modernize codebase to use Java 21 features
- Convert VS3Tupel and VSLamportTime to records for immutability - Use switch expressions with pattern matching in VSTimestampTriggeredEvent - Modernize exception handling with pattern matching in VSErrorHandler - Replace anonymous ActionListener with lambda in VSAboutFrame - Use formatted strings instead of concatenation in VSDummyProtocol - Add sealed hierarchy VSEventType for exhaustive pattern matching - Create VSSimulationConfig record for configuration management - Maintain backward compatibility with deprecated methods All 132 unit tests pass successfully with Java 21 features. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
Diffstat (limited to 'src/main/java/core/VSSimulationConfig.java')
-rw-r--r--src/main/java/core/VSSimulationConfig.java106
1 files changed, 106 insertions, 0 deletions
diff --git a/src/main/java/core/VSSimulationConfig.java b/src/main/java/core/VSSimulationConfig.java
new file mode 100644
index 0000000..a3b94d9
--- /dev/null
+++ b/src/main/java/core/VSSimulationConfig.java
@@ -0,0 +1,106 @@
+package core;
+
+/**
+ * Configuration record for simulation parameters using Java 21 records.
+ * This immutable configuration object encapsulates all simulation settings.
+ *
+ * <p>Example usage:</p>
+ * <pre>{@code
+ * var config = VSSimulationConfig.defaultConfig();
+ * var customConfig = new VSSimulationConfig(
+ * 5, // processes
+ * 100, // timestep
+ * 10000, // duration
+ * true, // logging enabled
+ * 0.1f // clock variance
+ * );
+ * }</pre>
+ *
+ * @param numProcesses number of processes in the simulation (1-6)
+ * @param timestepMs simulation timestep in milliseconds
+ * @param maxDurationMs maximum simulation duration in milliseconds
+ * @param loggingEnabled whether logging is enabled
+ * @param defaultClockVariance default clock variance for processes
+ *
+ * @author Paul C. Buetow
+ */
+public record VSSimulationConfig(
+ int numProcesses,
+ long timestepMs,
+ long maxDurationMs,
+ boolean loggingEnabled,
+ float defaultClockVariance
+) {
+
+ /**
+ * Validates the configuration parameters.
+ * Called automatically by the record's canonical constructor.
+ */
+ public VSSimulationConfig {
+ if (numProcesses < constants.VSConstants.MIN_PROCESSES ||
+ numProcesses > constants.VSConstants.MAX_PROCESSES) {
+ throw new IllegalArgumentException(
+ "Number of processes must be between %d and %d"
+ .formatted(constants.VSConstants.MIN_PROCESSES,
+ constants.VSConstants.MAX_PROCESSES)
+ );
+ }
+ if (timestepMs <= 0) {
+ throw new IllegalArgumentException("Timestep must be positive");
+ }
+ if (maxDurationMs <= 0) {
+ throw new IllegalArgumentException("Max duration must be positive");
+ }
+ if (defaultClockVariance < -1.0f || defaultClockVariance > 1.0f) {
+ throw new IllegalArgumentException("Clock variance must be between -1.0 and 1.0");
+ }
+ }
+
+ /**
+ * Creates a default configuration suitable for most simulations.
+ *
+ * @return default simulation configuration
+ */
+ public static VSSimulationConfig defaultConfig() {
+ return new VSSimulationConfig(
+ constants.VSConstants.DEFAULT_PROCESSES,
+ 100, // 100ms timestep
+ 60000, // 60 second max duration
+ true, // logging enabled
+ 0.0f // no clock variance
+ );
+ }
+
+ /**
+ * Creates a new configuration with a different number of processes.
+ *
+ * @param newNumProcesses the new number of processes
+ * @return new configuration with updated process count
+ */
+ public VSSimulationConfig withProcesses(int newNumProcesses) {
+ return new VSSimulationConfig(
+ newNumProcesses,
+ timestepMs,
+ maxDurationMs,
+ loggingEnabled,
+ defaultClockVariance
+ );
+ }
+
+ /**
+ * Creates a new configuration with different timing parameters.
+ *
+ * @param newTimestep the new timestep in milliseconds
+ * @param newMaxDuration the new maximum duration in milliseconds
+ * @return new configuration with updated timing
+ */
+ public VSSimulationConfig withTiming(long newTimestep, long newMaxDuration) {
+ return new VSSimulationConfig(
+ numProcesses,
+ newTimestep,
+ newMaxDuration,
+ loggingEnabled,
+ defaultClockVariance
+ );
+ }
+} \ No newline at end of file