[New] Added ITimeVariable.equals() contract with tests

This commit is contained in:
Robert von Burg 2022-10-19 10:54:36 +02:00
parent 0cd54539bf
commit 9641dc2fb5
Signed by: eitch
GPG Key ID: 75DB9C85C74331F7
4 changed files with 62 additions and 17 deletions

View File

@ -60,7 +60,7 @@ public interface ITimeVariable<T extends IValue> {
* @param time * @param time
* the time the sequence starts with * the time the sequence starts with
* *
* <b>Note:</b> The returned result is unmodifiable * <b>Note:</b> The returned result is unmodifiable
* *
* @return the sequence of {@link ITimeValue} objects in the future * @return the sequence of {@link ITimeValue} objects in the future
*/ */
@ -82,7 +82,7 @@ public interface ITimeVariable<T extends IValue> {
* @param time * @param time
* the time the sequence starts with * the time the sequence starts with
* *
* <b>Note:</b> The returned result is unmodifiable * <b>Note:</b> The returned result is unmodifiable
* *
* @return the sequence of {@link ITimeValue} objects in the future * @return the sequence of {@link ITimeValue} objects in the future
*/ */
@ -144,4 +144,10 @@ public interface ITimeVariable<T extends IValue> {
* parent * parent
*/ */
void setReadonly(); void setReadonly();
/**
* Returns true if the given {@link ITimeVariable} is equal to this {@link ITimeVariable} by validating that the
* values {@link IValue IValues} have the same time and actual value
*/
boolean equals(ITimeVariable<T> other);
} }

View File

@ -18,10 +18,7 @@ package li.strolch.model.timevalue.impl;
import static java.util.Collections.unmodifiableNavigableSet; import static java.util.Collections.unmodifiableNavigableSet;
import java.io.Serializable; import java.io.Serializable;
import java.util.Iterator; import java.util.*;
import java.util.NavigableSet;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.stream.Stream; import java.util.stream.Stream;
import li.strolch.exception.StrolchModelException; import li.strolch.exception.StrolchModelException;
@ -170,4 +167,36 @@ public class TimeVariable<T extends IValue> implements ITimeVariable<T>, Seriali
+ " is currently readOnly, to modify clone first!"); + " is currently readOnly, to modify clone first!");
} }
} }
@Override
public boolean equals(ITimeVariable<T> o) {
return equals((Object) o);
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
@SuppressWarnings("unchecked")
TimeVariable<T> other = (TimeVariable<T>) o;
if (this.container.size() != other.container.size())
return false;
Iterator<ITimeValue<T>> thisIter = this.container.iterator();
Iterator<ITimeValue<T>> thatIter = other.container.iterator();
while (thisIter.hasNext()) {
ITimeValue<T> thisNext = thisIter.next();
ITimeValue<T> thatNext = thatIter.next();
if (!thisNext.equals(thatNext))
return false;
}
return true;
}
@Override
public int hashCode() {
return Objects.hash(this.container);
}
} }

View File

