summaryrefslogtreecommitdiff
path: root/src/main/java/events/implementations/VSVectorTimestampEvent.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/VSVectorTimestampEvent.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/VSVectorTimestampEvent.java')
-rw-r--r--src/main/java/events/implementations/VSVectorTimestampEvent.java142
1 files changed, 142 insertions, 0 deletions
diff --git a/src/main/java/events/implementations/VSVectorTimestampEvent.java b/src/main/java/events/implementations/VSVectorTimestampEvent.java
new file mode 100644
index 0000000..4d3b327
--- /dev/null
+++ b/src/main/java/events/implementations/VSVectorTimestampEvent.java
@@ -0,0 +1,142 @@
+package events.implementations;
+
+import core.VSInternalProcess;
+import core.time.VSVectorTime;
+
+/**
+ * Concrete implementation of a Vector timestamp-triggered event.
+ * This event fires when a specific Vector timestamp condition is met.
+ *
+ * Example usage:
+ * - Fire when vector clock equals [3,2,1]
+ * - Fire when vector clock is greater than or equal to [5,0,2]
+ * - Fire when any element in vector clock reaches a threshold
+ *
+ * @author Paul C. Buetow
+ */
+public class VSVectorTimestampEvent extends VSTimestampTriggeredEvent {
+
+ private String actionDescription;
+ private Runnable customAction;
+
+ /**
+ * Constructor for basic Vector timestamp event
+ */
+ public VSVectorTimestampEvent(VSVectorTime targetVector, ComparisonOperator op) {
+ super(targetVector, op);
+ this.actionDescription = "Vector timestamp condition met";
+ }
+
+ /**
+ * Constructor with custom action description
+ */
+ public VSVectorTimestampEvent(VSVectorTime targetVector, ComparisonOperator op, String description) {
+ super(targetVector, op);
+ this.actionDescription = description;
+ }
+
+ /**
+ * Constructor with custom action
+ */
+ public VSVectorTimestampEvent(VSVectorTime targetVector, ComparisonOperator op, String description, Runnable action) {
+ super(targetVector, op);
+ this.actionDescription = description;
+ this.customAction = action;
+ }
+
+ /**
+ * Default constructor for serialization
+ */
+ public VSVectorTimestampEvent() {
+ super();
+ this.actionDescription = "Vector timestamp event";
+ }
+
+ @Override
+ public void onInit() {
+ super.onInit();
+ }
+
+ @Override
+ protected void onTimestampReached() {
+ VSInternalProcess internalProcess = (VSInternalProcess) process;
+
+ // Log the event
+ String message = String.format("Vector timestamp event triggered: %s (current: %s, target: %s %s)",
+ actionDescription,
+ internalProcess.getVectorTime(),
+ targetVectorTime,
+ operator);
+
+ internalProcess.log(message);
+
+ // Execute custom action if provided
+ if (customAction != null) {
+ try {
+ customAction.run();
+ } catch (Exception e) {
+ internalProcess.log("Error executing custom action: " + e.getMessage());
+ }
+ }
+
+ // Default behavior: change process color to indicate trigger
+ changeProcessColor();
+ }
+
+ /**
+ * Change process color to indicate the timestamp event has been triggered
+ */
+ protected void changeProcessColor() {
+ if (process instanceof VSInternalProcess) {
+ VSInternalProcess internalProcess = (VSInternalProcess) process;
+ // Change to highlight color temporarily
+ internalProcess.highlightOn();
+ }
+ }
+
+ /**
+ * Convenience method to create event that triggers when any vector element reaches threshold
+ */
+ public static VSVectorTimestampEvent createThresholdEvent(int processCount, long threshold, String description) {
+ VSVectorTime targetVector = new VSVectorTime(0);
+ for (int i = 0; i < processCount; i++) {
+ targetVector.add(threshold);
+ }
+ return new VSVectorTimestampEvent(targetVector, ComparisonOperator.GREATER_EQUAL, description);
+ }
+
+ /**
+ * Convenience method to create event that triggers when specific process element reaches value
+ */
+ public static VSVectorTimestampEvent createProcessThresholdEvent(int processCount, int processIndex, long threshold, String description) {
+ VSVectorTime targetVector = new VSVectorTime(0);
+ for (int i = 0; i < processCount; i++) {
+ targetVector.add(i == processIndex ? threshold : 0L);
+ }
+ return new VSVectorTimestampEvent(targetVector, ComparisonOperator.GREATER_EQUAL, description);
+ }
+
+ @Override
+ public String toString() {
+ return String.format(" [VectorTrigger: %s %s - %s]",
+ targetVectorTime, operator, actionDescription);
+ }
+
+ // Getters and setters
+ public String getActionDescription() {
+ return actionDescription;
+ }
+
+ public void setActionDescription(String description) {
+ this.actionDescription = description;
+ }
+
+ public void setCustomAction(Runnable action) {
+ this.customAction = action;
+ }
+
+ @Override
+ protected String createShortname(String savedShortname) {
+ return "VectorTrigger";
+ }
+} \ No newline at end of file