summaryrefslogtreecommitdiff
path: root/src/main/java/events/VSAbstractEvent.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/VSAbstractEvent.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/VSAbstractEvent.java')
-rw-r--r--src/main/java/events/VSAbstractEvent.java98
1 files changed, 95 insertions, 3 deletions
diff --git a/src/main/java/events/VSAbstractEvent.java b/src/main/java/events/VSAbstractEvent.java
index d11ccbd..37c3d59 100644
--- a/src/main/java/events/VSAbstractEvent.java
+++ b/src/main/java/events/VSAbstractEvent.java
@@ -19,6 +19,16 @@ import serialize.VSSerialize;
* @author Paul C. Buetow
*/
abstract public class VSAbstractEvent extends VSSerializablePrefs {
+ /** Event priority constants for task ordering */
+ public static final int PRIORITY_HIGHEST = -3; // Process recover events
+ public static final int PRIORITY_HIGH = -2; // Process crash events
+ public static final int PRIORITY_MEDIUM = -1; // Protocol events
+ public static final int PRIORITY_NORMAL = 0; // All other events
+
+ /** Class name prefix used by Java reflection */
+ private static final String CLASS_PREFIX = "class ";
+ private static final int CLASS_PREFIX_LENGTH = 6;
+
/** The prefs. */
public VSPrefs prefs;
@@ -32,6 +42,88 @@ abstract public class VSAbstractEvent extends VSSerializablePrefs {
private String eventClassname;
/**
+ * Check if this event is an internal event.
+ *
+ * @return true if this is an internal event
+ */
+ public boolean isInternalEvent() {
+ return false;
+ }
+
+ /**
+ * Check if this event is serializable.
+ *
+ * @return true if this event is serializable
+ */
+ public boolean isSerializable() {
+ return true;
+ }
+
+ /**
+ * Check if this event is a message receive event.
+ *
+ * @return true if this is a message receive event
+ */
+ public boolean isMessageReceiveEvent() {
+ return false;
+ }
+
+ /**
+ * Check if this event is a process recover event.
+ *
+ * @return true if this is a process recover event
+ */
+ public boolean isProcessRecoverEvent() {
+ return false;
+ }
+
+ /**
+ * Check if this event is a process crash event.
+ *
+ * @return true if this is a process crash event
+ */
+ public boolean isProcessCrashEvent() {
+ return false;
+ }
+
+ /**
+ * Check if this event is a protocol event.
+ *
+ * @return true if this is a protocol event
+ */
+ public boolean isProtocolEvent() {
+ return false;
+ }
+
+ /**
+ * Check if this event should trigger timestamp increases when executed.
+ *
+ * @return true if timestamps should be increased
+ */
+ public boolean shouldIncreaseTimestamps() {
+ return true;
+ }
+
+ /**
+ * Get the priority of this event for ordering in VSTask comparisons.
+ * Lower values have higher priority.
+ *
+ * @return the event priority
+ */
+ public int getEventPriority() {
+ return PRIORITY_NORMAL;
+ }
+
+ /**
+ * Check if this event is copyable.
+ *
+ * @return true if this event can be copied
+ */
+ public boolean isCopyable() {
+ return this instanceof VSCopyableEvent;
+ }
+
+ /**
* Creates a copy of the event and using a new process.
*
* @param theProcess The new process
@@ -43,7 +135,7 @@ abstract public class VSAbstractEvent extends VSSerializablePrefs {
if (theProcess == null)
theProcess = (VSInternalProcess) process;
- if (!(this instanceof VSCopyableEvent))
+ if (!isCopyable())
throw new VSEventNotCopyableException(
eventShortname + " (" + eventClassname + ")");
@@ -93,8 +185,8 @@ abstract public class VSAbstractEvent extends VSSerializablePrefs {
* @param eventClassname the new classname
*/
public final void setClassname(String eventClassname) {
- if (eventClassname.startsWith("class "))
- eventClassname = eventClassname.substring(6);
+ if (eventClassname.startsWith(CLASS_PREFIX))
+ eventClassname = eventClassname.substring(CLASS_PREFIX_LENGTH);
this.eventClassname = eventClassname;
}