From 94d039f720d46a64b9daeee6dfba78e0cfff6824 Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Wed, 12 Jan 2022 16:38:50 +0100 Subject: [PATCH] [New] added Activity.streamActions(Predicate) and code cleanup --- .../li/strolch/model/activity/Activity.java | 130 +++++++----------- 1 file changed, 53 insertions(+), 77 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 698caede1..90c69a477 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 @@ -390,29 +390,6 @@ public class Activity extends AbstractStrolchRootElement return Optional.empty(); } - public T findElement(Predicate predicate, - Supplier msgSupplier) { - @SuppressWarnings("unchecked") - T t = (T) this.elements.values().stream().filter(predicate).collect(singletonCollector(msgSupplier)); - return t; - } - - public List findElements(Predicate predicate) { - return this.elements.values().stream().filter(predicate).collect(toList()); - } - - public List findActions(Predicate predicate) { - return this.elements.values().stream() // - .filter(IActivityElement::isAction) // - .map(e -> (Action) e) // - .filter(predicate) // - .collect(toList()); - } - - public List findActionsDeep(Predicate predicate) { - return streamActionsDeep().filter(predicate).collect(toList()); - } - public List getElementsByType(String type) { List elements = new ArrayList<>(); Iterator> iter = elementIterator(); @@ -433,10 +410,37 @@ public class Activity extends AbstractStrolchRootElement return this.elements; } + /** + * @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.emptyIterator(); + return this.elements.entrySet().iterator(); + } + + /** + * @return the stream for entries, which include the id as key and the {@link IActivityElement} as value + */ + public Stream> elementStream() { + if (this.elements == null) + return Stream.empty(); + return this.elements.entrySet().stream(); + } + public Stream streamElements() { + if (this.elements == null) + return Stream.empty(); return this.elements.values().stream(); } + public Stream streamActions(Predicate predicate) { + return streamElements() // + .filter(IActivityElement::isAction) // + .map(e -> (Action) e) // + .filter(predicate); + } + public Stream streamActionsDeep() { return streamElements().flatMap(e -> { if (e.isAction()) @@ -445,24 +449,36 @@ public class Activity extends AbstractStrolchRootElement }).map(IActivityElement::asAction); } + public Stream streamActionsDeep(Predicate predicate) { + return streamActionsDeep().filter(predicate); + } + + public T findElement(Predicate predicate, + Supplier msgSupplier) { + @SuppressWarnings("unchecked") + T t = (T) streamElements().filter(predicate).collect(singletonCollector(msgSupplier)); + return t; + } + + public List findElements(Predicate predicate) { + return streamElements().filter(predicate).collect(toList()); + } + + public List findActions(Predicate predicate) { + return streamActions(predicate).collect(toList()); + } + + public List findActionsDeep(Predicate predicate) { + return streamActionsDeep().filter(predicate).collect(toList()); + } + /** * Returns all the actions as a flat list * * @return the list of actions */ public List getActionsAsFlatList() { - List actions = new ArrayList<>(); - getActionsAsFlatList(actions); - return actions; - } - - private void getActionsAsFlatList(List actions) { - for (IActivityElement element : this.elements.values()) { - if (element instanceof Activity) - ((Activity) element).getActionsAsFlatList(actions); - else - actions.add((Action) element); - } + return streamActionsDeep().toList(); } /** @@ -474,18 +490,7 @@ public class Activity extends AbstractStrolchRootElement * @return the list of actions with the given state */ 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 streamActionsDeep(action -> action.getState() == state).toList(); } /** @@ -497,18 +502,7 @@ public class Activity extends AbstractStrolchRootElement * @return the list of actions with the given type */ public List getActionsByType(String type) { - List actions = new ArrayList<>(); - getActionsByType(actions, type); - return actions; - } - - private void getActionsByType(List actions, String type) { - for (IActivityElement element : this.elements.values()) { - if (element instanceof Activity) - ((Activity) element).getActionsByType(actions, type); - else if (element.getType().equals(type)) - actions.add((Action) element); - } + return streamActionsDeep(action -> action.getType().equals(type)).toList(); } public T getElementByLocator(Locator locator) { @@ -534,24 +528,6 @@ public class Activity extends AbstractStrolchRootElement return t; } - /** - * @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.emptyIterator(); - return this.elements.entrySet().iterator(); - } - - /** - * @return the stream for entries, which include the id as key and the {@link IActivityElement} as value - */ - public Stream> elementStream() { - if (this.elements == null) - return Stream.empty(); - return this.elements.entrySet().stream(); - } - @Override public Activity asActivity() { return this;