summaryrefslogtreecommitdiff
path: root/src/main/java/events
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/events
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/events')
-rw-r--r--src/main/java/events/VSEventType.java71
-rw-r--r--src/main/java/events/implementations/VSTimestampTriggeredEvent.java42
2 files changed, 85 insertions, 28 deletions
diff --git a/src/main/java/events/VSEventType.java b/src/main/java/events/VSEventType.java
new file mode 100644
index 0000000..dc673ed
--- /dev/null
+++ b/src/main/java/events/VSEventType.java
@@ -0,0 +1,71 @@
+package events;
+
+import protocols.VSAbstractProtocol;
+import events.implementations.VSTimestampTriggeredEvent;
+
+/**
+ * Sealed hierarchy representing different types of events in the simulator.
+ * This uses Java 21's sealed classes feature to create a closed type hierarchy
+ * that can be exhaustively matched in switch expressions.
+ *
+ * <p>Example usage with pattern matching:</p>
+ * <pre>{@code
+ * VSEventType eventType = getEventType(event);
+ * String description = switch (eventType) {
+ * case InternalEvent e -> "Internal: " + e.description();
+ * case ProtocolEvent e -> "Protocol: " + e.protocolName();
+ * case TimestampEvent e -> "Timestamp: " + e.condition();
+ * case SystemEvent e -> "System: " + e.eventName();
+ * };
+ * }</pre>
+ *
+ * @since Java 21
+ */
+public sealed interface VSEventType
+ permits VSEventType.InternalEvent,
+ VSEventType.ProtocolEvent,
+ VSEventType.TimestampEvent,
+ VSEventType.SystemEvent {
+
+ /**
+ * Internal events that don't affect timestamps.
+ */
+ record InternalEvent(String description) implements VSEventType {}
+
+ /**
+ * Protocol-related events for distributed algorithms.
+ */
+ record ProtocolEvent(String protocolName, boolean isServer) implements VSEventType {}
+
+ /**
+ * Timestamp-triggered events based on logical time conditions.
+ */
+ record TimestampEvent(String condition, long targetTime) implements VSEventType {}
+
+ /**
+ * System events like process crashes and recoveries.
+ */
+ record SystemEvent(String eventName, int processId) implements VSEventType {}
+
+ /**
+ * Utility method to categorize an event.
+ *
+ * @param event the event to categorize
+ * @return the appropriate event type
+ */
+ static VSEventType categorize(VSAbstractEvent event) {
+ if (event.isInternalEvent()) {
+ return new InternalEvent(event.getShortname());
+ } else if (event instanceof VSAbstractProtocol protocol) {
+ return new ProtocolEvent(protocol.getShortname(), protocol.isServer());
+ } else if (event instanceof VSTimestampTriggeredEvent timestampEvent) {
+ return new TimestampEvent(
+ timestampEvent.getOperator().toString(),
+ timestampEvent.getTargetLamportTime()
+ );
+ } else {
+ return new SystemEvent(event.getShortname(),
+ event.getProcess() != null ? event.getProcess().getProcessNum() : -1);
+ }
+ }
+} \ No newline at end of file
diff --git a/src/main/java/events/implementations/VSTimestampTriggeredEvent.java b/src/main/java/events/implementations/VSTimestampTriggeredEvent.java
index c5e5386..278d2f2 100644
--- a/src/main/java/events/implementations/VSTimestampTriggeredEvent.java
+++ b/src/main/java/events/implementations/VSTimestampTriggeredEvent.java
@@ -130,20 +130,13 @@ public abstract class VSTimestampTriggeredEvent extends VSAbstractEvent implemen
protected boolean checkLamportCondition(VSInternalProcess process) {
long currentLamport = process.getLamportTime();
- switch (operator) {
- case EQUAL:
- return currentLamport == targetLamportTime;
- case GREATER_THAN:
- return currentLamport > targetLamportTime;
- case LESS_THAN:
- return currentLamport < targetLamportTime;
- case GREATER_EQUAL:
- return currentLamport >= targetLamportTime;
- case LESS_EQUAL:
- return currentLamport <= targetLamportTime;
- default:
- return false;
- }
+ return switch (operator) {
+ case EQUAL -> currentLamport == targetLamportTime;
+ case GREATER_THAN -> currentLamport > targetLamportTime;
+ case LESS_THAN -> currentLamport < targetLamportTime;
+ case GREATER_EQUAL -> currentLamport >= targetLamportTime;
+ case LESS_EQUAL -> currentLamport <= targetLamportTime;
+ };
}
/**
@@ -156,20 +149,13 @@ public abstract class VSTimestampTriggeredEvent extends VSAbstractEvent implemen
return false;
}
- switch (operator) {
- case EQUAL:
- return vectorTimesEqual(currentVector, targetVectorTime);
- case GREATER_THAN:
- return vectorTimeGreater(currentVector, targetVectorTime, false);
- case LESS_THAN:
- return vectorTimeGreater(targetVectorTime, currentVector, false);
- case GREATER_EQUAL:
- return vectorTimeGreater(currentVector, targetVectorTime, true);
- case LESS_EQUAL:
- return vectorTimeGreater(targetVectorTime, currentVector, true);
- default:
- return false;
- }
+ return switch (operator) {
+ case EQUAL -> vectorTimesEqual(currentVector, targetVectorTime);
+ case GREATER_THAN -> vectorTimeGreater(currentVector, targetVectorTime, false);
+ case LESS_THAN -> vectorTimeGreater(targetVectorTime, currentVector, false);
+ case GREATER_EQUAL -> vectorTimeGreater(currentVector, targetVectorTime, true);
+ case LESS_EQUAL -> vectorTimeGreater(targetVectorTime, currentVector, true);
+ };
}
/**