From 06b02b600269be4ab52a5d5294bf36ebb637bbbe Mon Sep 17 00:00:00 2001 From: Nathan Glenn Date: Tue, 30 Apr 2024 22:12:51 -0500 Subject: [PATCH] add toString(), use enum for clarity I want toString() to get a nice representation during debugging. Enums are much clearer than integers when debugging, as well. --- .../edu/umich/soar/debugger/doc/Document.java | 6 +-- .../debugger/doc/events/SoarAgentEvent.java | 42 ++++++++----------- .../doc/events/SoarChangeGenerator.java | 5 +-- .../debugger/modules/FoldingTextView.java | 6 +++ .../debugger/modules/UpdateCommandView.java | 9 ++++ 5 files changed, 38 insertions(+), 30 deletions(-) diff --git a/Java/Debugger/src/edu/umich/soar/debugger/doc/Document.java b/Java/Debugger/src/edu/umich/soar/debugger/doc/Document.java index 9f206d7560..c7d7fd6708 100644 --- a/Java/Debugger/src/edu/umich/soar/debugger/doc/Document.java +++ b/Java/Debugger/src/edu/umich/soar/debugger/doc/Document.java @@ -1128,13 +1128,13 @@ protected synchronized void fireSoarConnectionChanged() protected synchronized void fireAgentAdded(Agent agent) { m_SoarChangeGenerator.fireAgentListChanged(this, - SoarAgentEvent.kAgentAdded, agent); + SoarAgentEvent.EventType.AGENT_ADDED, agent); } protected synchronized void fireAgentRemoved(Agent agent) { m_SoarChangeGenerator.fireAgentListChanged(this, - SoarAgentEvent.kAgentRemoved, agent); + SoarAgentEvent.EventType.AGENT_REMOVED, agent); } // More generic than agent added/removed. Use the more specific form if you @@ -1142,7 +1142,7 @@ protected synchronized void fireAgentRemoved(Agent agent) protected synchronized void fireAgentListChanged() { m_SoarChangeGenerator.fireAgentListChanged(this, - SoarAgentEvent.kListChanged, null); + SoarAgentEvent.EventType.LIST_CHANGED, null); } public String sendAgentCommand(Agent agent, String commandLine) diff --git a/Java/Debugger/src/edu/umich/soar/debugger/doc/events/SoarAgentEvent.java b/Java/Debugger/src/edu/umich/soar/debugger/doc/events/SoarAgentEvent.java index deb32e5bfd..40f6cb6b03 100644 --- a/Java/Debugger/src/edu/umich/soar/debugger/doc/events/SoarAgentEvent.java +++ b/Java/Debugger/src/edu/umich/soar/debugger/doc/events/SoarAgentEvent.java @@ -18,48 +18,42 @@ * Used to report a change to an agent (added, removed etc.) * ************************************************************************/ -public class SoarAgentEvent extends java.util.EventObject -{ +public class SoarAgentEvent extends java.util.EventObject { private static final long serialVersionUID = -1073490020065002122L; - public static final int kAgentAdded = 1; - - public static final int kAgentRemoved = 2; - - public static final int kListChanged = 3; // More generic -- just says the list - // isn't the same now (use the more - // specific event if you can) + public enum EventType { + AGENT_ADDED, + AGENT_REMOVED, + // More generic -- just says the list + // isn't the same now (use the more + // specific event if you can) + LIST_CHANGED; + } - private final int m_Type; + private final EventType m_Type; private final Agent m_Agent; - public SoarAgentEvent(Object source, int type, Agent agent) - { + public SoarAgentEvent(Object source, EventType type, Agent agent) { super(source); m_Type = type; m_Agent = agent; } - public boolean isAgentAdded() - { - return m_Type == kAgentAdded; + public boolean isAgentAdded() { + return m_Type.equals(EventType.AGENT_ADDED); } - public boolean isAgentRemoved() - { - return m_Type == kAgentRemoved; + public boolean isAgentRemoved() { + return m_Type.equals(EventType.AGENT_REMOVED); } - public boolean isAgentListChanged() - { - return m_Type == kAgentAdded || m_Type == kAgentRemoved - || m_Type == kListChanged; + public boolean isAgentListChanged() { + return isAgentAdded() || isAgentRemoved() || m_Type.equals(EventType.LIST_CHANGED); } - public Agent getAgent() - { + public Agent getAgent() { return m_Agent; } } diff --git a/Java/Debugger/src/edu/umich/soar/debugger/doc/events/SoarChangeGenerator.java b/Java/Debugger/src/edu/umich/soar/debugger/doc/events/SoarChangeGenerator.java index 8a47c6d4e2..79235f6909 100644 --- a/Java/Debugger/src/edu/umich/soar/debugger/doc/events/SoarChangeGenerator.java +++ b/Java/Debugger/src/edu/umich/soar/debugger/doc/events/SoarChangeGenerator.java @@ -45,9 +45,8 @@ public synchronized void fireSoarConnectionChanged(Object source, } } - public synchronized void fireAgentListChanged(Object source, int type, - Agent agent) - { + public synchronized void fireAgentListChanged(Object source, SoarAgentEvent.EventType type, + Agent agent) { SoarAgentEvent event = new SoarAgentEvent(source, type, agent); for (SoarChangeListener current : m_Listeners) { diff --git a/Java/Debugger/src/edu/umich/soar/debugger/modules/FoldingTextView.java b/Java/Debugger/src/edu/umich/soar/debugger/modules/FoldingTextView.java index ec1f07dc24..f9b5d7ce98 100644 --- a/Java/Debugger/src/edu/umich/soar/debugger/modules/FoldingTextView.java +++ b/Java/Debugger/src/edu/umich/soar/debugger/modules/FoldingTextView.java @@ -1536,4 +1536,10 @@ public void loadFromXML(MainFrame frame, updateButtonState(); } } + + @Override + public String toString() { + Agent agent = getAgentFocus(); + return "FoldingTextView, agent=" + (agent == null ? "null" : agent.GetAgentName()); + } } diff --git a/Java/Debugger/src/edu/umich/soar/debugger/modules/UpdateCommandView.java b/Java/Debugger/src/edu/umich/soar/debugger/modules/UpdateCommandView.java index 2cba37aa2f..ec1b6cd833 100644 --- a/Java/Debugger/src/edu/umich/soar/debugger/modules/UpdateCommandView.java +++ b/Java/Debugger/src/edu/umich/soar/debugger/modules/UpdateCommandView.java @@ -11,6 +11,8 @@ ********************************************************************************************/ package edu.umich.soar.debugger.modules; +import sml.Agent; + /************************************************************************ * * This version of the BaseCommandView updates at the end of each run and clears @@ -38,4 +40,11 @@ public String getModuleBaseName() { return "update"; } + + @Override + public String toString() { + Agent agent = getAgentFocus(); + return "UpdateCommandView, command=" + (m_CurrentCommand == null ? "null" : m_CurrentCommand) + + ", agent=" + (agent == null ? "null" : agent.GetAgentName()); + } }