@ -33,8 +33,8 @@ import li.strolch.model.timevalue.ITimeVariable;
import li.strolch.utils.dbc.DBC; import li.strolch.utils.dbc.DBC;
/** /**
* Visitor of {@link StrolchRootElement} to check if they are equal. This implementations stores a list of {@link * Visitor of {@link StrolchRootElement} to check if they are equal. This implementations stores a list of
* Locator} for every element of an object which is not equal, thus making it easy to find the inconsistencies in * {@link Locator} for every element of an object which is not equal, thus making it easy to find the inconsistencies in
* objects. * objects.
* *
* @author Robert von Burg <eitch@eitchnet.ch> * @author Robert von Burg <eitch@eitchnet.ch>
@ -175,7 +175,7 @@ public class StrolchElementDeepEqualsVisitor implements StrolchElementVisitor<Li
if (srcElement.hasPolicyDefs() && dstElement.hasPolicyDefs()) if (srcElement.hasPolicyDefs() && dstElement.hasPolicyDefs())
deepEquals(srcElement.getPolicyDefs(), dstElement.getPolicyDefs()); deepEquals(srcElement.getPolicyDefs(), dstElement.getPolicyDefs());
else if (srcElement.hasPolicyDefs() != dstElement.hasPolicyDefs()) else if (srcElement.hasPolicyDefs() != dstElement.hasPolicyDefs())
addLocator(srcElement.getPolicyDefs().getLocator()); addLocator(srcElement.getPolicyDefs().getLocator().append(Tags.POLICIES));
} }
private void deepEquals(StrolchElement srcElement, StrolchElement dstElement) { private void deepEquals(StrolchElement srcElement, StrolchElement dstElement) {
@ -252,7 +252,7 @@ public class StrolchElementDeepEqualsVisitor implements StrolchElementVisitor<Li
if (srcAction.hasPolicyDefs() && dstAction.hasPolicyDefs()) if (srcAction.hasPolicyDefs() && dstAction.hasPolicyDefs())
deepEquals(srcAction.getPolicyDefs(), dstAction.getPolicyDefs()); deepEquals(srcAction.getPolicyDefs(), dstAction.getPolicyDefs());
else if (srcAction.hasPolicyDefs() != dstAction.hasPolicyDefs()) else if (srcAction.hasPolicyDefs() != dstAction.hasPolicyDefs())
addLocator(dstAction.getPolicyDefs().getLocator()); addLocator(dstAction.getPolicyDefs().getLocator().append(Tags.POLICIES));
} }
private void deepEquals(GroupedParameterizedElement srcElement, GroupedParameterizedElement dstElement) { private void deepEquals(GroupedParameterizedElement srcElement, GroupedParameterizedElement dstElement) {
@ -305,16 +305,16 @@ public class StrolchElementDeepEqualsVisitor implements StrolchElementVisitor<Li
private void deepEquals(Parameter<?> srcParam, Parameter<?> dstParam) { private void deepEquals(Parameter<?> srcParam, Parameter<?> dstParam) {
deepEquals((StrolchElement) srcParam, (StrolchElement) dstParam); deepEquals((StrolchElement) srcParam, (StrolchElement) dstParam);
if (!srcParam.getUom().equals(dstParam.getUom())) if (!srcParam.getUom().equals(dstParam.getUom()))
addLocator(dstParam.getLocator()); addLocator(dstParam.getLocator().append(Tags.UOM));
if (!srcParam.getInterpretation().equals(dstParam.getInterpretation())) if (!srcParam.getInterpretation().equals(dstParam.getInterpretation()))
addLocator(dstParam.getLocator()); addLocator(dstParam.getLocator().append(Tags.INTERPRETATION));
if (srcParam.isHidden() != dstParam.isHidden()) if (srcParam.isHidden() != dstParam.isHidden())
addLocator(dstParam.getLocator()); addLocator(dstParam.getLocator().append(Tags.HIDDEN));
if (srcParam.getIndex() != dstParam.getIndex()) if (srcParam.getIndex() != dstParam.getIndex())
addLocator(dstParam.getLocator()); addLocator(dstParam.getLocator().append(Tags.INDEX));
if (!srcParam.getValue().equals(dstParam.getValue())) if (!srcParam.getValue().equals(dstParam.getValue()))
addLocator(dstParam.getLocator()); addLocator(dstParam.getLocator().append(Tags.VALUE));
} }
private void deepEquals(StrolchTimedState<?> srcState, StrolchTimedState<?> dstState) { private void deepEquals(StrolchTimedState<?> srcState, StrolchTimedState<?> dstState) {
@ -322,7 +322,7 @@ public class StrolchElementDeepEqualsVisitor implements StrolchElementVisitor<Li
final ITimeVariable<?> srcTimeEvolution = srcState.getTimeEvolution(); final ITimeVariable<?> srcTimeEvolution = srcState.getTimeEvolution();
final ITimeVariable<?> dstTimeEvolution = dstState.getTimeEvolution(); final ITimeVariable<?> dstTimeEvolution = dstState.getTimeEvolution();
if (!srcTimeEvolution.getValues().equals(dstTimeEvolution.getValues())) if (!srcTimeEvolution.equals(dstTimeEvolution))
addLocator(dstState.getLocator().append(Tags.VALUES)); addLocator(dstState.getLocator().append(Tags.VALUES));
} }

View File

@ -274,10 +274,20 @@ public class ModelTest {
FloatParameter fParam = bag.getParameter(PARAM_FLOAT_ID); FloatParameter fParam = bag.getParameter(PARAM_FLOAT_ID);
fParam.setValue(23434234.234); fParam.setValue(23434234.234);
fParam.setName("Ohla"); fParam.setName("Ohla");
FloatTimedState floatState = dstRes.getTimedState(STATE_FLOAT_ID, true);
floatState.setStateFromStringAt(STATE_TIME_30, "3453451.0");
StrolchElementDeepEqualsVisitor visitor = new StrolchElementDeepEqualsVisitor(srcRes); StrolchElementDeepEqualsVisitor visitor = new StrolchElementDeepEqualsVisitor(srcRes);
List<Locator> mismatches = dstRes.accept(visitor); List<Locator> mismatches = dstRes.accept(visitor);
assertFalse("Resource should not be same if param is changed!", mismatches.isEmpty()); assertFalse("Resource should not be same if param is changed!", mismatches.isEmpty());
assertEquals("Multiple changes should be registered", 3, mismatches.size()); assertEquals("Multiple changes should be registered", 4, mismatches.size());
assertTrue("Should contain locator",
mismatches.contains(Locator.valueOf("Resource/MyType/@res01/Bag/@bag01/Name")));
assertTrue("Should contain locator",
mismatches.contains(Locator.valueOf("Resource/MyType/@res01/Bag/@bag01/@param2/Name")));
assertTrue("Should contain locator",
mismatches.contains(Locator.valueOf("Resource/MyType/@res01/Bag/@bag01/@param2/Value")));
assertTrue("Should contain locator",
mismatches.contains(Locator.valueOf("Resource/MyType/@res01/State/@state1/Values")));
} }
@Test @Test