[New] Added methods to get Parameters, ParameterBags and TimedStates as Streams

This commit is contained in:
Robert von Burg 2019-08-07 11:26:22 +02:00
parent a0c3629fea
commit f97a8f647f
5 changed files with 297 additions and 60 deletions

View File

@ -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<Parameter<?>> 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<Parameter<?>> 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<Parameter<?>> 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<Parameter<?>> 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<Parameter<?>> 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 <U, T extends Parameter<U>> 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<ParameterBag> 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<ParameterBag> 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<ParameterBag> getParameterBagsByType(String type) {
return this.parameterBagMap.values() //
.stream() //
.filter(map -> map.getType().equals(type)) //
.collect(Collectors.toList());
return streamOfParameterBagsByType(type).collect(toList());
}
/**

View File

@ -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 {
*/
<U, T extends Parameter<U>> 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<Parameter<?>> 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<Parameter<?>> 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<Parameter<?>> 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<ParameterBag> 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<ParameterBag> streamOfParameterBagsByType(String type);
/**
* Returns the {@link ParameterBag ParameterBags} of the given type
*

View File

@ -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 <U, T extends Parameter<U>> 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<Parameter<?>> 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<Parameter<?>> 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<Parameter<?>> 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<Parameter<?>> 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<Parameter<?>> 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<Parameter<?>> 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<String> getParameterKeySet() {
if (this.parameterMap == null) {
if (this.parameterMap == null)
return Collections.emptySet();
}
return new HashSet<>(this.parameterMap.keySet());
}

View File

@ -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<? extends StrolchTimedState<?>> 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<? extends StrolchTimedState<?>> 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<? extends StrolchTimedState<?>> 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<? extends StrolchTimedState<?>> 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<? extends StrolchTimedState<?>> getTimedStatesByInterpretationAndUom(String interpretation,
String uom) {
return streamOfTimedStatesByInterpretationAndUom(interpretation, uom).collect(toList());
}
public boolean hasTimedStates() {
return this.timedStateMap != null && !this.timedStateMap.isEmpty();
}

View File

@ -35,7 +35,7 @@ public interface StrolchTimedState<T extends IValue> extends StrolchElement {
*
* @return the hidden value
*/
public boolean isHidden();
boolean isHidden();
/**
* set the hidden attribute
@ -43,14 +43,14 @@ public interface StrolchTimedState<T extends IValue> 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<T extends IValue> 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<T extends IValue> 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<T extends IValue> 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<T extends IValue> extends StrolchElement {
* @param interpretation
* the interpretation
*/
public void setInterpretation(String interpretation);
void setInterpretation(String interpretation);
public ITimeValue<T> getNextMatch(Long time, T value);
ITimeValue<T> getNextMatch(Long time, T value);
public ITimeValue<T> getPreviousMatch(Long time, T value);
ITimeValue<T> getPreviousMatch(Long time, T value);
public <U extends IValueChange<T>> void applyChange(U change, boolean compact);
<U extends IValueChange<T>> void applyChange(U change, boolean compact);
public ITimeValue<T> getStateAt(Long time);
ITimeValue<T> 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<T extends IValue> extends StrolchElement {
*/
void setStateFromStringAt(final Long time, final String value);
public ITimeVariable<T> getTimeEvolution();
ITimeVariable<T> getTimeEvolution();
public void setParent(Resource aThis);
void setParent(Resource aThis);
@Override
public StrolchTimedState<T> getClone();
StrolchTimedState<T> getClone();
}