summaryrefslogtreecommitdiff
path: root/src/main/java/events/implementations/VSVectorClockMonitor.java
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2025-06-20 17:18:45 +0300
committerPaul Buetow <paul@buetow.org>2025-06-20 17:18:45 +0300
commit5e16f7f37c984d7ee1d1f0484cf0a8154bbb849d (patch)
treeb163049ab785dcfba3bc46cb159156e1c8566bf1 /src/main/java/events/implementations/VSVectorClockMonitor.java
parent28beef18a728ec4c35e47378c514ad826c2f9a31 (diff)
Improve code quality: Replace instanceof with polymorphism and extract constants
Major improvements: 1. Replace instanceof checks with polymorphic methods in VSAbstractEvent hierarchy - Added isInternalEvent(), isMessageReceiveEvent(), etc. methods - Added getEventPriority() for clean event ordering - Added shouldIncreaseTimestamps() to control timestamp behavior - Refactored VSTask to use these polymorphic methods 2. Extract magic numbers and strings to constants - Created VSConstants class for centralized configuration values - Added event priority constants (PRIORITY_HIGHEST, PRIORITY_HIGH, etc.) - Extracted string constants like CLASS_PREFIX - Moved magic numbers to named constants (PERCENTAGE_RANGE, etc.) 3. Update tests to work with new polymorphic approach - Fixed mocking in VSTaskTest to return correct values - All 132 tests passing These changes improve maintainability, reduce coupling, and make the codebase more self-documenting. The polymorphic approach eliminates type checking and makes it easier to add new event types. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
Diffstat (limited to 'src/main/java/events/implementations/VSVectorClockMonitor.java')
-rw-r--r--src/main/java/events/implementations/VSVectorClockMonitor.java136
1 files changed, 136 insertions, 0 deletions
diff --git a/src/main/java/events/implementations/VSVectorClockMonitor.java b/src/main/java/events/implementations/VSVectorClockMonitor.java
new file mode 100644
index 0000000..05a15f5
--- /dev/null
+++ b/src/main/java/events/implementations/VSVectorClockMonitor.java
@@ -0,0 +1,136 @@
+package events.implementations;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import core.VSInternalProcess;
+import core.time.VSVectorTime;
+
+/**
+ * A monitor that tracks vector clock changes and triggers events when
+ * vector timestamp conditions are met. This monitor is integrated into
+ * the process's vector clock update mechanism rather than being time-based.
+ *
+ * @author Paul C. Buetow
+ */
+public class VSVectorClockMonitor {
+
+ private List<VSTimestampTriggeredEvent> vectorEvents;
+ private VSInternalProcess process;
+ private VSVectorTime lastCheckedVector;
+
+ /**
+ * Constructor
+ */
+ public VSVectorClockMonitor(VSInternalProcess process) {
+ this.process = process;
+ this.vectorEvents = new ArrayList<>();
+ this.lastCheckedVector = null;
+ }
+
+ /**
+ * Add a vector timestamp event to monitor
+ */
+ public void addVectorEvent(VSTimestampTriggeredEvent event) {
+ if (event.getTimestampType() == VSTimestampTriggeredEvent.TimestampType.VECTOR &&
+ !vectorEvents.contains(event)) {
+ vectorEvents.add(event);
+ }
+ }
+
+ /**
+ * Remove a vector timestamp event from monitoring
+ */
+ public void removeVectorEvent(VSTimestampTriggeredEvent event) {
+ vectorEvents.remove(event);
+ }
+
+ /**
+ * Check all vector events when the vector clock changes.
+ * This should be called whenever the process's vector clock is updated.
+ */
+ public void checkVectorEvents() {
+ if (vectorEvents.isEmpty()) {
+ return;
+ }
+
+ VSVectorTime currentVector = process.getVectorTime();
+
+ // Only check if vector clock actually changed
+ if (currentVector == null || vectorClockEquals(currentVector, lastCheckedVector)) {
+ return;
+ }
+
+ List<VSTimestampTriggeredEvent> triggeredEvents = new ArrayList<>();
+
+ for (VSTimestampTriggeredEvent event : vectorEvents) {
+ if (!event.hasTriggered()) {
+ // Initialize event if needed
+ if (event.getProcess() == null) {
+ event.init(process);
+ }
+
+ // Check condition
+ if (event.checkCondition(process)) {
+ event.onStart(); // This will trigger the event
+ triggeredEvents.add(event);
+
+ process.log("Vector timestamp event triggered: " + event.toString());
+ }
+ }
+ }
+
+ // Remove triggered events
+ vectorEvents.removeAll(triggeredEvents);
+
+ // Update last checked vector
+ lastCheckedVector = currentVector != null ? currentVector.getCopy() : null;
+ }
+
+ /**
+ * Check if two vector clocks are equal
+ */
+ private boolean vectorClockEquals(VSVectorTime v1, VSVectorTime v2) {
+ if (v1 == null && v2 == null) {
+ return true;
+ }
+ if (v1 == null || v2 == null) {
+ return false;
+ }
+
+ int maxSize = Math.max(v1.size(), v2.size());
+
+ for (int i = 0; i < maxSize; i++) {
+ long val1 = i < v1.size() ? v1.get(i) : 0;
+ long val2 = i < v2.size() ? v2.get(i) : 0;
+
+ if (val1 != val2) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ /**
+ * Get the number of vector events being monitored
+ */
+ public int getVectorEventCount() {
+ return vectorEvents.size();
+ }
+
+ /**
+ * Clear all vector events
+ */
+ public void clearVectorEvents() {
+ vectorEvents.clear();
+ lastCheckedVector = null;
+ }
+
+ /**
+ * Get a copy of the monitored events list
+ */
+ public List<VSTimestampTriggeredEvent> getVectorEvents() {
+ return new ArrayList<>(vectorEvents);
+ }
+} \ No newline at end of file