diff --git a/src/main/java/li/strolch/model/ModelGenerator.java b/src/main/java/li/strolch/model/ModelGenerator.java index 3e6cf99f7..6808069cd 100644 --- a/src/main/java/li/strolch/model/ModelGenerator.java +++ b/src/main/java/li/strolch/model/ModelGenerator.java @@ -15,10 +15,13 @@ */ package li.strolch.model; +import ch.eitchnet.utils.helper.StringHelper; import java.util.ArrayList; import java.util.Date; +import java.util.HashSet; +import java.util.Iterator; import java.util.List; - +import java.util.Set; import li.strolch.model.parameter.BooleanParameter; import li.strolch.model.parameter.DateParameter; import li.strolch.model.parameter.FloatParameter; @@ -27,224 +30,319 @@ import li.strolch.model.parameter.LongParameter; import li.strolch.model.parameter.Parameter; import li.strolch.model.parameter.StringListParameter; import li.strolch.model.parameter.StringParameter; -import ch.eitchnet.utils.helper.StringHelper; +import li.strolch.model.timedstate.BooleanTimedState; +import li.strolch.model.timedstate.FloatTimedState; +import li.strolch.model.timedstate.IntegerTimedState; +import li.strolch.model.timedstate.StringSetTimedState; +import li.strolch.model.timedstate.StrolchTimedState; +import li.strolch.model.timevalue.impl.AString; +import li.strolch.model.timevalue.impl.BooleanValue; +import li.strolch.model.timevalue.impl.FloatValue; +import li.strolch.model.timevalue.impl.IntegerValue; +import li.strolch.model.timevalue.impl.StringSetValue; +import li.strolch.model.timevalue.impl.ValueChange; /** * Class which can be used to generate objects which implement {@link StrolchElement}. These generated classes can then * be used in test classes etc. - * + * * @author Robert von Burg */ @SuppressWarnings("nls") public class ModelGenerator { - public static final String PARAM_BOOLEAN_ID = "@param1"; - public static final String PARAM_BOOLEAN_NAME = "Boolean Param"; + public static final String PARAM_BOOLEAN_ID = "@param1"; + public static final String PARAM_BOOLEAN_NAME = "Boolean Param"; - public static final String PARAM_FLOAT_ID = "@param2"; - public static final String PARAM_FLOAT_NAME = "Float Param"; + public static final String PARAM_FLOAT_ID = "@param2"; + public static final String PARAM_FLOAT_NAME = "Float Param"; - public static final String PARAM_INTEGER_ID = "@param3"; - public static final String PARAM_INTEGER_NAME = "Integer Param"; + public static final String PARAM_INTEGER_ID = "@param3"; + public static final String PARAM_INTEGER_NAME = "Integer Param"; - public static final String PARAM_LONG_ID = "@param4"; - public static final String PARAM_LONG_NAME = "Long Param"; + public static final String PARAM_LONG_ID = "@param4"; + public static final String PARAM_LONG_NAME = "Long Param"; - public static final String PARAM_STRING_ID = "@param5"; - public static final String PARAM_STRING_NAME = "String Param"; + public static final String PARAM_STRING_ID = "@param5"; + public static final String PARAM_STRING_NAME = "String Param"; - public static final String PARAM_DATE_ID = "@param6"; - public static final String PARAM_DATE_NAME = "Date Param"; + public static final String PARAM_DATE_ID = "@param6"; + public static final String PARAM_DATE_NAME = "Date Param"; - public static final String PARAM_LIST_STRING_ID = "@param7"; - public static final String PARAM_LIST_STRING_NAME = "StringList Param"; + public static final String PARAM_LIST_STRING_ID = "@param7"; + public static final String PARAM_LIST_STRING_NAME = "StringList Param"; - public static final String BAG_ID = "@bag01"; - public static final String BAG_NAME = "Test Bag"; - public static final String BAG_TYPE = "TestBag"; + public static final String STATE_FLOAT_ID = "@state1"; + public static final String STATE_FLOAT_NAME = "Float State"; - /** - * Creates an {@link Resource} with the given values and adds a {@link ParameterBag} by calling - * {@link #createParameterBag(String, String, String)} - * - * @param id - * the id of the {@link Resource} - * @param name - * the name of the {@link Resource} - * @param type - * the type of the {@link Resource} - * - * @return the newly created {@link Resource} - */ - public static Resource createResource(String id, String name, String type) { - Resource resource = new Resource(id, name, type); - ParameterBag bag = createParameterBag(BAG_ID, BAG_NAME, BAG_TYPE); - resource.addParameterBag(bag); + public static final String STATE_INTEGER_ID = "@state2"; + public static final String STATE_INTEGER_NAME = "Float State"; - return resource; - } + public static final String STATE_STRING_ID = "@state3"; + public static final String STATE_STRING_NAME = "Float State"; - /** - * Creates a list of {@link Resource Resources} with the given values and adds a {@link ParameterBag} by calling - * {@link #createParameterBag(String, String, String)} - * - * @param idStart - * id range start - * @param count - * the number of elements to create - * @param idPrefix - * the prefix to generate IDs for the {@link Resource Resources} - * @param name - * the name of the {@link Resource} - * @param type - * the type of the {@link Resource} - * - * @return the list of newly created {@link Resource Resources} - */ - public static List createResources(int idStart, int count, String idPrefix, String name, String type) { - List resources = new ArrayList<>(); - for (int i = 0; i < count; i++) { - String id = StringHelper.normalizeLength(String.valueOf((i + idStart)), 8, true, '0'); - resources.add(createResource(idPrefix + "_" + id, name + " " + i, type)); - } - return resources; - } + public static final String STATE_BOOLEAN_ID = "@state4"; + public static final String STATE_BOOLEAN_NAME = "Float State"; - /** - * Creates an {@link Order} with the given values and adds a {@link ParameterBag} by calling - * {@link #createParameterBag(String, String, String)} - * - * @param id - * the id of the {@link Order} - * @param name - * the name of the {@link Order} - * @param type - * the type of the {@link Order} - * - * @return the newly created {@link Order} - */ - public static Order createOrder(String id, String name, String type) { - return createOrder(id, name, type, new Date(), State.CREATED); - } + public static final long STATE_TIME_0 = 0L; + public static final long STATE_TIME_10 = 10L; + public static final long STATE_TIME_20 = 20L; + public static final long STATE_TIME_30 = 30L; - /** - * Creates an {@link Order} with the given values and adds a {@link ParameterBag} by calling - * {@link #createParameterBag(String, String, String)} - * - * @param id - * the id of the {@link Order} - * @param name - * the name of the {@link Order} - * @param type - * the type of the {@link Order} - * @param date - * the date of the {@link Order} - * @param state - * the {@link State} of the {@link Order} - * - * @return the newly created {@link Order} - */ - public static Order createOrder(String id, String name, String type, Date date, State state) { + public static final Double STATE_FLOAT_TIME_0 = 0.0D; + public static final Double STATE_FLOAT_TIME_10 = 10.0D; + public static final Double STATE_FLOAT_TIME_20 = 20.0D; + public static final Double STATE_FLOAT_TIME_30 = 30.0D; - Order order = new Order(id, name, type, date, state); - ParameterBag bag = createParameterBag(BAG_ID, BAG_NAME, BAG_TYPE); - order.addParameterBag(bag); + public static final Integer STATE_INTEGER_TIME_0 = 0; + public static final Integer STATE_INTEGER_TIME_10 = 10; + public static final Integer STATE_INTEGER_TIME_20 = 20; + public static final Integer STATE_INTEGER_TIME_30 = 30; - return order; - } + public static final String STATE_STRING_TIME_0 = "empty"; + public static final String STATE_STRING_TIME_10 = "a"; + public static final String STATE_STRING_TIME_20 = "b"; + public static final String STATE_STRING_TIME_30 = "c"; - /** - * Creates a list of {@link Order Orders} with the given values and adds a {@link ParameterBag} by calling - * {@link #createParameterBag(String, String, String)} - * - * @param idStart - * id range start - * @param count - * the number of elements to create - * @param idPrefix - * the prefix to generate IDs for the {@link Order Orders} - * @param name - * the name of the {@link Order} - * @param type - * the type of the {@link Order} - * - * @return the list of newly created {@link Order Orders} - */ - public static List createOrders(int idStart, int count, String idPrefix, String name, String type) { - List orders = new ArrayList<>(); - for (int i = 0; i < count; i++) { - String id = StringHelper.normalizeLength(String.valueOf((i + idStart)), 8, true, '0'); - orders.add(createOrder(idPrefix + "_" + id, name + " " + i, type)); - } - return orders; - } + public static final Boolean STATE_BOOLEAN_TIME_0 = Boolean.FALSE; + public static final Boolean STATE_BOOLEAN_TIME_10 = Boolean.TRUE; + public static final Boolean STATE_BOOLEAN_TIME_20 = Boolean.FALSE; + public static final Boolean STATE_BOOLEAN_TIME_30 = Boolean.TRUE; - /** - * Creates a {@link ParameterBag} with the given values and calls {@link #addAllParameters(ParameterBag)} to add - * {@link Parameter}s - * - * @param id - * the id of the {@link ParameterBag} - * @param name - * the name of the {@link ParameterBag} - * @param type - * the type of the {@link ParameterBag} - * - * @return the newly created {@link ParameterBag} - */ - public static ParameterBag createParameterBag(String id, String name, String type) { + public static final String BAG_ID = "@bag01"; + public static final String BAG_NAME = "Test Bag"; + public static final String BAG_TYPE = "TestBag"; - ParameterBag bag = new ParameterBag(id, name, type); - addAllParameters(bag); - return bag; - } + /** + * Creates an {@link Resource} with the given values and adds a {@link ParameterBag} by calling + * {@link #createParameterBag(String, String, String)} + * + * @param id the id of the {@link Resource} + * @param name the name of the {@link Resource} + * @param type the type of the {@link Resource} + * + * @return the newly created {@link Resource} + */ + public static Resource createResource(String id, String name, String type) { + Resource resource = new Resource(id, name, type); + ParameterBag bag = createParameterBag(BAG_ID, BAG_NAME, BAG_TYPE); + resource.addParameterBag(bag); + addTimedStates(resource); - /** - * Adds the following {@link Parameter}s to the given {@link ParameterBag}: - *
    - *
  • BooleanParameter - true
  • - *
  • FloatParameter - 44.3
  • - *
  • IntegerParameter - 77
  • - *
  • LongParameter - 4453234566L
  • - *
  • StringParameter - "Strolch"
  • - *
  • DateParameter - 1354295525628L
  • - *
  • StringListParameter - Hello, World
  • - *
- * - * @param bag - */ - public static void addAllParameters(ParameterBag bag) { + return resource; + } - BooleanParameter boolParam = new BooleanParameter(PARAM_BOOLEAN_ID, PARAM_BOOLEAN_NAME, true); - boolParam.setIndex(1); - bag.addParameter(boolParam); + /** + * Creates {@link StrolchTimedState} instances and adds them to the {@link Resource} + * + * @param resource the resource to which to addd the newly created {@link StrolchTimedState} + */ + public static void addTimedStates(Resource resource) { - FloatParameter floatParam = new FloatParameter(PARAM_FLOAT_ID, PARAM_FLOAT_NAME, 44.3); - floatParam.setIndex(2); - bag.addParameter(floatParam); + // float state + FloatTimedState floatTimedState = new FloatTimedState(STATE_FLOAT_ID, STATE_FLOAT_NAME); + floatTimedState.applyChange(new ValueChange<>(STATE_TIME_0, new FloatValue(STATE_FLOAT_TIME_0))); + FloatValue floatValueChange = new FloatValue(STATE_FLOAT_TIME_10); + floatTimedState.applyChange(new ValueChange<>(STATE_TIME_10, floatValueChange)); + floatTimedState.applyChange(new ValueChange<>(STATE_TIME_20, floatValueChange)); + floatTimedState.applyChange(new ValueChange<>(STATE_TIME_30, floatValueChange)); + resource.addTimedState(floatTimedState); - IntegerParameter integerParam = new IntegerParameter(PARAM_INTEGER_ID, PARAM_INTEGER_NAME, 77); - integerParam.setIndex(3); - bag.addParameter(integerParam); + // integer state + IntegerTimedState integerTimedState = new IntegerTimedState(STATE_INTEGER_ID, STATE_INTEGER_NAME); + integerTimedState.applyChange(new ValueChange<>(STATE_TIME_0, + new IntegerValue(STATE_INTEGER_TIME_0))); + IntegerValue integerValueChange = new IntegerValue(STATE_INTEGER_TIME_10); + integerTimedState.applyChange(new ValueChange<>(STATE_TIME_10, integerValueChange)); + integerTimedState.applyChange(new ValueChange<>(STATE_TIME_20, integerValueChange)); + integerTimedState.applyChange(new ValueChange<>(STATE_TIME_30, integerValueChange)); + resource.addTimedState(integerTimedState); - LongParameter longParam = new LongParameter(PARAM_LONG_ID, PARAM_LONG_NAME, 4453234566L); - longParam.setIndex(4); - bag.addParameter(longParam); + // boolean state + BooleanTimedState booleanTimedState = new BooleanTimedState(STATE_BOOLEAN_ID, STATE_BOOLEAN_NAME); + booleanTimedState.applyChange(new ValueChange<>(STATE_TIME_0, + new BooleanValue(STATE_BOOLEAN_TIME_0))); + BooleanValue booleanValueChange = new BooleanValue(STATE_BOOLEAN_TIME_10); + booleanTimedState.applyChange(new ValueChange<>(STATE_TIME_10, booleanValueChange)); + booleanValueChange = booleanValueChange.getInverse(); + booleanTimedState.applyChange(new ValueChange<>(STATE_TIME_20, booleanValueChange)); + booleanValueChange = booleanValueChange.getInverse(); + booleanTimedState.applyChange(new ValueChange<>(STATE_TIME_30, booleanValueChange)); + resource.addTimedState(booleanTimedState); - StringParameter stringParam = new StringParameter(PARAM_STRING_ID, PARAM_STRING_NAME, "Strolch"); - stringParam.setIndex(5); - bag.addParameter(stringParam); + // string state + StringSetTimedState stringTimedState = new StringSetTimedState(STATE_STRING_ID, STATE_STRING_NAME); + StringSetValue change = new StringSetValue(asSet(STATE_STRING_TIME_0)); + stringTimedState.applyChange(new ValueChange<>(STATE_TIME_0, change)); + change = change.getInverse(); + change.add(asSet(STATE_STRING_TIME_10)); + stringTimedState.applyChange(new ValueChange<>(STATE_TIME_10, change)); + removeInverted(change.getValue()); + change = change.getInverse(); + change.add(asSet(STATE_STRING_TIME_20)); + stringTimedState.applyChange(new ValueChange<>(STATE_TIME_20, change)); + removeInverted(change.getValue()); + change = change.getInverse(); + change.add(asSet(STATE_STRING_TIME_30)); + stringTimedState.applyChange(new ValueChange<>(STATE_TIME_30, change)); + resource.addTimedState(stringTimedState); + } - DateParameter dateParam = new DateParameter(PARAM_DATE_ID, PARAM_DATE_NAME, new Date(1354295525628L)); - dateParam.setIndex(6); - bag.addParameter(dateParam); + private static Set asSet(String value) { + HashSet hashSet = new HashSet<>(); + hashSet.add(new AString(value)); + return hashSet; + } - ArrayList stringList = new ArrayList(); - stringList.add("Hello"); - stringList.add("World"); - StringListParameter stringListP = new StringListParameter(PARAM_LIST_STRING_ID, PARAM_LIST_STRING_NAME, - stringList); - stringListP.setIndex(7); - bag.addParameter(stringListP); - } + private static void removeInverted(Set set) { + for (Iterator iter = set.iterator(); iter.hasNext();) { + AString aString = iter.next(); + if (aString.isInverse()) { + iter.remove(); + } + } + } + + /** + * Creates a list of {@link Resource Resources} with the given values and adds a {@link ParameterBag} by calling + * {@link #createParameterBag(String, String, String)} + * + * @param idStart id range start + * @param count the number of elements to create + * @param idPrefix the prefix to generate IDs for the {@link Resource Resources} + * @param name the name of the {@link Resource} + * @param type the type of the {@link Resource} + * + * @return the list of newly created {@link Resource Resources} + */ + public static List createResources(int idStart, int count, String idPrefix, String name, String type) { + List resources = new ArrayList<>(); + for (int i = 0; i < count; i++) { + String id = StringHelper.normalizeLength(String.valueOf((i + idStart)), 8, true, '0'); + resources.add(createResource(idPrefix + "_" + id, name + " " + i, type)); + } + return resources; + } + + /** + * Creates an {@link Order} with the given values and adds a {@link ParameterBag} by calling + * {@link #createParameterBag(String, String, String)} + * + * @param id the id of the {@link Order} + * @param name the name of the {@link Order} + * @param type the type of the {@link Order} + * + * @return the newly created {@link Order} + */ + public static Order createOrder(String id, String name, String type) { + return createOrder(id, name, type, new Date(), State.CREATED); + } + + /** + * Creates an {@link Order} with the given values and adds a {@link ParameterBag} by calling + * {@link #createParameterBag(String, String, String)} + * + * @param id the id of the {@link Order} + * @param name the name of the {@link Order} + * @param type the type of the {@link Order} + * @param date the date of the {@link Order} + * @param state the {@link State} of the {@link Order} + * + * @return the newly created {@link Order} + */ + public static Order createOrder(String id, String name, String type, Date date, State state) { + + Order order = new Order(id, name, type, date, state); + ParameterBag bag = createParameterBag(BAG_ID, BAG_NAME, BAG_TYPE); + order.addParameterBag(bag); + + return order; + } + + /** + * Creates a list of {@link Order Orders} with the given values and adds a {@link ParameterBag} by calling + * {@link #createParameterBag(String, String, String)} + * + * @param idStart id range start + * @param count the number of elements to create + * @param idPrefix the prefix to generate IDs for the {@link Order Orders} + * @param name the name of the {@link Order} + * @param type the type of the {@link Order} + * + * @return the list of newly created {@link Order Orders} + */ + public static List createOrders(int idStart, int count, String idPrefix, String name, String type) { + List orders = new ArrayList<>(); + for (int i = 0; i < count; i++) { + String id = StringHelper.normalizeLength(String.valueOf((i + idStart)), 8, true, '0'); + orders.add(createOrder(idPrefix + "_" + id, name + " " + i, type)); + } + return orders; + } + + /** + * Creates a {@link ParameterBag} with the given values and calls {@link #addAllParameters(ParameterBag)} to add + * {@link Parameter}s + * + * @param id the id of the {@link ParameterBag} + * @param name the name of the {@link ParameterBag} + * @param type the type of the {@link ParameterBag} + * + * @return the newly created {@link ParameterBag} + */ + public static ParameterBag createParameterBag(String id, String name, String type) { + + ParameterBag bag = new ParameterBag(id, name, type); + addAllParameters(bag); + return bag; + } + + /** + * Adds the following {@link Parameter}s to the given {@link ParameterBag}: + *
    + *
  • BooleanParameter - true
  • + *
  • FloatParameter - 44.3
  • + *
  • IntegerParameter - 77
  • + *
  • LongParameter - 4453234566L
  • + *
  • StringParameter - "Strolch"
  • + *
  • DateParameter - 1354295525628L
  • + *
  • StringListParameter - Hello, World
  • + *
+ * + * @param bag + */ + public static void addAllParameters(ParameterBag bag) { + + BooleanParameter boolParam = new BooleanParameter(PARAM_BOOLEAN_ID, PARAM_BOOLEAN_NAME, true); + boolParam.setIndex(1); + bag.addParameter(boolParam); + + FloatParameter floatParam = new FloatParameter(PARAM_FLOAT_ID, PARAM_FLOAT_NAME, 44.3); + floatParam.setIndex(2); + bag.addParameter(floatParam); + + IntegerParameter integerParam = new IntegerParameter(PARAM_INTEGER_ID, PARAM_INTEGER_NAME, 77); + integerParam.setIndex(3); + bag.addParameter(integerParam); + + LongParameter longParam = new LongParameter(PARAM_LONG_ID, PARAM_LONG_NAME, 4453234566L); + longParam.setIndex(4); + bag.addParameter(longParam); + + StringParameter stringParam = new StringParameter(PARAM_STRING_ID, PARAM_STRING_NAME, "Strolch"); + stringParam.setIndex(5); + bag.addParameter(stringParam); + + DateParameter dateParam = new DateParameter(PARAM_DATE_ID, PARAM_DATE_NAME, new Date(1354295525628L)); + dateParam.setIndex(6); + bag.addParameter(dateParam); + + ArrayList stringList = new ArrayList(); + stringList.add("Hello"); + stringList.add("World"); + StringListParameter stringListP = new StringListParameter(PARAM_LIST_STRING_ID, PARAM_LIST_STRING_NAME, + stringList); + stringListP.setIndex(7); + bag.addParameter(stringListP); + } } diff --git a/src/main/java/li/strolch/model/ParameterizedElement.java b/src/main/java/li/strolch/model/ParameterizedElement.java index 30b2f4c20..2696399f7 100644 --- a/src/main/java/li/strolch/model/ParameterizedElement.java +++ b/src/main/java/li/strolch/model/ParameterizedElement.java @@ -15,6 +15,7 @@ */ package li.strolch.model; +import ch.eitchnet.utils.dbc.DBC; import java.text.MessageFormat; import java.util.ArrayList; import java.util.Collections; @@ -45,253 +46,255 @@ import ch.eitchnet.utils.helper.StringHelper; */ public abstract class ParameterizedElement extends AbstractStrolchElement { - private static final long serialVersionUID = 0L; + private static final long serialVersionUID = 0L; - protected GroupedParameterizedElement parent; - protected Map> parameterMap; - protected String type; + protected GroupedParameterizedElement parent; + protected Map> parameterMap; + protected String type; - /** - * Empty Constructor - */ - protected ParameterizedElement() { - // - } + /** + * Empty Constructor + */ + protected ParameterizedElement() { + // + } - /** - * Default Constructor - * - * @param id - * @param name - * @param type - */ - public ParameterizedElement(String id, String name, String type) { - setId(id); - setName(name); - setType(type); - } + /** + * Default Constructor + * + * @param id + * @param name + * @param type + */ + public ParameterizedElement(String id, String name, String type) { + setId(id); + setName(name); + setType(type); + } - @Override - public String getType() { - return this.type; - } + @Override + public String getType() { + return this.type; + } - /** - * Sets the type of this {@link ParameterizedElement} - * - * @param type - * the type to set - */ - public void setType(String type) { - if (StringHelper.isEmpty(type)) { - String msg = "Type may not be empty on element {0}"; //$NON-NLS-1$ - msg = MessageFormat.format(msg, getLocator()); - throw new StrolchException(msg); - } + /** + * Sets the type of this {@link ParameterizedElement} + * + * @param type the type to set + */ + public void setType(String type) { + if (StringHelper.isEmpty(type)) { + String msg = "Type may not be empty on element {0}"; //$NON-NLS-1$ + msg = MessageFormat.format(msg, getLocator()); + throw new StrolchException(msg); + } - this.type = type; - } + this.type = type; + } - /** - * Returns the {@link Parameter} with the given id, or null if it does not exist - * - * @param key - * the id of the parameter to return - * - * @return the {@link Parameter} with the given id, or null if it does not exist - */ - @SuppressWarnings("unchecked") - public T getParameter(String key) { - if (this.parameterMap == null) - return null; - return (T) this.parameterMap.get(key); - } + /** + * Returns the {@link Parameter} with the given id, or null if it does not exist + * + * @param key the id of the parameter to return + * + * @return the {@link Parameter} with the given id, or null if it does not exist + */ + @SuppressWarnings("unchecked") + public T getParameter(String key) { + if (this.parameterMap == null) { + return null; + } + return (T) this.parameterMap.get(key); + } - /** - * Adds the given {@link Parameter} to the {@link ParameterizedElement} - * - * @param parameter - * the {@link Parameter} to add - */ - public void addParameter(Parameter parameter) { - if (this.parameterMap == null) - this.parameterMap = new HashMap>(); - this.parameterMap.put(parameter.getId(), parameter); - parameter.setParent(this); - } + /** + * Adds the given {@link Parameter} to the {@link ParameterizedElement} + * + * @param parameter the {@link Parameter} to add + */ + public void addParameter(Parameter parameter) { + if (this.parameterMap == null) { + this.parameterMap = new HashMap>(); + } + this.parameterMap.put(parameter.getId(), parameter); + parameter.setParent(this); + } - /** - * Removes the {@link Parameter} with the given key - * - * @param key - * the key of the {@link Parameter} to remove - * - * @return the removed {@link Parameter}, or null if it does not exist - */ - @SuppressWarnings("unchecked") - public Parameter removeParameter(String key) { - if (this.parameterMap == null) - return null; - return (Parameter) this.parameterMap.remove(key); - } + /** + * Removes the {@link Parameter} with the given key + * + * @param key the key of the {@link Parameter} to remove + * + * @return the removed {@link Parameter}, or null if it does not exist + */ + @SuppressWarnings("unchecked") + public Parameter removeParameter(String key) { + if (this.parameterMap == null) { + return null; + } + return (Parameter) this.parameterMap.remove(key); + } - /** - * Returns a list of all the {@link Parameter}s in this {@link ParameterizedElement} - * - * @return a list of all the {@link Parameter}s in this {@link ParameterizedElement} - */ - public List> getParameters() { - if (this.parameterMap == null) - return Collections.emptyList(); - return new ArrayList>(this.parameterMap.values()); - } + /** + * Returns a list of all the {@link Parameter}s in this {@link ParameterizedElement} + * + * @return a list of all the {@link Parameter}s in this {@link ParameterizedElement} + */ + public List> getParameters() { + if (this.parameterMap == null) { + return Collections.emptyList(); + } + return new ArrayList>(this.parameterMap.values()); + } - /** - * Returns true, if the this {@link ParameterizedElement} has any {@link Parameter Parameters}, false otherwise - * - * @return true, if the this {@link ParameterizedElement} has any {@link Parameter Parameters}, false otherwise - */ - public boolean hasParameters() { - return this.parameterMap != null && !this.parameterMap.isEmpty(); - } + /** + * Returns true, if the this {@link ParameterizedElement} has any {@link Parameter Parameters}, false otherwise + * + * @return true, if the this {@link ParameterizedElement} has any {@link Parameter Parameters}, false otherwise + */ + public boolean hasParameters() { + return this.parameterMap != null && !this.parameterMap.isEmpty(); + } - /** - * Returns true, if the {@link Parameter} exists with the given key, false otherwise - * - * @param key - * the key of the {@link Parameter} to check for - * - * @return true, if the {@link Parameter} exists with the given key, false otherwise - */ - public boolean hasParameter(String key) { - if (this.parameterMap == null) - return false; - return this.parameterMap.containsKey(key); - } + /** + * Returns true, if the {@link Parameter} exists with the given key, false otherwise + * + * @param key the key of the {@link Parameter} to check for + * + * @return true, if the {@link Parameter} exists with the given key, false otherwise + */ + public boolean hasParameter(String key) { + if (this.parameterMap == null) { + return false; + } + return this.parameterMap.containsKey(key); + } - /** - * Returns a {@link Set} of all the {@link Parameter} keys in this {@link ParameterizedElement} - * - * @return a {@link Set} of all the {@link Parameter} keys in this {@link ParameterizedElement} - */ - public Set getParameterKeySet() { - if (this.parameterMap == null) - return Collections.emptySet(); - return new HashSet(this.parameterMap.keySet()); - } + /** + * Returns a {@link Set} of all the {@link Parameter} keys in this {@link ParameterizedElement} + * + * @return a {@link Set} of all the {@link Parameter} keys in this {@link ParameterizedElement} + */ + public Set getParameterKeySet() { + if (this.parameterMap == null) { + return Collections.emptySet(); + } + return new HashSet(this.parameterMap.keySet()); + } - @Override - public void fillLocator(LocatorBuilder lb) { - this.parent.fillLocator(lb); - lb.append(this.id); - } + @Override + public void fillLocator(LocatorBuilder lb) { + this.parent.fillLocator(lb); + lb.append(this.id); + } - @Override - public Locator getLocator() { - LocatorBuilder lb = new LocatorBuilder(); - fillLocator(lb); - return lb.build(); - } + @Override + public Locator getLocator() { + LocatorBuilder lb = new LocatorBuilder(); + fillLocator(lb); + return lb.build(); + } - @Override - protected void fromDom(Element element) { - super.fromDom(element); + @Override + protected void fromDom(Element element) { + super.fromDom(element); - String type = element.getAttribute(Tags.TYPE); - setType(type); + String type = element.getAttribute(Tags.TYPE); + setType(type); - // add all the parameters - NodeList parameterElements = element.getElementsByTagName(Tags.PARAMETER); - for (int i = 0; i < parameterElements.getLength(); i++) { - Element paramElement = (Element) parameterElements.item(i); - String paramtype = paramElement.getAttribute(Tags.TYPE); + // add all the parameters + NodeList parameterElements = element.getElementsByTagName(Tags.PARAMETER); + for (int i = 0; i < parameterElements.getLength(); i++) { + Element paramElement = (Element) parameterElements.item(i); + String paramtype = paramElement.getAttribute(Tags.TYPE); - if (paramtype.equals(StringParameter.TYPE)) { - StringParameter param = new StringParameter(paramElement); - addParameter(param); - } else if (paramtype.equals(IntegerParameter.TYPE)) { - IntegerParameter param = new IntegerParameter(paramElement); - addParameter(param); - } else if (paramtype.equals(FloatParameter.TYPE)) { - FloatParameter param = new FloatParameter(paramElement); - addParameter(param); - } else if (paramtype.equals(LongParameter.TYPE)) { - LongParameter param = new LongParameter(paramElement); - addParameter(param); - } else if (paramtype.equals(DateParameter.TYPE)) { - DateParameter param = new DateParameter(paramElement); - addParameter(param); - } else if (paramtype.equals(BooleanParameter.TYPE)) { - BooleanParameter param = new BooleanParameter(paramElement); - addParameter(param); - } else if (paramtype.equals(StringListParameter.TYPE)) { - StringListParameter param = new StringListParameter(paramElement); - addParameter(param); - } else { - String msg = "What kind of parameter is this: {0}"; //$NON-NLS-1$ - msg = MessageFormat.format(msg, paramtype); - throw new StrolchException(msg); - } - } - } + DBC.PRE.assertNotEmpty("Type must be set on Parameter for bag with id " + id, paramtype); - @Override - protected void fillElement(Element element) { - super.fillElement(element); + if (paramtype.equals(StringParameter.TYPE)) { + StringParameter param = new StringParameter(paramElement); + addParameter(param); + } else if (paramtype.equals(IntegerParameter.TYPE)) { + IntegerParameter param = new IntegerParameter(paramElement); + addParameter(param); + } else if (paramtype.equals(FloatParameter.TYPE)) { + FloatParameter param = new FloatParameter(paramElement); + addParameter(param); + } else if (paramtype.equals(LongParameter.TYPE)) { + LongParameter param = new LongParameter(paramElement); + addParameter(param); + } else if (paramtype.equals(DateParameter.TYPE)) { + DateParameter param = new DateParameter(paramElement); + addParameter(param); + } else if (paramtype.equals(BooleanParameter.TYPE)) { + BooleanParameter param = new BooleanParameter(paramElement); + addParameter(param); + } else if (paramtype.equals(StringListParameter.TYPE)) { + StringListParameter param = new StringListParameter(paramElement); + addParameter(param); + } else { + String msg = "What kind of parameter is this: {0}"; //$NON-NLS-1$ + msg = MessageFormat.format(msg, paramtype); + throw new StrolchException(msg); + } + } + } - if (this.parameterMap != null) { - for (Parameter parameter : this.parameterMap.values()) { - element.appendChild(parameter.toDom(element.getOwnerDocument())); - } - } - } + @Override + protected void fillElement(Element element) { + super.fillElement(element); - @Override - protected void fillClone(StrolchElement clone) { - super.fillClone(clone); - ParameterizedElement peClone = (ParameterizedElement) clone; - peClone.setType(this.type); - if (this.parameterMap != null) { - for (Parameter param : this.parameterMap.values()) { - peClone.addParameter(param.getClone()); - } - } - } + if (this.parameterMap != null) { + for (Parameter parameter : this.parameterMap.values()) { + element.appendChild(parameter.toDom(element.getOwnerDocument())); + } + } + } - @Override - public GroupedParameterizedElement getParent() { - return this.parent; - } + @Override + protected void fillClone(StrolchElement clone) { + super.fillClone(clone); + ParameterizedElement peClone = (ParameterizedElement) clone; + peClone.setType(this.type); + if (this.parameterMap != null) { + for (Parameter param : this.parameterMap.values()) { + peClone.addParameter(param.getClone()); + } + } + } - /** - * Set the parent for this {@link ParameterizedElement} - * - * @param parent - * the parent to set - */ - public void setParent(GroupedParameterizedElement parent) { - this.parent = parent; - } + @Override + public GroupedParameterizedElement getParent() { + return this.parent; + } - @Override - public StrolchRootElement getRootElement() { - return this.parent.getRootElement(); - } + /** + * Set the parent for this {@link ParameterizedElement} + * + * @param parent the parent to set + */ + public void setParent(GroupedParameterizedElement parent) { + this.parent = parent; + } - @SuppressWarnings("nls") - @Override - public String toString() { + @Override + public StrolchRootElement getRootElement() { + return this.parent.getRootElement(); + } - StringBuilder builder = new StringBuilder(); + @SuppressWarnings("nls") + @Override + public String toString() { - builder.append("ParameterizedElement [id="); - builder.append(this.id); - builder.append(", name="); - builder.append(this.name); - builder.append(", type="); - builder.append(this.type); - builder.append("]"); + StringBuilder builder = new StringBuilder(); - return builder.toString(); - } + builder.append("ParameterizedElement [id="); + builder.append(this.id); + builder.append(", name="); + builder.append(this.name); + builder.append(", type="); + builder.append(this.type); + builder.append("]"); + + return builder.toString(); + } } diff --git a/src/main/java/li/strolch/model/Resource.java b/src/main/java/li/strolch/model/Resource.java index 11fe1cb57..86b012e7e 100644 --- a/src/main/java/li/strolch/model/Resource.java +++ b/src/main/java/li/strolch/model/Resource.java @@ -15,105 +15,202 @@ */ package li.strolch.model; +import ch.eitchnet.utils.dbc.DBC; +import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import li.strolch.exception.StrolchException; import li.strolch.model.Locator.LocatorBuilder; +import li.strolch.model.timedstate.BooleanTimedState; +import li.strolch.model.timedstate.FloatTimedState; +import li.strolch.model.timedstate.IntegerTimedState; +import li.strolch.model.timedstate.StringSetTimedState; +import li.strolch.model.timedstate.StrolchTimedState; import li.strolch.model.visitor.StrolchRootElementVisitor; - import org.w3c.dom.Document; import org.w3c.dom.Element; +import org.w3c.dom.NodeList; /** * @author Robert von Burg */ public class Resource extends GroupedParameterizedElement implements StrolchRootElement { - private static final long serialVersionUID = 0L; + private static final long serialVersionUID = 0L; - /** - * Empty constructor - */ - public Resource() { - // - } + private Map> timedStateMap; - /** - * Default constructor - * - * @param id - * @param name - * @param type - */ - public Resource(String id, String name, String type) { - super(id, name, type); - } + /** + * Empty constructor + */ + public Resource() { + // + } - /** - * DOM Constructor - * - * @param element - */ - public Resource(Element element) { - super.fromDom(element); - } + /** + * Default constructor + * + * @param id + * @param name + * @param type + */ + public Resource(String id, String name, String type) { + super(id, name, type); + } - @Override - public Element toDom(Document doc) { + /** + * DOM Constructor + * + * @param element + */ + public Resource(Element element) { + super.fromDom(element); - Element element = doc.createElement(Tags.RESOURCE); - fillElement(element); + NodeList timedStateElems = element.getElementsByTagName(Tags.TIMED_STATE); + for (int i = 0; i < timedStateElems.getLength(); i++) { + Element timedStateElem = (Element) timedStateElems.item(i); + String typeS = timedStateElem.getAttribute(Tags.TYPE); - return element; - } + DBC.PRE.assertNotEmpty("Type must be set on TimedState for resource with id " + id, typeS); - @Override - public Resource getClone() { - Resource clone = new Resource(); + if (typeS.equals(FloatTimedState.TYPE)) { + StrolchTimedState timedState = new FloatTimedState(timedStateElem); + addTimedState(timedState); + } else if (typeS.equals(IntegerTimedState.TYPE)) { + StrolchTimedState timedState = new IntegerTimedState(timedStateElem); + addTimedState(timedState); + } else if (typeS.equals(BooleanTimedState.TYPE)) { + StrolchTimedState timedState = new BooleanTimedState(timedStateElem); + addTimedState(timedState); + } else if (typeS.equals(StringSetTimedState.TYPE)) { + StrolchTimedState timedState = new StringSetTimedState(timedStateElem); + addTimedState(timedState); + } else { + String msg = "What kind of TimedState is this: {0}"; //$NON-NLS-1$ + msg = MessageFormat.format(msg, typeS); + throw new StrolchException(msg); + } + } + } - super.fillClone(clone); + public void addTimedState(StrolchTimedState strolchTimedState) { + if (this.timedStateMap == null) { + this.timedStateMap = new HashMap<>(); + } - return clone; - } + this.timedStateMap.put(strolchTimedState.getId(), strolchTimedState); + strolchTimedState.setParent(this); + } - @Override - protected void fillLocator(LocatorBuilder lb) { - lb.append(Tags.RESOURCE).append(getType()).append(getId()); - } + @SuppressWarnings({"rawtypes", "unchecked"}) + public T getTimedState(String id) { + if (this.timedStateMap == null) { + return null; + } + return (T) this.timedStateMap.get(id); + } - @Override - public Locator getLocator() { - LocatorBuilder lb = new LocatorBuilder(); - fillLocator(lb); - return lb.build(); - } + @SuppressWarnings({"unchecked", "rawtypes"}) + public T removeTimedState(String id) { + if (this.timedStateMap == null) { + return null; + } + return (T) this.timedStateMap.remove(id); + } - @Override - public StrolchElement getParent() { - return null; - } + public Set getTimedStateKeySet() { + if (this.timedStateMap == null) { + return Collections.emptySet(); + } + return new HashSet<>(this.timedStateMap.keySet()); + } - @Override - public Resource getRootElement() { - return this; - } + public List> getTimedStates() { + if (this.timedStateMap == null) { + return Collections.emptyList(); + } + return new ArrayList<>(this.timedStateMap.values()); + } - @Override - public T accept(StrolchRootElementVisitor visitor) { - return visitor.visitResource(this); - } + public boolean hasTimedStates() { + return this.timedStateMap != null && !this.timedStateMap.isEmpty(); + } - @SuppressWarnings("nls") - @Override - public String toString() { + public boolean hasTimedState(String id) { + return this.timedStateMap != null && this.timedStateMap.containsKey(id); + } - StringBuilder builder = new StringBuilder(); + @Override + public Element toDom(Document doc) { - builder.append("Resource [id="); - builder.append(this.id); - builder.append(", name="); - builder.append(this.name); - builder.append(", type="); - builder.append(this.type); - builder.append("]"); + Element element = doc.createElement(Tags.RESOURCE); + fillElement(element); - return builder.toString(); - } + if (this.timedStateMap != null) { + for (StrolchTimedState state : this.timedStateMap.values()) { + Element timedStateElem = state.toDom(element.getOwnerDocument()); + element.appendChild(timedStateElem); + } + } + + return element; + } + + @Override + public Resource getClone() { + Resource clone = new Resource(); + + super.fillClone(clone); + + return clone; + } + + @Override + public void fillLocator(LocatorBuilder lb) { + lb.append(Tags.RESOURCE).append(getType()).append(getId()); + } + + @Override + public Locator getLocator() { + LocatorBuilder lb = new LocatorBuilder(); + fillLocator(lb); + return lb.build(); + } + + @Override + public StrolchElement getParent() { + return null; + } + + @Override + public Resource getRootElement() { + return this; + } + + @Override + public T accept(StrolchRootElementVisitor visitor) { + return visitor.visitResource(this); + } + + @SuppressWarnings("nls") + @Override + public String toString() { + + StringBuilder builder = new StringBuilder(); + + builder.append("Resource [id="); + builder.append(this.id); + builder.append(", name="); + builder.append(this.name); + builder.append(", type="); + builder.append(this.type); + builder.append("]"); + + return builder.toString(); + } } diff --git a/src/main/java/li/strolch/model/Tags.java b/src/main/java/li/strolch/model/Tags.java index b68bdac9c..52733c40f 100644 --- a/src/main/java/li/strolch/model/Tags.java +++ b/src/main/java/li/strolch/model/Tags.java @@ -25,11 +25,13 @@ public class Tags { public static final String DATE = "Date"; public static final String STATE = "State"; public static final String VALUE = "Value"; + public static final String TIME = "Time"; public static final String INTERPRETATION = "Interpretation"; public static final String UOM = "Uom"; public static final String HIDDEN = "Hidden"; public static final String INDEX = "Index"; public static final String PARAMETER = "Parameter"; + public static final String TIMED_STATE = "TimedState"; public static final String PARAMETERIZED_ELEMENT = "ParameterizedElement"; public static final String RESOURCE = "Resource"; public static final String ORDER = "Order"; diff --git a/src/main/java/li/strolch/model/parameter/AbstractParameter.java b/src/main/java/li/strolch/model/parameter/AbstractParameter.java index 7bcd7b324..a2895713a 100644 --- a/src/main/java/li/strolch/model/parameter/AbstractParameter.java +++ b/src/main/java/li/strolch/model/parameter/AbstractParameter.java @@ -200,7 +200,7 @@ public abstract class AbstractParameter extends AbstractStrolchElement implem public Locator getLocator() { LocatorBuilder lb = new LocatorBuilder(); this.parent.fillLocator(lb); - this.fillLocator(lb); + fillLocator(lb); return lb.build(); } diff --git a/src/main/java/li/strolch/model/timedstate/AbstractStrolchTimedState.java b/src/main/java/li/strolch/model/timedstate/AbstractStrolchTimedState.java new file mode 100644 index 000000000..61f118599 --- /dev/null +++ b/src/main/java/li/strolch/model/timedstate/AbstractStrolchTimedState.java @@ -0,0 +1,127 @@ +/* + * Copyright 2013 Robert von Burg + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package li.strolch.model.timedstate; + +import li.strolch.model.AbstractStrolchElement; +import li.strolch.model.Locator; +import li.strolch.model.Locator.LocatorBuilder; +import li.strolch.model.Resource; +import li.strolch.model.StrolchElement; +import li.strolch.model.StrolchRootElement; +import li.strolch.model.Tags; +import li.strolch.model.timevalue.ITimeValue; +import li.strolch.model.timevalue.ITimeVariable; +import li.strolch.model.timevalue.IValue; +import li.strolch.model.timevalue.IValueChange; + +/** + * @author Robert von Burg + */ +@SuppressWarnings("rawtypes") +public abstract class AbstractStrolchTimedState extends AbstractStrolchElement implements + StrolchTimedState { + + private static final long serialVersionUID = 1L; + + protected Resource parent; + protected ITimedState state; + + public AbstractStrolchTimedState() { + this.state = new TimedState<>(); + } + + public AbstractStrolchTimedState(String id, String name) { + super(id, name); + this.state = new TimedState<>(); + } + + @Override + public ITimeValue getNextMatch(Long time, T value) { + return this.state.getNextMatch(time, value); + } + + @Override + public ITimeValue getPreviousMatch(Long time, T value) { + return this.state.getPreviousMatch(time, value); + } + + @Override + public > void applyChange(U change) { + this.state.applyChange(change); + } + + @Override + public ITimeValue getStateAt(Long time) { + return this.state.getStateAt(time); + } + + @Override + public ITimeVariable getTimeEvolution() { + return this.state.getTimeEvolution(); + } + + @Override + public StrolchElement getParent() { + return this.parent; + } + + @Override + public void setParent(Resource parent) { + this.parent = parent; + } + + @Override + public StrolchRootElement getRootElement() { + return this.parent; + } + + @Override + protected void fillLocator(LocatorBuilder locatorBuilder) { + locatorBuilder.append(Tags.TIMED_STATE); + locatorBuilder.append(getId()); + } + + @Override + public Locator getLocator() { + LocatorBuilder lb = new LocatorBuilder(); + this.parent.fillLocator(lb); + fillLocator(lb); + return lb.build(); + } + + protected void fillClone(AbstractStrolchTimedState clone) { + super.fillClone(clone); + clone.state = this.state.getCopy(); + } + + @SuppressWarnings("nls") + @Override + public String toString() { + + StringBuilder builder = new StringBuilder(); + + builder.append(getClass().getSimpleName()); + builder.append(" [id="); + builder.append(this.id); + builder.append(", name="); + builder.append(this.name); + builder.append(", valueNow="); + builder.append(this.state.getStateAt(System.currentTimeMillis())); + builder.append("]"); + + return builder.toString(); + } +} diff --git a/src/main/java/li/strolch/model/timedstate/BooleanTimedState.java b/src/main/java/li/strolch/model/timedstate/BooleanTimedState.java new file mode 100644 index 000000000..fce5aa0c1 --- /dev/null +++ b/src/main/java/li/strolch/model/timedstate/BooleanTimedState.java @@ -0,0 +1,88 @@ +/* + * Copyright 2013 Robert von Burg + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package li.strolch.model.timedstate; + +import java.util.SortedSet; +import li.strolch.model.StrolchElement; +import li.strolch.model.Tags; +import li.strolch.model.timevalue.ITimeValue; +import li.strolch.model.timevalue.impl.BooleanValue; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; + +/** + * @author Robert von Burg + */ +public class BooleanTimedState extends AbstractStrolchTimedState { + + private static final long serialVersionUID = 1L; + + public static final String TYPE = "BooleanState"; + + public BooleanTimedState() { + super(); + } + + public BooleanTimedState(String id, String name) { + super(id, name); + } + + public BooleanTimedState(Element element) { + super.fromDom(element); + + this.state = new TimedState<>(); + + NodeList timeValueElems = element.getElementsByTagName(Tags.VALUE); + for (int i = 0; i < timeValueElems.getLength(); i++) { + Element timeValueElem = (Element) timeValueElems.item(i); + Long time = Long.valueOf(timeValueElem.getAttribute(Tags.TIME)); + Boolean value = Boolean.valueOf(timeValueElem.getAttribute(Tags.VALUE)); + BooleanValue booleanValue = new BooleanValue(value); + this.state.getTimeEvolution().setValueAt(time, booleanValue); + } + } + + @Override + public Element toDom(Document doc) { + + Element stateElement = doc.createElement(Tags.TIMED_STATE); + super.fillElement(stateElement); + SortedSet> values = this.state.getTimeEvolution().getValues(); + for (ITimeValue timeValue : values) { + Long time = timeValue.getTime(); + BooleanValue value = timeValue.getValue(); + Element valueElem = doc.createElement(Tags.VALUE); + valueElem.setAttribute(Tags.TIME, time.toString()); + valueElem.setAttribute(Tags.VALUE, value.getValue().toString()); + stateElement.appendChild(valueElem); + } + + return stateElement; + } + + @Override + public String getType() { + return TYPE; + } + + @Override + public StrolchElement getClone() { + BooleanTimedState clone = new BooleanTimedState(); + fillClone(clone); + return clone; + } +} diff --git a/src/main/java/li/strolch/model/timedstate/FloatTimedState.java b/src/main/java/li/strolch/model/timedstate/FloatTimedState.java new file mode 100644 index 000000000..9c153af55 --- /dev/null +++ b/src/main/java/li/strolch/model/timedstate/FloatTimedState.java @@ -0,0 +1,88 @@ +/* + * Copyright 2013 Robert von Burg + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package li.strolch.model.timedstate; + +import java.util.SortedSet; +import li.strolch.model.StrolchElement; +import li.strolch.model.Tags; +import li.strolch.model.timevalue.ITimeValue; +import li.strolch.model.timevalue.impl.FloatValue; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; + +/** + * @author Robert von Burg + */ +public class FloatTimedState extends AbstractStrolchTimedState { + + private static final long serialVersionUID = 1L; + + public static final String TYPE = "FloatState"; + + public FloatTimedState() { + super(); + } + + public FloatTimedState(String id, String name) { + super(id, name); + } + + public FloatTimedState(Element element) { + super.fromDom(element); + + this.state = new TimedState<>(); + + NodeList timeValueElems = element.getElementsByTagName(Tags.VALUE); + for (int i = 0; i < timeValueElems.getLength(); i++) { + Element timeValueElem = (Element) timeValueElems.item(i); + Long time = Long.valueOf(timeValueElem.getAttribute(Tags.TIME)); + Double value = Double.valueOf(timeValueElem.getAttribute(Tags.VALUE)); + FloatValue floatValue = new FloatValue(value); + this.state.getTimeEvolution().setValueAt(time, floatValue); + } + } + + @Override + public Element toDom(Document doc) { + + Element stateElement = doc.createElement(Tags.TIMED_STATE); + super.fillElement(stateElement); + SortedSet> values = this.state.getTimeEvolution().getValues(); + for (ITimeValue timeValue : values) { + Long time = timeValue.getTime(); + FloatValue value = timeValue.getValue(); + Element valueElem = doc.createElement(Tags.VALUE); + valueElem.setAttribute(Tags.TIME, time.toString()); + valueElem.setAttribute(Tags.VALUE, value.getValue().toString()); + stateElement.appendChild(valueElem); + } + + return stateElement; + } + + @Override + public String getType() { + return TYPE; + } + + @Override + public StrolchElement getClone() { + FloatTimedState clone = new FloatTimedState(); + fillClone(clone); + return clone; + } +} diff --git a/src/main/java/li/strolch/model/timedstate/ITimedState.java b/src/main/java/li/strolch/model/timedstate/ITimedState.java index 27bfbb2ce..6115a5b1e 100644 --- a/src/main/java/li/strolch/model/timedstate/ITimedState.java +++ b/src/main/java/li/strolch/model/timedstate/ITimedState.java @@ -45,7 +45,7 @@ public interface ITimedState { * @param change * the state change to be applied */ - void applyChange(final IValueChange change); + > void applyChange(final U change); /** * @return the state at the given time @@ -57,4 +57,8 @@ public interface ITimedState { */ ITimeVariable getTimeEvolution(); + /** + * @return a copy of this timed state + */ + ITimedState getCopy(); } \ No newline at end of file diff --git a/src/main/java/li/strolch/model/timedstate/IntegerTimedState.java b/src/main/java/li/strolch/model/timedstate/IntegerTimedState.java new file mode 100644 index 000000000..31e7cec32 --- /dev/null +++ b/src/main/java/li/strolch/model/timedstate/IntegerTimedState.java @@ -0,0 +1,88 @@ +/* + * Copyright 2013 Robert von Burg + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package li.strolch.model.timedstate; + +import java.util.SortedSet; +import li.strolch.model.StrolchElement; +import li.strolch.model.Tags; +import li.strolch.model.timevalue.ITimeValue; +import li.strolch.model.timevalue.impl.IntegerValue; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; + +/** + * @author Robert von Burg + */ +public class IntegerTimedState extends AbstractStrolchTimedState { + + private static final long serialVersionUID = 1L; + + public static final String TYPE = "IntegerState"; + + public IntegerTimedState() { + super(); + } + + public IntegerTimedState(String id, String name) { + super(id, name); + } + + public IntegerTimedState(Element element) { + super.fromDom(element); + + this.state = new TimedState<>(); + + NodeList timeValueElems = element.getElementsByTagName(Tags.VALUE); + for (int i = 0; i < timeValueElems.getLength(); i++) { + Element timeValueElem = (Element) timeValueElems.item(i); + Long time = Long.valueOf(timeValueElem.getAttribute(Tags.TIME)); + Integer value = Integer.valueOf(timeValueElem.getAttribute(Tags.VALUE)); + IntegerValue integerValue = new IntegerValue(value); + this.state.getTimeEvolution().setValueAt(time, integerValue); + } + } + + @Override + public Element toDom(Document doc) { + + Element stateElement = doc.createElement(Tags.TIMED_STATE); + super.fillElement(stateElement); + SortedSet> values = this.state.getTimeEvolution().getValues(); + for (ITimeValue timeValue : values) { + Long time = timeValue.getTime(); + IntegerValue value = timeValue.getValue(); + Element valueElem = doc.createElement(Tags.VALUE); + valueElem.setAttribute(Tags.TIME, time.toString()); + valueElem.setAttribute(Tags.VALUE, value.getValue().toString()); + stateElement.appendChild(valueElem); + } + + return stateElement; + } + + @Override + public String getType() { + return TYPE; + } + + @Override + public StrolchElement getClone() { + IntegerTimedState clone = new IntegerTimedState(); + fillClone(clone); + return clone; + } +} diff --git a/src/main/java/li/strolch/model/timedstate/StringSetTimedState.java b/src/main/java/li/strolch/model/timedstate/StringSetTimedState.java new file mode 100644 index 000000000..13722527e --- /dev/null +++ b/src/main/java/li/strolch/model/timedstate/StringSetTimedState.java @@ -0,0 +1,111 @@ +/* + * Copyright 2013 Robert von Burg + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package li.strolch.model.timedstate; + +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; +import java.util.SortedSet; +import li.strolch.model.StrolchElement; +import li.strolch.model.Tags; +import li.strolch.model.timevalue.ITimeValue; +import li.strolch.model.timevalue.impl.AString; +import li.strolch.model.timevalue.impl.StringSetValue; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; + +/** + * @author Robert von Burg + */ +public class StringSetTimedState extends AbstractStrolchTimedState { + + private static final long serialVersionUID = 1L; + + public static final String TYPE = "StringSetState"; + + public StringSetTimedState() { + super(); + } + + public StringSetTimedState(String id, String name) { + super(id, name); + } + + public StringSetTimedState(Element element) { + super.fromDom(element); + + this.state = new TimedState<>(); + + NodeList timeValueElems = element.getElementsByTagName(Tags.VALUE); + for (int i = 0; i < timeValueElems.getLength(); i++) { + Element timeValueElem = (Element) timeValueElems.item(i); + Long time = Long.valueOf(timeValueElem.getAttribute(Tags.TIME)); + + String valueAsString = timeValueElem.getAttribute(Tags.VALUE); + Set value = new HashSet<>(); + String[] values = valueAsString.split(","); + for (String s : values) { + value.add(new AString(s.trim())); + } + + StringSetValue integerValue = new StringSetValue(value); + this.state.getTimeEvolution().setValueAt(time, integerValue); + } + } + + @Override + public Element toDom(Document doc) { + + Element stateElement = doc.createElement(Tags.TIMED_STATE); + super.fillElement(stateElement); + SortedSet> values = this.state.getTimeEvolution().getValues(); + for (ITimeValue timeValue : values) { + Long time = timeValue.getTime(); + StringSetValue stringSetValue = timeValue.getValue(); + + Set value = stringSetValue.getValue(); + StringBuilder sb = new StringBuilder(); + Iterator iter = value.iterator(); + while (iter.hasNext()) { + sb.append(iter.next().getString()); + if (iter.hasNext()) { + sb.append(", "); + } + } + String valueAsString = sb.toString(); + + Element valueElem = doc.createElement(Tags.VALUE); + valueElem.setAttribute(Tags.TIME, time.toString()); + valueElem.setAttribute(Tags.VALUE, valueAsString); + stateElement.appendChild(valueElem); + } + + return stateElement; + } + + @Override + public String getType() { + return TYPE; + } + + @Override + public StrolchElement getClone() { + StringSetTimedState clone = new StringSetTimedState(); + fillClone(clone); + return clone; + } +} diff --git a/src/main/java/li/strolch/model/timedstate/StrolchTimedState.java b/src/main/java/li/strolch/model/timedstate/StrolchTimedState.java new file mode 100644 index 000000000..f2ab09454 --- /dev/null +++ b/src/main/java/li/strolch/model/timedstate/StrolchTimedState.java @@ -0,0 +1,42 @@ +/* + * Copyright 2013 Robert von Burg + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package li.strolch.model.timedstate; + +import li.strolch.model.Resource; +import li.strolch.model.StrolchElement; +import li.strolch.model.timevalue.ITimeValue; +import li.strolch.model.timevalue.ITimeVariable; +import li.strolch.model.timevalue.IValue; +import li.strolch.model.timevalue.IValueChange; + +/** + * @author Robert von Burg + */ +@SuppressWarnings("rawtypes") +public interface StrolchTimedState extends StrolchElement { + + public ITimeValue getNextMatch(Long time, T value); + + public ITimeValue getPreviousMatch(Long time, T value); + + public > void applyChange(U change); + + public ITimeValue getStateAt(Long time); + + public ITimeVariable getTimeEvolution(); + + public void setParent(Resource aThis); +} diff --git a/src/main/java/li/strolch/model/timedstate/TimedState.java b/src/main/java/li/strolch/model/timedstate/TimedState.java index b4a6bb0f3..23f9fbdf8 100644 --- a/src/main/java/li/strolch/model/timedstate/TimedState.java +++ b/src/main/java/li/strolch/model/timedstate/TimedState.java @@ -34,7 +34,7 @@ import li.strolch.model.timevalue.impl.TimeVariable; public class TimedState implements ITimedState, Serializable { private static final long serialVersionUID = 1L; - + private ITimeVariable timeVariable = new TimeVariable(); @Override @@ -64,7 +64,7 @@ public class TimedState implements ITimedState, Serializabl } @Override - public void applyChange(final IValueChange change) { + public > void applyChange(U change) { this.timeVariable.applyChange(change); } @@ -78,4 +78,10 @@ public class TimedState implements ITimedState, Serializabl return this.timeVariable; } + @Override + public ITimedState getCopy() { + TimedState copy = new TimedState<>(); + copy.timeVariable = this.timeVariable.getCopy(); + return copy; + } } diff --git a/src/main/java/li/strolch/model/timevalue/ITimeValue.java b/src/main/java/li/strolch/model/timevalue/ITimeValue.java index fdc0090fa..de87f9190 100644 --- a/src/main/java/li/strolch/model/timevalue/ITimeValue.java +++ b/src/main/java/li/strolch/model/timevalue/ITimeValue.java @@ -36,4 +36,5 @@ public interface ITimeValue extends Comparable> ITimeValue add(final T change); + ITimeValue getCopy(); } diff --git a/src/main/java/li/strolch/model/timevalue/ITimeVariable.java b/src/main/java/li/strolch/model/timevalue/ITimeVariable.java index d453e8f55..0e7347f79 100644 --- a/src/main/java/li/strolch/model/timevalue/ITimeVariable.java +++ b/src/main/java/li/strolch/model/timevalue/ITimeVariable.java @@ -19,8 +19,7 @@ import java.util.Collection; import java.util.SortedSet; /** - * A timed variable storing a ordered sequence of {@link ITimeValue} objects - * modeling a time evolution of a quantity. + * A timed variable storing a ordered sequence of {@link ITimeValue} objects modeling a time evolution of a quantity. * * @author Martin Smock * @@ -41,14 +40,12 @@ public interface ITimeVariable { void setValueAt(final Long time, final T value); /** - * get the latest {@link ITimeValue} whose time field is less or equal to - * the time given + * get the latest {@link ITimeValue} whose time field is less or equal to the time given */ ITimeValue getValueAt(final Long time); /** - * Applies a {@link IValueChange} propagating the change to all future - * values starting from the time of the change. + * Applies a {@link IValueChange} propagating the change to all future values starting from the time of the change. * * @param change * the {@link IValueChange} to be applied @@ -56,8 +53,7 @@ public interface ITimeVariable { void applyChange(final IValueChange change); /** - * Get all {@link ITimeValue} objects whose time field is greater or equal - * to the given time + * Get all {@link ITimeValue} objects whose time field is greater or equal to the given time * * @param time * the time the sequence starts with @@ -66,8 +62,7 @@ public interface ITimeVariable { Collection> getFutureValues(final Long time); /** - * Get all {@link ITimeValue} objects whose time field is strictly smaller - * than the given time + * Get all {@link ITimeValue} objects whose time field is strictly smaller than the given time * * @param time * the time the sequence starts with @@ -81,13 +76,16 @@ public interface ITimeVariable { * @return a defensive copy of the {@link ITimeValue}s */ SortedSet> getValues(); - + /** - * removes {@link ITimeValue} objects from the sequence, where the successor - * matches value. I.e considering a pair of adjacent {@link ITimeValue} - * objects in the sequence which have the same {@link IValue}, the later one - * is removed, since it contains no additional information. + * removes {@link ITimeValue} objects from the sequence, where the successor matches value. I.e considering a pair + * of adjacent {@link ITimeValue} objects in the sequence which have the same {@link IValue}, the later one is + * removed, since it contains no additional information. */ void compact(); + /** + * @return a copy of this time variable + */ + ITimeVariable getCopy(); } diff --git a/src/main/java/li/strolch/model/timevalue/IValue.java b/src/main/java/li/strolch/model/timevalue/IValue.java index 5d8d73f56..83446b94a 100644 --- a/src/main/java/li/strolch/model/timevalue/IValue.java +++ b/src/main/java/li/strolch/model/timevalue/IValue.java @@ -23,7 +23,6 @@ package li.strolch.model.timevalue; * * @param * any object for which a (generalized) add operation can be defined. - * */ public interface IValue { @@ -52,5 +51,4 @@ public interface IValue { * @return a copy of this */ IValue getCopy(); - } diff --git a/src/main/java/li/strolch/model/timevalue/impl/BooleanValue.java b/src/main/java/li/strolch/model/timevalue/impl/BooleanValue.java new file mode 100644 index 000000000..bde39dab3 --- /dev/null +++ b/src/main/java/li/strolch/model/timevalue/impl/BooleanValue.java @@ -0,0 +1,108 @@ +/* + * Copyright 2013 Martin Smock + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package li.strolch.model.timevalue.impl; + +import java.io.Serializable; + +import li.strolch.model.timevalue.ITimeValue; +import li.strolch.model.timevalue.IValue; + +/** + * {@link IValue} implementation to work with Boolean valued {@link ITimeValue} objects + * + * @author Martin Smock + */ +public class BooleanValue implements IValue, Serializable { + + private static final long serialVersionUID = 1L; + + public static final BooleanValue NEUTRAL = new BooleanValue(false); + + private Boolean value; + + public BooleanValue(Boolean value) { + this.value = value; + } + + public BooleanValue(boolean value) { + this.value = Boolean.valueOf(value); + } + + public BooleanValue(String valueAsString) throws NumberFormatException { + this.value = Boolean.parseBoolean(valueAsString); + } + + @Override + public BooleanValue add(Boolean o) { + this.value = o; + return this; + } + + @Override + public boolean matches(IValue other) { + return this.value.equals(other.getValue()); + } + + @Override + public Boolean getValue() { + return this.value; + } + + @Override + public BooleanValue getInverse() { + return new BooleanValue(!getValue()); + } + + @SuppressWarnings("nls") + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("BooleanValue [value="); + sb.append(this.value); + sb.append("]"); + return sb.toString(); + } + + @Override + public BooleanValue getCopy() { + return new BooleanValue(this.value); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((this.value == null) ? 0 : this.value.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + BooleanValue other = (BooleanValue) obj; + if (this.value == null) { + if (other.value != null) + return false; + } else if (!this.value.equals(other.value)) + return false; + return true; + } +} diff --git a/src/main/java/li/strolch/model/timevalue/impl/DoubleValue.java b/src/main/java/li/strolch/model/timevalue/impl/FloatValue.java similarity index 79% rename from src/main/java/li/strolch/model/timevalue/impl/DoubleValue.java rename to src/main/java/li/strolch/model/timevalue/impl/FloatValue.java index 0b61f657c..98209b787 100644 --- a/src/main/java/li/strolch/model/timevalue/impl/DoubleValue.java +++ b/src/main/java/li/strolch/model/timevalue/impl/FloatValue.java @@ -25,36 +25,36 @@ import li.strolch.model.timevalue.IValue; * * @author Martin Smock */ -public class DoubleValue implements IValue, Serializable { +public class FloatValue implements IValue, Serializable { private static final long serialVersionUID = 1L; - public static final DoubleValue NEUTRAL = new DoubleValue(0.0d); + public static final FloatValue NEUTRAL = new FloatValue(0.0d); private Double value; - public DoubleValue(Double value) { + public FloatValue(Double value) { this.value = value; } - public DoubleValue(double value) { + public FloatValue(double value) { this.value = Double.valueOf(value); } - public DoubleValue(Integer value) { + public FloatValue(Integer value) { this.value = this.value.doubleValue(); } - public DoubleValue(int value) { + public FloatValue(int value) { this.value = Integer.valueOf(value).doubleValue(); } - public DoubleValue(String valueAsString) throws NumberFormatException { + public FloatValue(String valueAsString) throws NumberFormatException { this.value = Double.parseDouble(valueAsString); } @Override - public DoubleValue add(Double o) { + public FloatValue add(Double o) { this.value += o; return this; } @@ -80,13 +80,13 @@ public class DoubleValue implements IValue, Serializable { } @Override - public DoubleValue getInverse() { - return new DoubleValue(-getValue()); + public FloatValue getInverse() { + return new FloatValue(-getValue()); } @Override - public DoubleValue getCopy(){ - return new DoubleValue(this.value); + public FloatValue getCopy(){ + return new FloatValue(this.value); } @Override @@ -105,7 +105,7 @@ public class DoubleValue implements IValue, Serializable { return false; if (getClass() != obj.getClass()) return false; - DoubleValue other = (DoubleValue) obj; + FloatValue other = (FloatValue) obj; if (this.value == null) { if (other.value != null) return false; diff --git a/src/main/java/li/strolch/model/timevalue/impl/StringSetValue.java b/src/main/java/li/strolch/model/timevalue/impl/StringSetValue.java index c99eacc26..b754b5c1e 100644 --- a/src/main/java/li/strolch/model/timevalue/impl/StringSetValue.java +++ b/src/main/java/li/strolch/model/timevalue/impl/StringSetValue.java @@ -15,93 +15,103 @@ */ package li.strolch.model.timevalue.impl; +import ch.eitchnet.utils.helper.StringHelper; import java.io.Serializable; import java.util.Collections; import java.util.HashSet; import java.util.Iterator; import java.util.Set; - +import li.strolch.exception.StrolchException; import li.strolch.model.timevalue.ITimeValue; import li.strolch.model.timevalue.IValue; /** * {@link IValue} implementation to work with String valued {@link ITimeValue} objects. Since a java.util.String object * does not define a inverse, a algebraic {@link AString} wrapper is used. - * + * * @author Martin Smock */ public class StringSetValue implements IValue>, Serializable { - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 1L; - private static Set neu = Collections.emptySet(); - public static final IValue> NEUTRAL = new StringSetValue(neu); + private static Set neu = Collections.emptySet(); + public static final IValue> NEUTRAL = new StringSetValue(neu); - private Set aStrings = new HashSet(); + private Set aStrings = new HashSet<>(); - public StringSetValue() { - } + public StringSetValue() { + } - public StringSetValue(final Set aStrings) { - this.aStrings = aStrings; - } + public StringSetValue(final Set aStrings) { + // assert no null values in set + for (AString aString : aStrings) { + if (StringHelper.isEmpty(aString.getString())) { + throw new StrolchException("StringSetValue may not contain null values in set!"); + } + } + this.aStrings = aStrings; + } - @Override - public Set getValue() { - return this.aStrings; - } + @Override + public Set getValue() { + return this.aStrings; + } - @Override - public IValue> add(Set o) { + @Override + public IValue> add(Set o) { - Set toBeAdded = new HashSet(o); + Set toBeAdded = new HashSet<>(o); - for (Iterator iter1 = toBeAdded.iterator(); iter1.hasNext();) { - AString toAdd = iter1.next(); - for (Iterator iter = this.aStrings.iterator(); iter.hasNext();) { - AString aString = iter.next(); - boolean valueMatch = aString.getString().equals(toAdd.getString()); - boolean compensate = (toAdd.isInverse() && !aString.isInverse()) - || (!toAdd.isInverse() && aString.isInverse()); - if (valueMatch && compensate) { - iter.remove(); - iter1.remove(); - } - } - } - this.aStrings.addAll(toBeAdded); - return this; - } + for (Iterator iter1 = toBeAdded.iterator(); iter1.hasNext();) { + AString toAdd = iter1.next(); + if (StringHelper.isEmpty(toAdd.getString())) { + throw new StrolchException("StringSetValue may not contain null values in set!"); + } - @Override - public boolean matches(IValue> other) { - return getValue().equals(other.getValue()); - } + for (Iterator iter = this.aStrings.iterator(); iter.hasNext();) { + AString aString = iter.next(); + boolean valueMatch = aString.getString().equals(toAdd.getString()); + boolean compensate = (toAdd.isInverse() && !aString.isInverse()) + || (!toAdd.isInverse() && aString.isInverse()); + if (valueMatch && compensate) { + iter.remove(); + iter1.remove(); + } + } + } + this.aStrings.addAll(toBeAdded); + return this; + } - @Override - public IValue> getInverse() { - Set inverseSet = new HashSet(); - for (AString as : this.aStrings) { - inverseSet.add(as.getInverse()); - } - StringSetValue inverse = new StringSetValue(); - inverse.aStrings = inverseSet; - return inverse; - } + @Override + public boolean matches(IValue> other) { + return getValue().equals(other.getValue()); + } - @Override - public StringSetValue getCopy() { - return new StringSetValue(this.aStrings); - } + @Override + public StringSetValue getInverse() { + Set inverseSet = new HashSet<>(); + for (AString as : this.aStrings) { + inverseSet.add(as.getInverse()); + } + StringSetValue inverse = new StringSetValue(); + inverse.aStrings = inverseSet; + return inverse; + } - @SuppressWarnings("nls") - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append("StringSetValue [aStrings="); - sb.append(this.aStrings); - sb.append("]"); - return sb.toString(); - } + @Override + public StringSetValue getCopy() { + return new StringSetValue(new HashSet<>(this.aStrings)); + } + @SuppressWarnings("nls") + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("StringSetValue [aStrings="); + sb.append(this.aStrings); + sb.append("]"); + return sb.toString(); + } } diff --git a/src/main/java/li/strolch/model/timevalue/impl/TimeValue.java b/src/main/java/li/strolch/model/timevalue/impl/TimeValue.java index 091b5031e..c400c7ba4 100644 --- a/src/main/java/li/strolch/model/timevalue/impl/TimeValue.java +++ b/src/main/java/li/strolch/model/timevalue/impl/TimeValue.java @@ -69,6 +69,12 @@ public class TimeValue implements ITimeValue, Serializable return this.getTime().compareTo(arg0.getTime()); } + @SuppressWarnings("unchecked") + @Override + public ITimeValue getCopy() { + return new TimeValue(time, (T) value.getCopy()); + } + @SuppressWarnings("nls") @Override public String toString() { @@ -112,5 +118,4 @@ public class TimeValue implements ITimeValue, Serializable return false; return true; } - } diff --git a/src/main/java/li/strolch/model/timevalue/impl/TimeVariable.java b/src/main/java/li/strolch/model/timevalue/impl/TimeVariable.java index 1e963eb1f..07cd0c2ad 100644 --- a/src/main/java/li/strolch/model/timevalue/impl/TimeVariable.java +++ b/src/main/java/li/strolch/model/timevalue/impl/TimeVariable.java @@ -26,7 +26,6 @@ import li.strolch.model.timevalue.ITimeVariable; import li.strolch.model.timevalue.IValue; import li.strolch.model.timevalue.IValueChange; - /** * @author Martin Smock */ @@ -34,7 +33,7 @@ import li.strolch.model.timevalue.IValueChange; public class TimeVariable implements ITimeVariable, Serializable { private static final long serialVersionUID = 1L; - + public SortedSet> container = new TreeSet>(); @Override @@ -71,7 +70,7 @@ public class TimeVariable implements ITimeVariable, Seriali TimeValue picker = new TimeValue(time, null); return new TreeSet>(this.container.headSet(picker)); } - + @Override public SortedSet> getValues() { return new TreeSet>(this.container); @@ -91,9 +90,10 @@ public class TimeVariable implements ITimeVariable, Seriali this.container.add(newValue); } else if (initialValue.getTime().longValue() < change.getTime().longValue()) { ITimeValue newValue = new TimeValue(change.getTime(), initialValue.getValue()); - newValue.add(change.getValue()); + newValue.add(change.getValue()); this.container.add(newValue); } + compact(); } @@ -115,7 +115,15 @@ public class TimeVariable implements ITimeVariable, Seriali predecessor = successor; } } - } + @Override + public ITimeVariable getCopy() { + TimeVariable clone = new TimeVariable<>(); + for (ITimeValue timeValue : this.container) { + clone.container.add(timeValue.getCopy()); + } + + return clone; + } } diff --git a/src/main/java/li/strolch/model/visitor/StrolchElementDeepEqualsVisitor.java b/src/main/java/li/strolch/model/visitor/StrolchElementDeepEqualsVisitor.java index 0c0c2c602..3ca79e0b1 100644 --- a/src/main/java/li/strolch/model/visitor/StrolchElementDeepEqualsVisitor.java +++ b/src/main/java/li/strolch/model/visitor/StrolchElementDeepEqualsVisitor.java @@ -18,7 +18,6 @@ package li.strolch.model.visitor; import java.util.ArrayList; import java.util.List; import java.util.Set; - import li.strolch.model.GroupedParameterizedElement; import li.strolch.model.Locator; import li.strolch.model.Order; @@ -26,123 +25,165 @@ import li.strolch.model.ParameterBag; import li.strolch.model.Resource; import li.strolch.model.StrolchElement; import li.strolch.model.parameter.Parameter; +import li.strolch.model.timedstate.StrolchTimedState; +import li.strolch.model.timevalue.ITimeVariable; /** * @author Robert von Burg - * + * */ public class StrolchElementDeepEqualsVisitor { - private List mismatchedLocators; + private List mismatchedLocators; - public StrolchElementDeepEqualsVisitor() { - this.mismatchedLocators = new ArrayList<>(); - } + public StrolchElementDeepEqualsVisitor() { + this.mismatchedLocators = new ArrayList<>(); + } - /** - * @return the mismatchedLocators - */ - public List getMismatchedLocators() { - return this.mismatchedLocators; - } + /** + * @return the mismatchedLocators + */ + public List getMismatchedLocators() { + return this.mismatchedLocators; + } - public boolean isEqual() { - return this.mismatchedLocators.isEmpty(); - } + public boolean isEqual() { + return this.mismatchedLocators.isEmpty(); + } - protected void deepEquals(StrolchElement srcElement, StrolchElement dstElement) { - if (!srcElement.getName().equals(dstElement.getName())) - this.mismatchedLocators.add(dstElement.getLocator()); - if (!srcElement.getType().equals(dstElement.getType())) - this.mismatchedLocators.add(dstElement.getLocator()); - } + protected void deepEquals(StrolchElement srcElement, StrolchElement dstElement) { + if (!srcElement.getName().equals(dstElement.getName())) { + this.mismatchedLocators.add(dstElement.getLocator()); + } + if (!srcElement.getType().equals(dstElement.getType())) { + this.mismatchedLocators.add(dstElement.getLocator()); + } + } - protected void deepEquals(Order srcOrder, Order dstOrder) { - deepEquals((StrolchElement) srcOrder, (StrolchElement) dstOrder); - if (!srcOrder.getState().equals(dstOrder.getState())) - this.mismatchedLocators.add(dstOrder.getLocator()); - if (!srcOrder.getDate().equals(dstOrder.getDate())) - this.mismatchedLocators.add(dstOrder.getLocator()); + protected void deepEquals(Order srcOrder, Order dstOrder) { + deepEquals((StrolchElement) srcOrder, (StrolchElement) dstOrder); + if (!srcOrder.getState().equals(dstOrder.getState())) { + this.mismatchedLocators.add(dstOrder.getLocator()); + } + if (!srcOrder.getDate().equals(dstOrder.getDate())) { + this.mismatchedLocators.add(dstOrder.getLocator()); + } - deepEquals((GroupedParameterizedElement) srcOrder, (GroupedParameterizedElement) dstOrder); - } + deepEquals((GroupedParameterizedElement) srcOrder, (GroupedParameterizedElement) dstOrder); + } - protected void deepEquals(Resource srcRes, Resource dstRes) { - deepEquals((StrolchElement) srcRes, (StrolchElement) dstRes); - deepEquals((GroupedParameterizedElement) srcRes, (GroupedParameterizedElement) dstRes); - } + protected void deepEquals(Resource srcRes, Resource dstRes) { + deepEquals((StrolchElement) srcRes, (StrolchElement) dstRes); + deepEquals((GroupedParameterizedElement) srcRes, (GroupedParameterizedElement) dstRes); + + Set srcTimedStateKeySet = srcRes.getTimedStateKeySet(); + for (String timedStateKey : srcTimedStateKeySet) { + StrolchTimedState srcTimedState = srcRes.getTimedState(timedStateKey); - protected void deepEquals(GroupedParameterizedElement srcElement, GroupedParameterizedElement dstElement) { - Set srcBagKeySet = srcElement.getParameterBagKeySet(); - for (String bagKey : srcBagKeySet) { - ParameterBag srcBag = srcElement.getParameterBag(bagKey); + if (!dstRes.hasTimedState(timedStateKey)) { + this.mismatchedLocators.add(srcTimedState.getLocator()); + continue; + } - if (!dstElement.hasParameterBag(bagKey)) { - this.mismatchedLocators.add(srcBag.getLocator()); - continue; - } + StrolchTimedState dstTimedState = dstRes.getTimedState(timedStateKey); + deepEquals(srcTimedState, dstTimedState); + } - ParameterBag dstBag = dstElement.getParameterBag(bagKey); - deepEquals(srcBag, dstBag); - } + Set dstTimedStateKeySet = dstRes.getTimedStateKeySet(); + for (String timedStateKey : dstTimedStateKeySet) { + if (!srcRes.hasTimedState(timedStateKey)) { + StrolchTimedState dstTimedState = dstRes.getTimedState(timedStateKey); + this.mismatchedLocators.add(dstTimedState.getLocator()); + } + } + } - Set dstBagKeySet = dstElement.getParameterBagKeySet(); - for (String bagKey : dstBagKeySet) { - if (!srcElement.hasParameterBag(bagKey)) { - ParameterBag dstBag = dstElement.getParameterBag(bagKey); - this.mismatchedLocators.add(dstBag.getLocator()); - } - } - } + protected void deepEquals(GroupedParameterizedElement srcElement, GroupedParameterizedElement dstElement) { + Set srcBagKeySet = srcElement.getParameterBagKeySet(); + for (String bagKey : srcBagKeySet) { + ParameterBag srcBag = srcElement.getParameterBag(bagKey); - protected void deepEquals(ParameterBag srcBag, ParameterBag dstBag) { - deepEquals((StrolchElement) srcBag, (StrolchElement) dstBag); + if (!dstElement.hasParameterBag(bagKey)) { + this.mismatchedLocators.add(srcBag.getLocator()); + continue; + } - Set srcParamKeySet = srcBag.getParameterKeySet(); - for (String paramKey : srcParamKeySet) { - Parameter srcParam = srcBag.getParameter(paramKey); - if (!dstBag.hasParameter(paramKey)) { - this.mismatchedLocators.add(srcParam.getLocator()); - continue; - } + ParameterBag dstBag = dstElement.getParameterBag(bagKey); + deepEquals(srcBag, dstBag); + } - Parameter dstParam = dstBag.getParameter(paramKey); - deepEquals(srcParam, dstParam); - } + Set dstBagKeySet = dstElement.getParameterBagKeySet(); + for (String bagKey : dstBagKeySet) { + if (!srcElement.hasParameterBag(bagKey)) { + ParameterBag dstBag = dstElement.getParameterBag(bagKey); + this.mismatchedLocators.add(dstBag.getLocator()); + } + } + } - Set dstParamKeySet = dstBag.getParameterKeySet(); - for (String paramKey : dstParamKeySet) { - if (!srcBag.hasParameter(paramKey)) { - Parameter dstParam = dstBag.getParameter(paramKey); - this.mismatchedLocators.add(dstParam.getLocator()); - } - } - } + protected void deepEquals(ParameterBag srcBag, ParameterBag dstBag) { + deepEquals((StrolchElement) srcBag, (StrolchElement) dstBag); - protected void deepEquals(Parameter srcParam, Parameter dstParam) { - deepEquals((StrolchElement) srcParam, (StrolchElement) dstParam); - if (!srcParam.getUom().equals(dstParam.getUom())) - this.mismatchedLocators.add(dstParam.getLocator()); - if (!srcParam.getInterpretation().equals(dstParam.getInterpretation())) - this.mismatchedLocators.add(dstParam.getLocator()); - if (srcParam.isHidden() != dstParam.isHidden()) - this.mismatchedLocators.add(dstParam.getLocator()); - if (srcParam.getIndex() != dstParam.getIndex()) - this.mismatchedLocators.add(dstParam.getLocator()); + Set srcParamKeySet = srcBag.getParameterKeySet(); + for (String paramKey : srcParamKeySet) { + Parameter srcParam = srcBag.getParameter(paramKey); + if (!dstBag.hasParameter(paramKey)) { + this.mismatchedLocators.add(srcParam.getLocator()); + continue; + } - if (!srcParam.getValue().equals(dstParam.getValue())) - this.mismatchedLocators.add(dstParam.getLocator()); - } + Parameter dstParam = dstBag.getParameter(paramKey); + deepEquals(srcParam, dstParam); + } - public static boolean isEqual(Order srcOrder, Order dstOrder) { - OrderDeepEqualsVisitor visitor = new OrderDeepEqualsVisitor(srcOrder); - visitor.visit(srcOrder); - return visitor.isEqual(); - } + Set dstParamKeySet = dstBag.getParameterKeySet(); + for (String paramKey : dstParamKeySet) { + if (!srcBag.hasParameter(paramKey)) { + Parameter dstParam = dstBag.getParameter(paramKey); + this.mismatchedLocators.add(dstParam.getLocator()); + } + } + } - public static boolean isEqual(Resource srcRes, Resource dstRes) { - ResourceDeepEqualsVisitor visitor = new ResourceDeepEqualsVisitor(srcRes); - visitor.visit(srcRes); - return visitor.isEqual(); - } + protected void deepEquals(Parameter srcParam, Parameter dstParam) { + deepEquals((StrolchElement) srcParam, (StrolchElement) dstParam); + if (!srcParam.getUom().equals(dstParam.getUom())) { + this.mismatchedLocators.add(dstParam.getLocator()); + } + if (!srcParam.getInterpretation().equals(dstParam.getInterpretation())) { + this.mismatchedLocators.add(dstParam.getLocator()); + } + if (srcParam.isHidden() != dstParam.isHidden()) { + this.mismatchedLocators.add(dstParam.getLocator()); + } + if (srcParam.getIndex() != dstParam.getIndex()) { + this.mismatchedLocators.add(dstParam.getLocator()); + } + + if (!srcParam.getValue().equals(dstParam.getValue())) { + this.mismatchedLocators.add(dstParam.getLocator()); + } + } + + protected void deepEquals(StrolchTimedState srcState, StrolchTimedState dstState) { + deepEquals((StrolchElement) srcState, (StrolchElement) dstState); + final ITimeVariable srcTimeEvolution = srcState.getTimeEvolution(); + final ITimeVariable dstTimeEvolution = dstState.getTimeEvolution(); + + if (!srcTimeEvolution.getValues().equals(dstTimeEvolution.getValues())) { + this.mismatchedLocators.add(dstState.getLocator()); + } + } + + public static boolean isEqual(Order srcOrder, Order dstOrder) { + OrderDeepEqualsVisitor visitor = new OrderDeepEqualsVisitor(srcOrder); + visitor.visit(dstOrder); + return visitor.isEqual(); + } + + public static boolean isEqual(Resource srcRes, Resource dstRes) { + ResourceDeepEqualsVisitor visitor = new ResourceDeepEqualsVisitor(srcRes); + visitor.visit(dstRes); + return visitor.isEqual(); + } } diff --git a/src/test/java/li/strolch/model/ModelTest.java b/src/test/java/li/strolch/model/ModelTest.java index c703c9aca..8046ce698 100644 --- a/src/test/java/li/strolch/model/ModelTest.java +++ b/src/test/java/li/strolch/model/ModelTest.java @@ -30,6 +30,9 @@ import li.strolch.model.parameter.IntegerParameter; import li.strolch.model.parameter.LongParameter; import li.strolch.model.parameter.StringListParameter; import li.strolch.model.parameter.StringParameter; +import li.strolch.model.timedstate.BooleanTimedState; +import li.strolch.model.timevalue.impl.BooleanValue; +import li.strolch.model.timevalue.impl.ValueChange; import li.strolch.model.visitor.OrderDeepEqualsVisitor; import li.strolch.model.visitor.ResourceDeepEqualsVisitor; @@ -38,99 +41,112 @@ import org.junit.Test; @SuppressWarnings("nls") public class ModelTest { - @Test - public void shouldCreateResource() { + @Test + public void shouldCreateResource() { - Resource resource = ModelGenerator.createResource("@res01", "Test resource", "MyType"); - ParameterBag bag = resource.getParameterBag(ModelGenerator.BAG_ID); - validateBag(bag); - } + Resource resource = ModelGenerator.createResource("@res01", "Test resource", "MyType"); + ParameterBag bag = resource.getParameterBag(ModelGenerator.BAG_ID); + validateBag(bag); + } - @Test - public void shouldCreateOrder() { + @Test + public void shouldCreateOrder() { - Order order = ModelGenerator.createOrder("@ord01", "Test Order", "MyType", new Date(), State.OPEN); - ParameterBag bag = order.getParameterBag(ModelGenerator.BAG_ID); - validateBag(bag); - } + Order order = ModelGenerator.createOrder("@ord01", "Test Order", "MyType", new Date(), State.OPEN); + ParameterBag bag = order.getParameterBag(ModelGenerator.BAG_ID); + validateBag(bag); + } - @Test - public void shouldPerformDeepResourceEquals() { - Resource srcRes = ModelGenerator.createResource("@res01", "Test resource", "MyType"); - Resource dstRes = ModelGenerator.createResource("@res01", "Test resource", "MyType"); - ResourceDeepEqualsVisitor visitor = new ResourceDeepEqualsVisitor(srcRes); - visitor.visit(dstRes); - assertTrue("Same Resource should be deep equal!", visitor.isEqual()); - } + @Test + public void shouldPerformDeepResourceEquals() { + Resource srcRes = ModelGenerator.createResource("@res01", "Test resource", "MyType"); + Resource dstRes = ModelGenerator.createResource("@res01", "Test resource", "MyType"); + ResourceDeepEqualsVisitor visitor = new ResourceDeepEqualsVisitor(srcRes); + visitor.visit(dstRes); + assertTrue("Same Resource should be deep equal!", visitor.isEqual()); + } - @Test - public void shouldFailDeepResourceEquals1() { - Resource srcRes = ModelGenerator.createResource("@res01", "Test resource", "MyType"); - Resource dstRes = ModelGenerator.createResource("@res01", "Test resource", "MyType"); - ParameterBag bag = dstRes.getParameterBag(ModelGenerator.BAG_ID); - bag.setName("Bla bla"); - FloatParameter fParam = bag.getParameter(ModelGenerator.PARAM_FLOAT_ID); - fParam.setValue(23434234.234); - fParam.setName("Ohla"); - ResourceDeepEqualsVisitor visitor = new ResourceDeepEqualsVisitor(srcRes); - visitor.visit(dstRes); - assertFalse("Resource should not be same if param is changed!", visitor.isEqual()); - assertEquals("Three changes should be registered", 3, visitor.getMismatchedLocators().size()); - } + @Test + public void shouldFailDeepResourceEquals1() { + Resource srcRes = ModelGenerator.createResource("@res01", "Test resource", "MyType"); + Resource dstRes = ModelGenerator.createResource("@res01", "Test resource", "MyType"); + ParameterBag bag = dstRes.getParameterBag(ModelGenerator.BAG_ID); + bag.setName("Bla bla"); + FloatParameter fParam = bag.getParameter(ModelGenerator.PARAM_FLOAT_ID); + fParam.setValue(23434234.234); + fParam.setName("Ohla"); + ResourceDeepEqualsVisitor visitor = new ResourceDeepEqualsVisitor(srcRes); + visitor.visit(dstRes); + assertFalse("Resource should not be same if param is changed!", visitor.isEqual()); + assertEquals("Three changes should be registered", 3, visitor.getMismatchedLocators().size()); + } - @Test - public void shouldPerformDeepOrderEquals() { - Date date = new Date(); - Order srcOrder = ModelGenerator.createOrder("@ord01", "Test Order", "MyType", date, State.OPEN); - Order dstOrder = ModelGenerator.createOrder("@ord01", "Test Order", "MyType", date, State.OPEN); - OrderDeepEqualsVisitor visitor = new OrderDeepEqualsVisitor(srcOrder); - visitor.visit(dstOrder); - assertTrue("Same Order should be deep equal: " + visitor.getMismatchedLocators(), visitor.isEqual()); - } + @Test + public void shouldFailDeepResourceEquals2() { + Resource srcRes = ModelGenerator.createResource("@res01", "Test resource", "MyType"); + Resource dstRes = ModelGenerator.createResource("@res01", "Test resource", "MyType"); + BooleanTimedState timedState = dstRes.getTimedState(ModelGenerator.STATE_BOOLEAN_ID); + timedState.applyChange(new ValueChange<>(System.currentTimeMillis(), new BooleanValue(Boolean.TRUE))); + timedState.setName("Ohla"); + ResourceDeepEqualsVisitor visitor = new ResourceDeepEqualsVisitor(srcRes); + visitor.visit(dstRes); + assertFalse("Resource should not be same if param is changed!", visitor.isEqual()); + assertEquals("One change should be registered!", 1, visitor.getMismatchedLocators().size()); + } - public static void validateBag(ParameterBag bag) { + @Test + public void shouldPerformDeepOrderEquals() { + Date date = new Date(); + Order srcOrder = ModelGenerator.createOrder("@ord01", "Test Order", "MyType", date, State.OPEN); + Order dstOrder = ModelGenerator.createOrder("@ord01", "Test Order", "MyType", date, State.OPEN); + OrderDeepEqualsVisitor visitor = new OrderDeepEqualsVisitor(srcOrder); + visitor.visit(dstOrder); + assertTrue("Same Order should be deep equal: " + visitor.getMismatchedLocators(), visitor.isEqual()); + } - assertNotNull(bag); + public static void validateBag(ParameterBag bag) { - assertEquals(ModelGenerator.BAG_ID, bag.getId()); - assertEquals(ModelGenerator.BAG_NAME, bag.getName()); - assertEquals(ModelGenerator.BAG_TYPE, bag.getType()); + assertNotNull(bag); - validateParams(bag); - } + assertEquals(ModelGenerator.BAG_ID, bag.getId()); + assertEquals(ModelGenerator.BAG_NAME, bag.getName()); + assertEquals(ModelGenerator.BAG_TYPE, bag.getType()); - public static void validateParams(ParameterBag bag) { + validateParams(bag); + } - BooleanParameter boolParam = bag.getParameter(ModelGenerator.PARAM_BOOLEAN_ID); - assertNotNull("Boolean Param missing with id " + ModelGenerator.PARAM_BOOLEAN_ID, boolParam); - assertEquals(true, boolParam.getValue().booleanValue()); + public static void validateParams(ParameterBag bag) { - FloatParameter floatParam = bag.getParameter(ModelGenerator.PARAM_FLOAT_ID); - assertNotNull("Float Param missing with id " + ModelGenerator.PARAM_FLOAT_ID, floatParam); - assertEquals(44.3, floatParam.getValue().doubleValue(), 0.0001); + BooleanParameter boolParam = bag.getParameter(ModelGenerator.PARAM_BOOLEAN_ID); + assertNotNull("Boolean Param missing with id " + ModelGenerator.PARAM_BOOLEAN_ID, boolParam); + assertEquals(true, boolParam.getValue().booleanValue()); - IntegerParameter integerParam = bag.getParameter(ModelGenerator.PARAM_INTEGER_ID); - assertNotNull("Integer Param missing with id " + ModelGenerator.PARAM_INTEGER_ID, integerParam); - assertEquals(77, integerParam.getValue().intValue()); + FloatParameter floatParam = bag.getParameter(ModelGenerator.PARAM_FLOAT_ID); + assertNotNull("Float Param missing with id " + ModelGenerator.PARAM_FLOAT_ID, floatParam); + assertEquals(44.3, floatParam.getValue().doubleValue(), 0.0001); - LongParameter longParam = bag.getParameter(ModelGenerator.PARAM_LONG_ID); - assertNotNull("Long Param missing with id " + ModelGenerator.PARAM_LONG_ID, longParam); - assertEquals(4453234566L, longParam.getValue().longValue()); + IntegerParameter integerParam = bag.getParameter(ModelGenerator.PARAM_INTEGER_ID); + assertNotNull("Integer Param missing with id " + ModelGenerator.PARAM_INTEGER_ID, integerParam); + assertEquals(77, integerParam.getValue().intValue()); - StringParameter stringParam = bag.getParameter(ModelGenerator.PARAM_STRING_ID); - assertNotNull("String Param missing with id " + ModelGenerator.PARAM_STRING_ID, stringParam); - assertEquals("Strolch", stringParam.getValue()); + LongParameter longParam = bag.getParameter(ModelGenerator.PARAM_LONG_ID); + assertNotNull("Long Param missing with id " + ModelGenerator.PARAM_LONG_ID, longParam); + assertEquals(4453234566L, longParam.getValue().longValue()); - DateParameter dateParam = bag.getParameter(ModelGenerator.PARAM_DATE_ID); - assertNotNull("Date Param missing with id " + ModelGenerator.PARAM_DATE_ID, dateParam); - assertEquals(1354295525628L, dateParam.getValue().getTime()); + StringParameter stringParam = bag.getParameter(ModelGenerator.PARAM_STRING_ID); + assertNotNull("String Param missing with id " + ModelGenerator.PARAM_STRING_ID, stringParam); + assertEquals("Strolch", stringParam.getValue()); - StringListParameter stringListP = bag.getParameter(ModelGenerator.PARAM_LIST_STRING_ID); - assertNotNull("StringList Param missing with id " + ModelGenerator.PARAM_LIST_STRING_ID, stringListP); + DateParameter dateParam = bag.getParameter(ModelGenerator.PARAM_DATE_ID); + assertNotNull("Date Param missing with id " + ModelGenerator.PARAM_DATE_ID, dateParam); + assertEquals(1354295525628L, dateParam.getValue().getTime()); - ArrayList stringList = new ArrayList(); - stringList.add("Hello"); - stringList.add("World"); - assertEquals(stringList, stringListP.getValue()); - } + StringListParameter stringListP = bag.getParameter(ModelGenerator.PARAM_LIST_STRING_ID); + assertNotNull("StringList Param missing with id " + ModelGenerator.PARAM_LIST_STRING_ID, stringListP); + + ArrayList stringList = new ArrayList<>(); + stringList.add("Hello"); + stringList.add("World"); + assertEquals(stringList, stringListP.getValue()); + } } diff --git a/src/test/java/li/strolch/model/XmlToDomTest.java b/src/test/java/li/strolch/model/XmlToDomTest.java index 76e543665..315e17065 100644 --- a/src/test/java/li/strolch/model/XmlToDomTest.java +++ b/src/test/java/li/strolch/model/XmlToDomTest.java @@ -15,51 +15,55 @@ */ package li.strolch.model; -import static org.junit.Assert.assertTrue; -import li.strolch.model.visitor.StrolchElementDeepEqualsVisitor; +import li.strolch.model.visitor.OrderDeepEqualsVisitor; +import li.strolch.model.visitor.ResourceDeepEqualsVisitor; import li.strolch.model.xml.OrderToDomVisitor; import li.strolch.model.xml.ResourceToDomVisitor; - +import static org.junit.Assert.assertTrue; import org.junit.Test; import org.w3c.dom.Document; import org.w3c.dom.Element; /** * @author Robert von Burg - * + * */ @SuppressWarnings("nls") public class XmlToDomTest extends ModelTest { - @Test - public void shouldFormatAndParseOrder() { + @Test + public void shouldFormatAndParseOrder() { - Order order = ModelGenerator.createOrder("@1", "My Order 1", "MyOrder"); + Order order = ModelGenerator.createOrder("@1", "My Order 1", "MyOrder"); - OrderToDomVisitor domVisitor = new OrderToDomVisitor(); - domVisitor.visit(order); - Document document = domVisitor.getDocument(); + OrderToDomVisitor domVisitor = new OrderToDomVisitor(); + domVisitor.visit(order); + Document document = domVisitor.getDocument(); - Element rootElement = document.getDocumentElement(); - Order parsedOrder = new Order(rootElement); + Element rootElement = document.getDocumentElement(); + Order parsedOrder = new Order(rootElement); - assertTrue("To DOM and back should equal same Order!", - StrolchElementDeepEqualsVisitor.isEqual(order, parsedOrder)); - } + OrderDeepEqualsVisitor visitor = new OrderDeepEqualsVisitor(order); + visitor.visit(parsedOrder); + assertTrue("To DOM and back should equal same Order:\n" + visitor.getMismatchedLocators(), + visitor.isEqual()); + } - @Test - public void shouldFormatAndParseResource() { + @Test + public void shouldFormatAndParseResource() { - Resource resource = ModelGenerator.createResource("@1", "My Resource 1", "MyResource"); + Resource resource = ModelGenerator.createResource("@1", "My Resource 1", "MyResource"); - ResourceToDomVisitor domVisitor = new ResourceToDomVisitor(); - domVisitor.visit(resource); - Document document = domVisitor.getDocument(); + ResourceToDomVisitor domVisitor = new ResourceToDomVisitor(); + domVisitor.visit(resource); + Document document = domVisitor.getDocument(); - Element rootElement = document.getDocumentElement(); - Resource parsedResource = new Resource(rootElement); + Element rootElement = document.getDocumentElement(); + Resource parsedResource = new Resource(rootElement); - assertTrue("To DOM and back should equal same Resource!", - StrolchElementDeepEqualsVisitor.isEqual(resource, parsedResource)); - } + ResourceDeepEqualsVisitor visitor = new ResourceDeepEqualsVisitor(resource); + visitor.visit(parsedResource); + assertTrue("To DOM and back should equal same Resource:\n" + visitor.getMismatchedLocators(), + visitor.isEqual()); + } } diff --git a/src/test/java/li/strolch/model/timedstate/StrolchTimedStateTest.java b/src/test/java/li/strolch/model/timedstate/StrolchTimedStateTest.java new file mode 100644 index 000000000..6d10aa4cb --- /dev/null +++ b/src/test/java/li/strolch/model/timedstate/StrolchTimedStateTest.java @@ -0,0 +1,117 @@ +/* + * Copyright 2013 Robert von Burg + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package li.strolch.model.timedstate; + +import java.util.HashSet; +import java.util.Set; +import li.strolch.model.ModelGenerator; +import li.strolch.model.Resource; +import li.strolch.model.timevalue.ITimeValue; +import li.strolch.model.timevalue.impl.AString; +import li.strolch.model.timevalue.impl.BooleanValue; +import li.strolch.model.timevalue.impl.FloatValue; +import li.strolch.model.timevalue.impl.IntegerValue; +import li.strolch.model.timevalue.impl.StringSetValue; +import static org.junit.Assert.assertEquals; +import org.junit.Test; + +/** + * @author Robert von Burg + */ +public class StrolchTimedStateTest { + + @Test + public void testFloatState() { + + Resource myRes = ModelGenerator.createResource("@1", "Test With States", "Stated"); + + FloatTimedState floatState = myRes.getTimedState(ModelGenerator.STATE_FLOAT_ID); + ITimeValue valueAt0 = floatState.getTimeEvolution().getValueAt(ModelGenerator.STATE_TIME_0); + assertEquals(ModelGenerator.STATE_FLOAT_TIME_0, valueAt0.getValue().getValue()); + + ITimeValue valueAt10 = floatState.getTimeEvolution().getValueAt(ModelGenerator.STATE_TIME_10); + assertEquals(ModelGenerator.STATE_FLOAT_TIME_10, valueAt10.getValue().getValue()); + + ITimeValue valueAt20 = floatState.getTimeEvolution().getValueAt(ModelGenerator.STATE_TIME_20); + assertEquals(ModelGenerator.STATE_FLOAT_TIME_20, valueAt20.getValue().getValue()); + + ITimeValue valueAt30 = floatState.getTimeEvolution().getValueAt(ModelGenerator.STATE_TIME_30); + assertEquals(ModelGenerator.STATE_FLOAT_TIME_30, valueAt30.getValue().getValue()); + } + + @Test + public void testIntegerState() { + + Resource myRes = ModelGenerator.createResource("@1", "Test With States", "Stated"); + + IntegerTimedState integerState = myRes.getTimedState(ModelGenerator.STATE_INTEGER_ID); + ITimeValue valueAt0 = integerState.getTimeEvolution().getValueAt(ModelGenerator.STATE_TIME_0); + assertEquals(ModelGenerator.STATE_INTEGER_TIME_0, valueAt0.getValue().getValue()); + + ITimeValue valueAt10 = integerState.getTimeEvolution().getValueAt(ModelGenerator.STATE_TIME_10); + assertEquals(ModelGenerator.STATE_INTEGER_TIME_10, valueAt10.getValue().getValue()); + + ITimeValue valueAt20 = integerState.getTimeEvolution().getValueAt(ModelGenerator.STATE_TIME_20); + assertEquals(ModelGenerator.STATE_INTEGER_TIME_20, valueAt20.getValue().getValue()); + + ITimeValue valueAt30 = integerState.getTimeEvolution().getValueAt(ModelGenerator.STATE_TIME_30); + assertEquals(ModelGenerator.STATE_INTEGER_TIME_30, valueAt30.getValue().getValue()); + } + + @Test + public void testBooleanState() { + + Resource myRes = ModelGenerator.createResource("@1", "Test With States", "Stated"); + + BooleanTimedState booleanState = myRes.getTimedState(ModelGenerator.STATE_BOOLEAN_ID); + ITimeValue valueAt0 = booleanState.getTimeEvolution().getValueAt(ModelGenerator.STATE_TIME_0); + assertEquals(ModelGenerator.STATE_BOOLEAN_TIME_0, valueAt0.getValue().getValue()); + + ITimeValue valueAt10 = booleanState.getTimeEvolution().getValueAt(ModelGenerator.STATE_TIME_10); + assertEquals(ModelGenerator.STATE_BOOLEAN_TIME_10, valueAt10.getValue().getValue()); + + ITimeValue valueAt20 = booleanState.getTimeEvolution().getValueAt(ModelGenerator.STATE_TIME_20); + assertEquals(ModelGenerator.STATE_BOOLEAN_TIME_20, valueAt20.getValue().getValue()); + + ITimeValue valueAt30 = booleanState.getTimeEvolution().getValueAt(ModelGenerator.STATE_TIME_30); + assertEquals(ModelGenerator.STATE_BOOLEAN_TIME_30, valueAt30.getValue().getValue()); + } + + @Test + public void testStringSetState() { + + Resource myRes = ModelGenerator.createResource("@1", "Test With States", "Stated"); + + StringSetTimedState booleanState = myRes.getTimedState(ModelGenerator.STATE_STRING_ID); + ITimeValue valueAt0 = booleanState.getTimeEvolution().getValueAt(ModelGenerator.STATE_TIME_0); + assertEquals(asSet(ModelGenerator.STATE_STRING_TIME_0), valueAt0.getValue().getValue()); + + ITimeValue valueAt10 = booleanState.getTimeEvolution().getValueAt(ModelGenerator.STATE_TIME_10); + assertEquals(asSet(ModelGenerator.STATE_STRING_TIME_10), valueAt10.getValue().getValue()); + + ITimeValue valueAt20 = booleanState.getTimeEvolution().getValueAt(ModelGenerator.STATE_TIME_20); + assertEquals(asSet(ModelGenerator.STATE_STRING_TIME_20), valueAt20.getValue().getValue()); + + ITimeValue valueAt30 = booleanState.getTimeEvolution().getValueAt(ModelGenerator.STATE_TIME_30); + assertEquals(asSet(ModelGenerator.STATE_STRING_TIME_30), valueAt30.getValue().getValue()); + } + + private static Set asSet(String value) { + HashSet hashSet = new HashSet<>(); + hashSet.add(new AString(value)); + return hashSet; + } +} diff --git a/src/test/java/li/strolch/model/timedstate/TimeStateTest.java b/src/test/java/li/strolch/model/timedstate/TimeStateTest.java index d91d9efa0..2654fb216 100644 --- a/src/test/java/li/strolch/model/timedstate/TimeStateTest.java +++ b/src/test/java/li/strolch/model/timedstate/TimeStateTest.java @@ -20,7 +20,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import li.strolch.model.timevalue.ITimeValue; import li.strolch.model.timevalue.IValueChange; -import li.strolch.model.timevalue.impl.DoubleValue; +import li.strolch.model.timevalue.impl.FloatValue; import li.strolch.model.timevalue.impl.ValueChange; import org.junit.Before; @@ -28,10 +28,10 @@ import org.junit.Test; public class TimeStateTest { - private ITimedState state = new TimedState(); + private ITimedState state = new TimedState(); - final DoubleValue expectedValue1 = new DoubleValue(Double.valueOf(100D)); - final DoubleValue expectedValue2 = new DoubleValue(Double.valueOf(200D)); + final FloatValue expectedValue1 = new FloatValue(Double.valueOf(100D)); + final FloatValue expectedValue2 = new FloatValue(Double.valueOf(200D)); final Long t0 = Long.valueOf(0); final Long t10 = Long.valueOf(10); @@ -42,20 +42,20 @@ public class TimeStateTest { @Before public void before() { - final IValueChange change1 = new ValueChange(this.t10, this.expectedValue1); + final IValueChange change1 = new ValueChange(this.t10, this.expectedValue1); this.state.applyChange(change1); - final ITimeValue stateAt9 = this.state.getStateAt(9L); + final ITimeValue stateAt9 = this.state.getStateAt(9L); assertNull(stateAt9); - final ITimeValue stateAt11 = this.state.getStateAt(11L); + final ITimeValue stateAt11 = this.state.getStateAt(11L); assertNotNull(stateAt11); assertEquals(true, stateAt11.getValue().matches(this.expectedValue1)); - final IValueChange change2 = new ValueChange(this.t30, this.expectedValue1); + final IValueChange change2 = new ValueChange(this.t30, this.expectedValue1); this.state.applyChange(change2); - final ITimeValue stateAt31 = this.state.getStateAt(31L); + final ITimeValue stateAt31 = this.state.getStateAt(31L); assertNotNull(stateAt31); assertEquals(true, stateAt31.getValue().matches(this.expectedValue2)); } @@ -63,7 +63,7 @@ public class TimeStateTest { @Test public void testGetNextMatch() { - ITimeValue nextMatch = this.state.getNextMatch(this.t0, this.expectedValue1); + ITimeValue nextMatch = this.state.getNextMatch(this.t0, this.expectedValue1); assertNotNull(nextMatch); assertEquals(this.t10, nextMatch.getTime()); @@ -89,7 +89,7 @@ public class TimeStateTest { @Test public void testGetPreviousMatch() { - ITimeValue previousMatch = this.state.getPreviousMatch(this.t100, this.expectedValue2); + ITimeValue previousMatch = this.state.getPreviousMatch(this.t100, this.expectedValue2); assertNotNull(previousMatch); assertEquals(this.t30, previousMatch.getTime()); diff --git a/src/test/java/li/strolch/model/timevalue/FloatTimeVariableTest.java b/src/test/java/li/strolch/model/timevalue/FloatTimeVariableTest.java index 0f788d2ca..3b3f218a9 100644 --- a/src/test/java/li/strolch/model/timevalue/FloatTimeVariableTest.java +++ b/src/test/java/li/strolch/model/timevalue/FloatTimeVariableTest.java @@ -22,7 +22,7 @@ import static org.junit.Assert.assertTrue; import java.util.Collection; import java.util.SortedSet; -import li.strolch.model.timevalue.impl.DoubleValue; +import li.strolch.model.timevalue.impl.FloatValue; import li.strolch.model.timevalue.impl.TimeVariable; import li.strolch.model.timevalue.impl.ValueChange; @@ -35,22 +35,22 @@ public class FloatTimeVariableTest { private static final Long STEP = 10L; private static final Long PICK = 50L; - private TimeVariable timeVariable; + private TimeVariable timeVariable; /** * set the values ascending with a difference of STEP */ @Before public void init() { - this.timeVariable = new TimeVariable(); + this.timeVariable = new TimeVariable(); for (long i = 0; i < MAX; i += STEP) { - this.timeVariable.setValueAt(Long.valueOf(i), new DoubleValue(i)); + this.timeVariable.setValueAt(Long.valueOf(i), new FloatValue(i)); } } @Test public void testGetValueAt() { - ITimeValue valueAt = this.timeVariable.getValueAt(PICK); + ITimeValue valueAt = this.timeVariable.getValueAt(PICK); assertEquals(PICK.doubleValue(), valueAt.getValue().getValue(), 0.0001); } @@ -59,12 +59,12 @@ public class FloatTimeVariableTest { */ @Test public void testGetFutureValues() { - Collection> futureValues = this.timeVariable.getFutureValues(PICK); + Collection> futureValues = this.timeVariable.getFutureValues(PICK); Long expectedTime = PICK; Double expectedValue = PICK.doubleValue(); - for (ITimeValue value : futureValues) { + for (ITimeValue value : futureValues) { assertEquals(expectedTime, value.getTime()); - assertTrue(value.getValue().matches(new DoubleValue(expectedValue))); + assertTrue(value.getValue().matches(new FloatValue(expectedValue))); expectedTime += STEP; expectedValue += STEP.doubleValue(); } @@ -76,12 +76,12 @@ public class FloatTimeVariableTest { */ @Test public void testGetPastValues() { - Collection> pastValues = this.timeVariable.getPastValues(MAX); + Collection> pastValues = this.timeVariable.getPastValues(MAX); Long expectedTime = 0L; Double expectedValue = expectedTime.doubleValue(); - for (ITimeValue value : pastValues) { + for (ITimeValue value : pastValues) { assertEquals(expectedTime, value.getTime()); - assertTrue(value.getValue().matches(new DoubleValue(expectedValue))); + assertTrue(value.getValue().matches(new FloatValue(expectedValue))); expectedTime += STEP; expectedValue += STEP.doubleValue(); } @@ -93,17 +93,17 @@ public class FloatTimeVariableTest { @Test public void testApplyChange() { - DoubleValue doubleValue = new DoubleValue(STEP.doubleValue()); + FloatValue doubleValue = new FloatValue(STEP.doubleValue()); - IValueChange change = new ValueChange(PICK, doubleValue); + IValueChange change = new ValueChange(PICK, doubleValue); this.timeVariable.applyChange(change); - Collection> futureValues = this.timeVariable.getFutureValues(PICK); + Collection> futureValues = this.timeVariable.getFutureValues(PICK); Long expectedTime = PICK; - IValue expectedValue = new DoubleValue(PICK.doubleValue() + change.getValue().getValue()); + IValue expectedValue = new FloatValue(PICK.doubleValue() + change.getValue().getValue()); - for (ITimeValue value : futureValues) { + for (ITimeValue value : futureValues) { assertEquals(expectedTime, value.getTime()); assertTrue(expectedValue.matches(value.getValue())); expectedTime += STEP; @@ -117,17 +117,17 @@ public class FloatTimeVariableTest { @Test public void testApply2Change() { - this.timeVariable = new TimeVariable(); + this.timeVariable = new TimeVariable(); - DoubleValue doubleValue = new DoubleValue(STEP.doubleValue()); + FloatValue doubleValue = new FloatValue(STEP.doubleValue()); - IValueChange change = new ValueChange(PICK, doubleValue); + IValueChange change = new ValueChange(PICK, doubleValue); this.timeVariable.applyChange(change); - ITimeValue actual = this.timeVariable.getValueAt(PICK); + ITimeValue actual = this.timeVariable.getValueAt(PICK); assertNotNull(actual); - IValue expectedValue = new DoubleValue(STEP.doubleValue()); + IValue expectedValue = new FloatValue(STEP.doubleValue()); assertEquals(true, actual.getValue().matches(expectedValue)); } @@ -138,14 +138,14 @@ public class FloatTimeVariableTest { @Test public void testCompact() { - this.timeVariable = new TimeVariable(); + this.timeVariable = new TimeVariable(); for (Long i = 0L; i < MAX; i += STEP) { - this.timeVariable.setValueAt(i, new DoubleValue(STEP.doubleValue())); + this.timeVariable.setValueAt(i, new FloatValue(STEP.doubleValue())); } // call this.timeVariable.compact(); // check - SortedSet> futureValues = this.timeVariable.getFutureValues(0L); + SortedSet> futureValues = this.timeVariable.getFutureValues(0L); assertEquals(1, futureValues.size()); } diff --git a/src/test/java/li/strolch/model/timevalue/ValueTests.java b/src/test/java/li/strolch/model/timevalue/ValueTests.java index ed0991a3d..0be352166 100644 --- a/src/test/java/li/strolch/model/timevalue/ValueTests.java +++ b/src/test/java/li/strolch/model/timevalue/ValueTests.java @@ -21,7 +21,7 @@ import java.util.HashSet; import java.util.Set; import li.strolch.model.timevalue.impl.AString; -import li.strolch.model.timevalue.impl.DoubleValue; +import li.strolch.model.timevalue.impl.FloatValue; import li.strolch.model.timevalue.impl.IntegerValue; import li.strolch.model.timevalue.impl.StringSetValue; @@ -34,8 +34,8 @@ public class ValueTests { */ @Test public void testDoubleInverse() { - DoubleValue value = new DoubleValue(10.0d); - DoubleValue inverse = value.getInverse(); + FloatValue value = new FloatValue(10.0d); + FloatValue inverse = value.getInverse(); assertEquals(Double.valueOf(-10.0d), inverse.getValue()); assertEquals(Double.valueOf(0), value.add(inverse.getValue()).getValue()); }