From a6483704c85af07cdfdb35d863717031103016fd Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Thu, 7 Nov 2019 10:56:55 +0100 Subject: [PATCH] [New] Added removing of future and past values on state variables --- .../model/timevalue/ITimeVariable.java | 28 +++++++++++++--- .../model/timevalue/impl/TimeValue.java | 12 +++---- .../model/timevalue/impl/TimeVariable.java | 33 ++++++++++++++----- 3 files changed, 53 insertions(+), 20 deletions(-) diff --git a/li.strolch.model/src/main/java/li/strolch/model/timevalue/ITimeVariable.java b/li.strolch.model/src/main/java/li/strolch/model/timevalue/ITimeVariable.java index 9526e4b6f..efe8c4b75 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/timevalue/ITimeVariable.java +++ b/li.strolch.model/src/main/java/li/strolch/model/timevalue/ITimeVariable.java @@ -37,12 +37,12 @@ public interface ITimeVariable { * @param value * the {@link IValue} to set */ - void setValueAt(final Long time, final T value); + void setValueAt(long time, final T value); /** * get the latest {@link ITimeValue} whose time field is less or equal to the time given */ - ITimeValue getValueAt(final Long time); + ITimeValue getValueAt(long time); /** * Applies a {@link IValueChange} propagating the change to all future values starting from the time of the change. @@ -62,7 +62,17 @@ public interface ITimeVariable { * * @return the sequence of {@link ITimeValue} objects in the future */ - Collection> getFutureValues(final Long time); + Collection> getFutureValues(long time); + + /** + * Removes all {@link ITimeValue} objects whose time field is greater or equal to the given time + * + * @param time + * the time the sequence starts with + * + * @return the sequence of {@link ITimeValue} objects removed + */ + Collection> removeFutureValues(long time); /** * Get all {@link ITimeValue} objects whose time field is strictly smaller than the given time @@ -72,7 +82,17 @@ public interface ITimeVariable { * * @return the sequence of {@link ITimeValue} objects in the future */ - Collection> getPastValues(final Long time); + Collection> getPastValues(long time); + + /** + * Remove all {@link ITimeValue} objects whose time field is strictly smaller than the given time + * + * @param time + * the time the sequence starts with + * + * @return the sequence of {@link ITimeValue} objects removed + */ + Collection> removePastValues(long time); /** * Get all {@link ITimeValue} objects diff --git a/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/TimeValue.java b/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/TimeValue.java index b5249751f..caa058c99 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/TimeValue.java +++ b/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/TimeValue.java @@ -26,7 +26,7 @@ import li.strolch.model.timevalue.IValue; @SuppressWarnings("rawtypes") public class TimeValue implements ITimeValue, Serializable { - protected final Long time; + protected final long time; protected T value; /** @@ -35,7 +35,7 @@ public class TimeValue implements ITimeValue, Serializable * @param value * the actual value for this value */ - public TimeValue(final Long time, final T value) { + public TimeValue(final long time, final T value) { this.time = time; this.value = value; } @@ -101,7 +101,7 @@ public class TimeValue implements ITimeValue, Serializable public int hashCode() { final int prime = 31; int result = 1; - result = prime * result + ((this.time == null) ? 0 : this.time.hashCode()); + result = prime * result + Long.hashCode(this.time); result = prime * result + ((this.value == null) ? 0 : this.value.hashCode()); return result; } @@ -119,11 +119,7 @@ public class TimeValue implements ITimeValue, Serializable } @SuppressWarnings("unchecked") TimeValue other = (TimeValue) obj; - if (this.time == null) { - if (other.time != null) { - return false; - } - } else if (!this.time.equals(other.time)) { + if (this.time != other.time) { return false; } if (this.value == null) { diff --git a/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/TimeVariable.java b/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/TimeVariable.java index 60c9381e0..09859c40c 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/TimeVariable.java +++ b/li.strolch.model/src/main/java/li/strolch/model/timevalue/impl/TimeVariable.java @@ -37,7 +37,7 @@ public class TimeVariable implements ITimeVariable, Seriali private boolean readonly; @Override - public ITimeValue getValueAt(final Long time) { + public ITimeValue getValueAt(long time) { ITimeValue tmp = null; for (ITimeValue value : this.container) { if (value.getTime() <= time) { @@ -50,7 +50,7 @@ public class TimeVariable implements ITimeVariable, Seriali } @Override - public void setValueAt(final Long time, final T targetValue) { + public void setValueAt(long time, final T targetValue) { assertNotReadonly(); ITimeValue current = getValueAt(time); if (current != null && current.getTime().equals(time)) { @@ -61,15 +61,31 @@ public class TimeVariable implements ITimeVariable, Seriali } @Override - public SortedSet> getFutureValues(final Long time) { - TimeValue picker = new TimeValue<>(time, null); - return new TreeSet<>(this.container.tailSet(picker)); + public SortedSet> getFutureValues(long time) { + return new TreeSet<>(this.container.tailSet(new TimeValue<>(time, null))); } @Override - public Collection> getPastValues(final Long time) { - TimeValue picker = new TimeValue<>(time, null); - return new TreeSet<>(this.container.headSet(picker)); + public Collection> removeFutureValues(long time) { + assertNotReadonly(); + SortedSet> values = this.container.tailSet(new TimeValue<>(time, null)); + TreeSet> result = new TreeSet<>(values); + values.clear(); + return result; + } + + @Override + public Collection> getPastValues(long time) { + return new TreeSet<>(this.container.headSet(new TimeValue<>(time, null))); + } + + @Override + public Collection> removePastValues(long time) { + assertNotReadonly(); + SortedSet> values = this.container.headSet(new TimeValue<>(time, null)); + TreeSet> result = new TreeSet<>(values); + values.clear(); + return result; } @Override @@ -103,6 +119,7 @@ public class TimeVariable implements ITimeVariable, Seriali @SuppressWarnings("unchecked") @Override public void compact() { + assertNotReadonly(); if (this.container.size() < 2) { return;