From 5e16f7f37c984d7ee1d1f0484cf0a8154bbb849d Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Fri, 20 Jun 2025 17:18:45 +0300 Subject: Improve code quality: Replace instanceof with polymorphism and extract constants MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../implementations/VSLamportTimestampEvent.java | 119 +++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 src/main/java/events/implementations/VSLamportTimestampEvent.java (limited to 'src/main/java/events/implementations/VSLamportTimestampEvent.java') diff --git a/src/main/java/events/implementations/VSLamportTimestampEvent.java b/src/main/java/events/implementations/VSLamportTimestampEvent.java new file mode 100644 index 0000000..272ea06 --- /dev/null +++ b/src/main/java/events/implementations/VSLamportTimestampEvent.java @@ -0,0 +1,119 @@ +package events.implementations; + +import core.VSInternalProcess; + +/** + * Concrete implementation of a Lamport timestamp-triggered event. + * This event fires when a specific Lamport timestamp condition is met. + * + * Example usage: + * - Fire when Lamport time equals 10 + * - Fire when Lamport time reaches 50 or greater + * - Fire when Lamport time is less than 5 + * + * @author Paul C. Buetow + */ +public class VSLamportTimestampEvent extends VSTimestampTriggeredEvent { + + private String actionDescription; + private Runnable customAction; + + /** + * Constructor for basic Lamport timestamp event + */ + public VSLamportTimestampEvent(long targetLamport, ComparisonOperator op) { + super(targetLamport, op); + this.actionDescription = "Lamport timestamp condition met"; + } + + /** + * Constructor with custom action description + */ + public VSLamportTimestampEvent(long targetLamport, ComparisonOperator op, String description) { + super(targetLamport, op); + this.actionDescription = description; + } + + /** + * Constructor with custom action + */ + public VSLamportTimestampEvent(long targetLamport, ComparisonOperator op, String description, Runnable action) { + super(targetLamport, op); + this.actionDescription = description; + this.customAction = action; + } + + /** + * Default constructor for serialization + */ + public VSLamportTimestampEvent() { + super(); + this.actionDescription = "Lamport timestamp event"; + } + + @Override + public void onInit() { + super.onInit(); + } + + @Override + protected void onTimestampReached() { + VSInternalProcess internalProcess = (VSInternalProcess) process; + + // Log the event + String message = String.format("Lamport timestamp event triggered: %s (current: %d, target: %d %s)", + actionDescription, + internalProcess.getLamportTime(), + targetLamportTime, + 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(); + } + } + + @Override + public String toString() { + return String.format(" [LamportTrigger: %d %s - %s]", + targetLamportTime, 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 "LamportTrigger"; + } +} \ No newline at end of file -- cgit v1.2.3