diff --git a/li.strolch.model/src/main/java/li/strolch/model/GroupedParameterizedElement.java b/li.strolch.model/src/main/java/li/strolch/model/GroupedParameterizedElement.java index d2bc95937..c20319c46 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/GroupedParameterizedElement.java +++ b/li.strolch.model/src/main/java/li/strolch/model/GroupedParameterizedElement.java @@ -15,9 +15,11 @@ */ package li.strolch.model; +import static java.util.stream.Collectors.toList; + import java.text.MessageFormat; import java.util.*; -import java.util.stream.Collectors; +import java.util.stream.Stream; import li.strolch.exception.StrolchException; import li.strolch.exception.StrolchModelException; @@ -139,6 +141,64 @@ public abstract class GroupedParameterizedElement extends AbstractStrolchElement return parameter; } + /** + * Returns a {@link Stream} of all the {@link Parameter Parameters} + * + * @param bagKey + * the key of the {@link ParameterBag} from which the {@link Parameter} is to be returned + * + * @return the parameters with the given interpretation + */ + @Override + public Stream> streamOfParameters(String bagKey) { + ParameterBag bag = getParameterBag(bagKey); + if (bag == null) + return Stream.empty(); + + return bag.streamOfParameters(); + } + + /** + * Returns a {@link Stream} of all the {@link Parameter Parameters} with the given interpretation + * + * @param bagKey + * the key of the {@link ParameterBag} from which the {@link Parameter} is to be returned + * @param interpretation + * the interpretation for which the parameters are to be returned + * + * @return the parameters with the given interpretation + */ + @Override + public Stream> streamOfParametersByInterpretation(String bagKey, String interpretation) { + ParameterBag bag = getParameterBag(bagKey); + if (bag == null) + return Stream.empty(); + + return bag.streamOfParametersByInterpretation(interpretation); + } + + /** + * Returns a {@link Stream} of all the {@link Parameter Parameters} with the given interpretation + * + * @param bagKey + * the key of the {@link ParameterBag} from which the {@link Parameter} is to be returned + * @param interpretation + * the interpretation for which the parameters are to be returned + * @param uom + * the uom for which the parameters are to be returned + * + * @return the parameters with the given interpretation + */ + @Override + public Stream> streamOfParametersByInterpretationAndUom(String bagKey, String interpretation, + String uom) { + ParameterBag bag = getParameterBag(bagKey); + if (bag == null) + return Stream.empty(); + + return bag.streamOfParametersByInterpretationAndUom(interpretation, uom); + } + /** * Returns a list of all the {@link Parameter Parameters} with the given interpretation * @@ -151,11 +211,7 @@ public abstract class GroupedParameterizedElement extends AbstractStrolchElement */ @Override public List> getParametersByInterpretation(String bagKey, String interpretation) { - ParameterBag bag = getParameterBag(bagKey); - if (bag == null) - return Collections.emptyList(); - - return bag.getParametersByInterpretation(interpretation); + return streamOfParametersByInterpretation(bagKey, interpretation).collect(toList()); } /** @@ -172,11 +228,7 @@ public abstract class GroupedParameterizedElement extends AbstractStrolchElement */ @Override public List> getParametersByInterpretationAndUom(String bagKey, String interpretation, String uom) { - ParameterBag bag = getParameterBag(bagKey); - if (bag == null) - return Collections.emptyList(); - - return bag.getParametersByInterpretationAndUom(interpretation, uom); + return streamOfParametersByInterpretationAndUom(bagKey, interpretation, uom).collect(toList()); } /** @@ -193,9 +245,8 @@ public abstract class GroupedParameterizedElement extends AbstractStrolchElement @Override public void addParameter(String bagKey, Parameter parameter) throws StrolchException { assertNotReadonly(); - if (this.parameterBagMap == null) { + if (this.parameterBagMap == null) this.parameterBagMap = new HashMap<>(1, 1.0F); - } ParameterBag bag = this.parameterBagMap.get(bagKey); if (bag == null) { String msg = "No parameter bag exists with key {0}"; //$NON-NLS-1$ @@ -219,9 +270,8 @@ public abstract class GroupedParameterizedElement extends AbstractStrolchElement @Override public > T removeParameter(String bagKey, String paramKey) { assertNotReadonly(); - if (this.parameterBagMap == null) { + if (this.parameterBagMap == null) return null; - } ParameterBag bag = this.parameterBagMap.get(bagKey); if (bag == null) { return null; @@ -272,6 +322,35 @@ public abstract class GroupedParameterizedElement extends AbstractStrolchElement return parameterBag; } + /** + * Returns a {@link Stream} of {@link ParameterBag ParameterBags} + * + * @return the {@link ParameterBag ParameterBags} + */ + @Override + public Stream streamOfParameterBags() { + if (this.parameterBagMap == null || this.parameterBagMap.isEmpty()) + return Stream.empty(); + return this.parameterBagMap.values().stream(); + } + + /** + * Returns a {@link Stream} of {@link ParameterBag ParameterBags} of the given type + * + * @param type + * the type of {@link ParameterBag} to return + * + * @return the {@link ParameterBag ParameterBags} of the given type + */ + @Override + public Stream streamOfParameterBagsByType(String type) { + if (this.parameterBagMap == null || this.parameterBagMap.isEmpty()) + return Stream.empty(); + return this.parameterBagMap.values() // + .stream() // + .filter(map -> map.getType().equals(type)); + } + /** * Returns the {@link ParameterBag ParameterBags} of the given type * @@ -282,10 +361,7 @@ public abstract class GroupedParameterizedElement extends AbstractStrolchElement */ @Override public List getParameterBagsByType(String type) { - return this.parameterBagMap.values() // - .stream() // - .filter(map -> map.getType().equals(type)) // - .collect(Collectors.toList()); + return streamOfParameterBagsByType(type).collect(toList()); } /** diff --git a/li.strolch.model/src/main/java/li/strolch/model/ParameterBagContainer.java b/li.strolch.model/src/main/java/li/strolch/model/ParameterBagContainer.java index 05e006d9f..f444ad31e 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/ParameterBagContainer.java +++ b/li.strolch.model/src/main/java/li/strolch/model/ParameterBagContainer.java @@ -2,6 +2,7 @@ package li.strolch.model; import java.util.List; import java.util.Set; +import java.util.stream.Stream; import li.strolch.exception.StrolchException; import li.strolch.exception.StrolchModelException; @@ -43,6 +44,42 @@ public interface ParameterBagContainer extends StrolchElement { */ > T getParameter(String bagKey, String paramKey, boolean assertExists); + /** + * Returns a {@link Stream} of all the {@link Parameter Parameters} for the given {@link ParameterBag} + * + * @param bagKey + * the key of the {@link ParameterBag} from which the {@link Parameter Parameters} are to be returned + * + * @return the parameters with the given interpretation + */ + Stream> streamOfParameters(String bagKey); + + /** + * Returns a {@link Stream} of all the {@link Parameter Parameters} with the given interpretation + * + * @param bagKey + * the key of the {@link ParameterBag} from which the {@link Parameter} is to be returned + * @param interpretation + * the interpretation for which the parameters are to be returned + * + * @return the parameters with the given interpretation + */ + Stream> streamOfParametersByInterpretation(String bagKey, String interpretation); + + /** + * Returns a {@link Stream} of all the {@link Parameter Parameters} with the given interpretation + * + * @param bagKey + * the key of the {@link ParameterBag} from which the {@link Parameter} is to be returned + * @param interpretation + * the interpretation for which the parameters are to be returned + * @param uom + * the uom for which the parameters are to be returned + * + * @return the parameters with the given interpretation + */ + Stream> streamOfParametersByInterpretationAndUom(String bagKey, String interpretation, String uom); + /** * Returns a list of all the {@link Parameter Parameters} with the given interpretation * @@ -116,6 +153,23 @@ public interface ParameterBagContainer extends StrolchElement { */ ParameterBag getParameterBag(String key, boolean assertExists); + /** + * Returns a {@link Stream} of {@link ParameterBag ParameterBags} + * + * @return the {@link ParameterBag ParameterBags} + */ + Stream streamOfParameterBags(); + + /** + * Returns a {@link Stream} of {@link ParameterBag ParameterBags} of the given type + * + * @param type + * the type of {@link ParameterBag} to return + * + * @return the {@link ParameterBag ParameterBags} of the given type + */ + Stream streamOfParameterBagsByType(String type); + /** * Returns the {@link ParameterBag ParameterBags} of the given type * diff --git a/li.strolch.model/src/main/java/li/strolch/model/ParameterizedElement.java b/li.strolch.model/src/main/java/li/strolch/model/ParameterizedElement.java index 3799ea3c8..841523d14 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/ParameterizedElement.java +++ b/li.strolch.model/src/main/java/li/strolch/model/ParameterizedElement.java @@ -15,9 +15,11 @@ */ package li.strolch.model; +import static java.util.stream.Collectors.toList; + import java.text.MessageFormat; import java.util.*; -import java.util.stream.Collectors; +import java.util.stream.Stream; import li.strolch.exception.StrolchException; import li.strolch.exception.StrolchModelException; @@ -130,9 +132,8 @@ public abstract class ParameterizedElement extends AbstractStrolchElement { */ public void addParameter(Parameter parameter) { assertNotReadonly(); - if (this.parameterMap == null) { + if (this.parameterMap == null) this.parameterMap = new HashMap<>(1, 1.0F); - } if (this.parameterMap.containsKey(parameter.getId())) { String msg = "A Parameter already exists with id {0} on {1}"; @@ -153,24 +154,67 @@ public abstract class ParameterizedElement extends AbstractStrolchElement { @SuppressWarnings("unchecked") public > T removeParameter(String key) { assertNotReadonly(); - if (this.parameterMap == null) { + if (this.parameterMap == null) return null; - } return (T) this.parameterMap.remove(key); } /** - * Returns a list of all the {@link Parameter}s in this {@link ParameterizedElement} + * Returns a list of all the {@link Parameter Parameter} in this {@link ParameterizedElement} * - * @return a list of all the {@link Parameter}s in this {@link ParameterizedElement} + * @return a list of all the {@link Parameter Parameter} in this {@link ParameterizedElement} */ public List> getParameters() { - if (this.parameterMap == null) { + if (this.parameterMap == null) return Collections.emptyList(); - } return new ArrayList<>(this.parameterMap.values()); } + /** + * Returns a {@link Stream} of all the {@link Parameter Parameters} + * + * @return the parameters + */ + public Stream> streamOfParameters() { + if (this.parameterMap == null) + return Stream.empty(); + + return this.parameterMap.values().stream(); + } + + /** + * Returns a {@link Stream} of all the {@link Parameter Parameters} with the given interpretation + * + * @param interpretation + * the interpretation for which the parameters are to be returned + * + * @return the parameters with the given interpretation + */ + public Stream> streamOfParametersByInterpretation(String interpretation) { + if (this.parameterMap == null) + return Stream.empty(); + + return this.parameterMap.values().stream().filter(p -> p.getInterpretation().equals(interpretation)); + } + + /** + * Returns a {@link Stream} of all the {@link Parameter Parameters} with the given interpretation + * + * @param interpretation + * the interpretation for which the parameters are to be returned + * @param uom + * the uom for which the parameters are to be returned + * + * @return the parameters with the given interpretation + */ + public Stream> streamOfParametersByInterpretationAndUom(String interpretation, String uom) { + if (this.parameterMap == null) + return Stream.empty(); + + return this.parameterMap.values().stream() + .filter(p -> p.getInterpretation().equals(interpretation) && p.getUom().equals(uom)); + } + /** * Returns a list of all the {@link Parameter Parameters} with the given interpretation * @@ -180,12 +224,7 @@ public abstract class ParameterizedElement extends AbstractStrolchElement { * @return the parameters with the given interpretation */ public List> getParametersByInterpretation(String interpretation) { - if (this.parameterMap == null) { - return Collections.emptyList(); - } - - return this.parameterMap.values().stream().filter(p -> p.getInterpretation().equals(interpretation)) - .collect(Collectors.toList()); + return streamOfParametersByInterpretation(interpretation).collect(toList()); } /** @@ -199,13 +238,7 @@ public abstract class ParameterizedElement extends AbstractStrolchElement { * @return the parameters with the given interpretation */ public List> getParametersByInterpretationAndUom(String interpretation, String uom) { - if (this.parameterMap == null) { - return Collections.emptyList(); - } - - return this.parameterMap.values().stream() - .filter(p -> p.getInterpretation().equals(interpretation) && p.getUom().equals(uom)) - .collect(Collectors.toList()); + return streamOfParametersByInterpretationAndUom(interpretation, uom).collect(toList()); } /** @@ -226,9 +259,8 @@ public abstract class ParameterizedElement extends AbstractStrolchElement { * @return true, if the {@link Parameter} exists with the given key, false otherwise */ public boolean hasParameter(String key) { - if (this.parameterMap == null) { + if (this.parameterMap == null) return false; - } return this.parameterMap.containsKey(key); } @@ -238,9 +270,8 @@ public abstract class ParameterizedElement extends AbstractStrolchElement { * @return a {@link Set} of all the {@link Parameter} keys in this {@link ParameterizedElement} */ public Set getParameterKeySet() { - if (this.parameterMap == null) { + if (this.parameterMap == null) return Collections.emptySet(); - } return new HashSet<>(this.parameterMap.keySet()); } diff --git a/li.strolch.model/src/main/java/li/strolch/model/Resource.java b/li.strolch.model/src/main/java/li/strolch/model/Resource.java index c5b98ec8a..79c7f260d 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/Resource.java +++ b/li.strolch.model/src/main/java/li/strolch/model/Resource.java @@ -15,8 +15,11 @@ */ package li.strolch.model; +import static java.util.stream.Collectors.toList; + import java.text.MessageFormat; import java.util.*; +import java.util.stream.Stream; import li.strolch.exception.StrolchException; import li.strolch.exception.StrolchModelException; @@ -151,6 +154,79 @@ public class Resource extends AbstractStrolchRootElement implements StrolchRootE return new ArrayList<>(this.timedStateMap.values()); } + /** + * Returns a {@link Stream} of all the {@link StrolchTimedState StrolchTimedStates} + * + * @return the timed states as a stream + */ + public Stream> streamOfTimedStates() { + if (this.timedStateMap == null || this.timedStateMap.isEmpty()) + return Stream.empty(); + + return this.timedStateMap.values().stream(); + } + + /** + * Returns a {@link Stream} of all the {@link StrolchTimedState StrolchTimedStates} with the given interpretation + * + * @param interpretation + * the interpretation for which the timed states are to be returned + * + * @return the timed states with the given interpretation + */ + public Stream> streamOfTimedStatesByInterpretation(String interpretation) { + if (this.timedStateMap == null || this.timedStateMap.isEmpty()) + return Stream.empty(); + + return this.timedStateMap.values().stream().filter(s -> s.getInterpretation().equals(interpretation)); + } + + /** + * Returns a {@link Stream} of all the {@link StrolchTimedState StrolchTimedStates} with the given interpretation + * + * @param interpretation + * the interpretation for which the timed states are to be returned + * @param uom + * the uom for which the timed states are to be returned + * + * @return the timed states with the given interpretation + */ + public Stream> streamOfTimedStatesByInterpretationAndUom(String interpretation, + String uom) { + if (this.timedStateMap == null || this.timedStateMap.isEmpty()) + return Stream.empty(); + + return this.timedStateMap.values().stream() + .filter(s -> s.getInterpretation().equals(interpretation) && s.getUom().equals(uom)); + } + + /** + * Returns a list of all the {@link StrolchTimedState StrolchTimedStates} with the given interpretation + * + * @param interpretation + * the interpretation for which the timed states are to be returned + * + * @return the timed states with the given interpretation + */ + public List> getTimedStatesByInterpretation(String interpretation) { + return streamOfTimedStatesByInterpretation(interpretation).collect(toList()); + } + + /** + * Returns a list of all the {@link StrolchTimedState StrolchTimedStates} with the given interpretation + * + * @param interpretation + * the interpretation for which the timed states are to be returned + * @param uom + * the uom for which the timed states are to be returned + * + * @return the timed states with the given interpretation + */ + public List> getTimedStatesByInterpretationAndUom(String interpretation, + String uom) { + return streamOfTimedStatesByInterpretationAndUom(interpretation, uom).collect(toList()); + } + public boolean hasTimedStates() { return this.timedStateMap != null && !this.timedStateMap.isEmpty(); } diff --git a/li.strolch.model/src/main/java/li/strolch/model/timedstate/StrolchTimedState.java b/li.strolch.model/src/main/java/li/strolch/model/timedstate/StrolchTimedState.java index ec5aae3f6..e4a4ac177 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/timedstate/StrolchTimedState.java +++ b/li.strolch.model/src/main/java/li/strolch/model/timedstate/StrolchTimedState.java @@ -35,7 +35,7 @@ public interface StrolchTimedState extends StrolchElement { * * @return the hidden value */ - public boolean isHidden(); + boolean isHidden(); /** * set the hidden attribute @@ -43,14 +43,14 @@ public interface StrolchTimedState extends StrolchElement { * @param hidden * the new hidden value */ - public void setHidden(boolean hidden); + void setHidden(boolean hidden); /** * Get the UOM of this {@link Parameter} * * @return the UOM */ - public String getUom(); + String getUom(); /** * Set the UOM of this {@link Parameter} @@ -58,14 +58,14 @@ public interface StrolchTimedState extends StrolchElement { * @param uom * the new UOM */ - public void setUom(String uom); + void setUom(String uom); /** * Returns the index of this {@link Parameter}. This can be used to sort the parameters in a UI * * @return the index of this {@link Parameter}. This can be used to sort the parameters in a UI */ - public int getIndex(); + int getIndex(); /** * Set the index of this {@link Parameter}. This can be used to sort the parameters in a UI @@ -73,7 +73,7 @@ public interface StrolchTimedState extends StrolchElement { * @param index * the index to set */ - public void setIndex(int index); + void setIndex(int index); /** * Returns the interpretation of this {@link Parameter}. The interpretation semantic describes what the value of @@ -86,7 +86,7 @@ public interface StrolchTimedState extends StrolchElement { * * @return string value */ - public String getInterpretation(); + String getInterpretation(); /** * Set the interpretation of this {@link Parameter}. The interpretation semantic describes what the value of this @@ -100,15 +100,15 @@ public interface StrolchTimedState extends StrolchElement { * @param interpretation * the interpretation */ - public void setInterpretation(String interpretation); + void setInterpretation(String interpretation); - public ITimeValue getNextMatch(Long time, T value); + ITimeValue getNextMatch(Long time, T value); - public ITimeValue getPreviousMatch(Long time, T value); + ITimeValue getPreviousMatch(Long time, T value); - public > void applyChange(U change, boolean compact); + > void applyChange(U change, boolean compact); - public ITimeValue getStateAt(Long time); + ITimeValue getStateAt(Long time); /** * set the value at a point in time to a given time value object from a string value @@ -120,10 +120,10 @@ public interface StrolchTimedState extends StrolchElement { */ void setStateFromStringAt(final Long time, final String value); - public ITimeVariable getTimeEvolution(); + ITimeVariable getTimeEvolution(); - public void setParent(Resource aThis); + void setParent(Resource aThis); @Override - public StrolchTimedState getClone(); + StrolchTimedState getClone(); }