[New] added Activity.streamActions(Predicate) and code cleanup
This commit is contained in:
parent
3becdd2d8e
commit
94d039f720
|
@ -390,29 +390,6 @@ public class Activity extends AbstractStrolchRootElement
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T extends IActivityElement> T findElement(Predicate<IActivityElement> predicate,
|
|
||||||
Supplier<String> msgSupplier) {
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
T t = (T) this.elements.values().stream().filter(predicate).collect(singletonCollector(msgSupplier));
|
|
||||||
return t;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<IActivityElement> findElements(Predicate<IActivityElement> predicate) {
|
|
||||||
return this.elements.values().stream().filter(predicate).collect(toList());
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Action> findActions(Predicate<Action> predicate) {
|
|
||||||
return this.elements.values().stream() //
|
|
||||||
.filter(IActivityElement::isAction) //
|
|
||||||
.map(e -> (Action) e) //
|
|
||||||
.filter(predicate) //
|
|
||||||
.collect(toList());
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Action> findActionsDeep(Predicate<Action> predicate) {
|
|
||||||
return streamActionsDeep().filter(predicate).collect(toList());
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<IActivityElement> getElementsByType(String type) {
|
public List<IActivityElement> getElementsByType(String type) {
|
||||||
List<IActivityElement> elements = new ArrayList<>();
|
List<IActivityElement> elements = new ArrayList<>();
|
||||||
Iterator<Entry<String, IActivityElement>> iter = elementIterator();
|
Iterator<Entry<String, IActivityElement>> iter = elementIterator();
|
||||||
|
@ -433,10 +410,37 @@ public class Activity extends AbstractStrolchRootElement
|
||||||
return this.elements;
|
return this.elements;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the iterator for entries, which include the id as key and the {@link IActivityElement} as value
|
||||||
|
*/
|
||||||
|
public Iterator<Entry<String, IActivityElement>> 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<Entry<String, IActivityElement>> elementStream() {
|
||||||
|
if (this.elements == null)
|
||||||
|
return Stream.empty();
|
||||||
|
return this.elements.entrySet().stream();
|
||||||
|
}
|
||||||
|
|
||||||
public Stream<IActivityElement> streamElements() {
|
public Stream<IActivityElement> streamElements() {
|
||||||
|
if (this.elements == null)
|
||||||
|
return Stream.empty();
|
||||||
return this.elements.values().stream();
|
return this.elements.values().stream();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Stream<Action> streamActions(Predicate<Action> predicate) {
|
||||||
|
return streamElements() //
|
||||||
|
.filter(IActivityElement::isAction) //
|
||||||
|
.map(e -> (Action) e) //
|
||||||
|
.filter(predicate);
|
||||||
|
}
|
||||||
|
|
||||||
public Stream<Action> streamActionsDeep() {
|
public Stream<Action> streamActionsDeep() {
|
||||||
return streamElements().flatMap(e -> {
|
return streamElements().flatMap(e -> {
|
||||||
if (e.isAction())
|
if (e.isAction())
|
||||||
|
@ -445,24 +449,36 @@ public class Activity extends AbstractStrolchRootElement
|
||||||
}).map(IActivityElement::asAction);
|
}).map(IActivityElement::asAction);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Stream<Action> streamActionsDeep(Predicate<Action> predicate) {
|
||||||
|
return streamActionsDeep().filter(predicate);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T extends IActivityElement> T findElement(Predicate<IActivityElement> predicate,
|
||||||
|
Supplier<String> msgSupplier) {
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
T t = (T) streamElements().filter(predicate).collect(singletonCollector(msgSupplier));
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<IActivityElement> findElements(Predicate<IActivityElement> predicate) {
|
||||||
|
return streamElements().filter(predicate).collect(toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Action> findActions(Predicate<Action> predicate) {
|
||||||
|
return streamActions(predicate).collect(toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Action> findActionsDeep(Predicate<Action> predicate) {
|
||||||
|
return streamActionsDeep().filter(predicate).collect(toList());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns all the actions as a flat list
|
* Returns all the actions as a flat list
|
||||||
*
|
*
|
||||||
* @return the list of actions
|
* @return the list of actions
|
||||||
*/
|
*/
|
||||||
public List<Action> getActionsAsFlatList() {
|
public List<Action> getActionsAsFlatList() {
|
||||||
List<Action> actions = new ArrayList<>();
|
return streamActionsDeep().toList();
|
||||||
getActionsAsFlatList(actions);
|
|
||||||
return actions;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void getActionsAsFlatList(List<Action> actions) {
|
|
||||||
for (IActivityElement element : this.elements.values()) {
|
|
||||||
if (element instanceof Activity)
|
|
||||||
((Activity) element).getActionsAsFlatList(actions);
|
|
||||||
else
|
|
||||||
actions.add((Action) element);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -474,18 +490,7 @@ public class Activity extends AbstractStrolchRootElement
|
||||||
* @return the list of actions with the given state
|
* @return the list of actions with the given state
|
||||||
*/
|
*/
|
||||||
public List<Action> getActionsWithState(State state) {
|
public List<Action> getActionsWithState(State state) {
|
||||||
List<Action> actions = new ArrayList<>();
|
return streamActionsDeep(action -> action.getState() == state).toList();
|
||||||
getActionsWithState(actions, state);
|
|
||||||
return actions;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void getActionsWithState(List<Action> 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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -497,18 +502,7 @@ public class Activity extends AbstractStrolchRootElement
|
||||||
* @return the list of actions with the given type
|
* @return the list of actions with the given type
|
||||||
*/
|
*/
|
||||||
public List<Action> getActionsByType(String type) {
|
public List<Action> getActionsByType(String type) {
|
||||||
List<Action> actions = new ArrayList<>();
|
return streamActionsDeep(action -> action.getType().equals(type)).toList();
|
||||||
getActionsByType(actions, type);
|
|
||||||
return actions;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void getActionsByType(List<Action> 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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T extends IActivityElement> T getElementByLocator(Locator locator) {
|
public <T extends IActivityElement> T getElementByLocator(Locator locator) {
|
||||||
|
@ -534,24 +528,6 @@ public class Activity extends AbstractStrolchRootElement
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @return the iterator for entries, which include the id as key and the {@link IActivityElement} as value
|
|
||||||
*/
|
|
||||||
public Iterator<Entry<String, IActivityElement>> 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<Entry<String, IActivityElement>> elementStream() {
|
|
||||||
if (this.elements == null)
|
|
||||||
return Stream.empty();
|
|
||||||
return this.elements.entrySet().stream();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Activity asActivity() {
|
public Activity asActivity() {
|
||||||
return this;
|
return this;
|
||||||
|
|
Loading…
Reference in New Issue