From 967b54f2797032d307154e586f7322420d79d2af Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Wed, 22 Feb 2017 17:46:27 +0100 Subject: [PATCH] [New] Added Activity.getActionsWithState(State) --- .../li/strolch/model/activity/Activity.java | 48 +++++++++++-------- .../test/java/li/strolch/model/ModelTest.java | 12 +++++ 2 files changed, 41 insertions(+), 19 deletions(-) diff --git a/li.strolch.model/src/main/java/li/strolch/model/activity/Activity.java b/li.strolch.model/src/main/java/li/strolch/model/activity/Activity.java index 600b02852..4cd25c5bd 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/activity/Activity.java +++ b/li.strolch.model/src/main/java/li/strolch/model/activity/Activity.java @@ -16,9 +16,11 @@ package li.strolch.model.activity; import java.text.MessageFormat; +import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.LinkedHashMap; +import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -39,8 +41,8 @@ import li.strolch.model.visitor.StrolchRootElementVisitor; import li.strolch.utils.dbc.DBC; /** - * Parameterized object grouping a collection of {@link Activity} and - * {@link Action} objects defining the process to be scheduled + * Parameterized object grouping a collection of {@link Activity} and {@link Action} objects defining the process to be + * scheduled * * @author Martin Smock */ @@ -118,35 +120,30 @@ public class Activity extends GroupedParameterizedElement } /** - * Returns true if this {@link Activity} contains any children i.e. any of - * {@link Action} or {@link Activity} + * Returns true if this {@link Activity} contains any children i.e. any of {@link Action} or {@link Activity} * - * @return true if this {@link Activity} contains any children i.e. any of - * {@link Action} or {@link Activity} + * @return true if this {@link Activity} contains any children i.e. any of {@link Action} or {@link Activity} */ public boolean hasElements() { return this.elements != null && !this.elements.isEmpty(); } /** - * Returns true if this {@link Activity} contains a child with the given id. - * The element instance type is ignored, i.e. {@link Action} or - * {@link Activity} + * Returns true if this {@link Activity} contains a child with the given id. The element instance type is ignored, + * i.e. {@link Action} or {@link Activity} * * @param id * the id of the element to check for * - * @return true if this {@link Activity} contains a child with the given id. - * The element instance type is ignored, i.e. {@link Action} or - * {@link Activity} + * @return true if this {@link Activity} contains a child with the given id. The element instance type is ignored, + * i.e. {@link Action} or {@link Activity} */ public boolean hasElement(String id) { return this.elements != null && this.elements.containsKey(id); } /** - * add an activity element to the LinkedHashMap of - * IActivityElements + * add an activity element to the LinkedHashMap of IActivityElements * * @param activityElement * @return the element added @@ -200,8 +197,7 @@ public class Activity extends GroupedParameterizedElement } /** - * @return get the LinkedHashMap of - * IActivityElements + * @return get the LinkedHashMap of IActivityElements */ public Map getElements() { if (this.elements == null) @@ -209,13 +205,27 @@ public class Activity extends GroupedParameterizedElement return this.elements; } + public List getActionsWithState(State state) { + List actions = new ArrayList<>(); + getActionsWithState(actions, state); + return actions; + } + + private void getActionsWithState(List actions, State state) { + for (IActivityElement element : this.elements.values()) { + if (element instanceof Activity) + ((Activity) element).getActionsWithState(actions, state); + else if (element.getState() == state) + actions.add((Action) element); + } + } + /** - * @return the iterator for entries, which include the id as key and the - * {@link IActivityElement} as value + * @return the iterator for entries, which include the id as key and the {@link IActivityElement} as value */ public Iterator> elementIterator() { if (this.elements == null) - return Collections.emptyMap().entrySet().iterator(); + return Collections. emptyMap().entrySet().iterator(); return this.elements.entrySet().iterator(); } diff --git a/li.strolch.model/src/test/java/li/strolch/model/ModelTest.java b/li.strolch.model/src/test/java/li/strolch/model/ModelTest.java index 09d494a19..0ad641f3a 100644 --- a/li.strolch.model/src/test/java/li/strolch/model/ModelTest.java +++ b/li.strolch.model/src/test/java/li/strolch/model/ModelTest.java @@ -159,6 +159,7 @@ public class ModelTest { validateBag(bag); Action action = activity.getElement("action_" + actId); + action.setState(State.ERROR); assertEquals("action_" + actId, action.getId()); assertEquals("Action " + actName, action.getName()); assertEquals("Use", action.getType()); @@ -174,6 +175,7 @@ public class ModelTest { validateBag(bag); action = activity.getElement("action_" + actId); + action.setState(State.ERROR); assertEquals("action_" + actId, action.getId()); assertEquals("Action " + actName, action.getName()); assertEquals("Use", action.getType()); @@ -189,12 +191,22 @@ public class ModelTest { validateBag(bag); action = activity.getElement("action1_" + actId); + action.setState(State.EXECUTION); assertEquals("action1_" + actId, action.getId()); assertEquals("Action " + actName, action.getName()); assertEquals("Use", action.getType()); assertEquals(ACTION_RES_ID, action.getResourceId()); assertEquals(ACTION_RES_TYPE, action.getResourceType()); assertEquals(changes, action.getChanges()); + + List actions = activity.getRootElement().getActionsWithState(State.ERROR); + assertEquals(2, actions.size()); + + actions = activity.getRootElement().getActionsWithState(State.EXECUTION); + assertEquals(1, actions.size()); + + actions = activity.getRootElement().getActionsWithState(State.CREATED); + assertEquals(1, actions.size()); } @Test