[New] Added removing of future and past values on state variables

This commit is contained in:
Robert von Burg 2019-11-07 10:56:55 +01:00
parent f1d94bb6f5
commit a6483704c8
3 changed files with 53 additions and 20 deletions

View File

@ -37,12 +37,12 @@ public interface ITimeVariable<T extends IValue> {
* @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<T> getValueAt(final Long time);
ITimeValue<T> 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<T extends IValue> {
*
* @return the sequence of {@link ITimeValue} objects in the future
*/
Collection<ITimeValue<T>> getFutureValues(final Long time);
Collection<ITimeValue<T>> 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<ITimeValue<T>> 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<T extends IValue> {
*
* @return the sequence of {@link ITimeValue} objects in the future
*/
Collection<ITimeValue<T>> getPastValues(final Long time);
Collection<ITimeValue<T>> 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<ITimeValue<T>> removePastValues(long time);
/**
* Get all {@link ITimeValue} objects

View File

@ -26,7 +26,7 @@ import li.strolch.model.timevalue.IValue;
@SuppressWarnings("rawtypes")
public class TimeValue<T extends IValue> implements ITimeValue<T>, Serializable {
protected final Long time;
protected final long time;
protected T value;
/**
@ -35,7 +35,7 @@ public class TimeValue<T extends IValue> implements ITimeValue<T>, 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<T extends IValue> implements ITimeValue<T>, 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<T extends IValue> implements ITimeValue<T>, Serializable
}
@SuppressWarnings("unchecked")
TimeValue<T> other = (TimeValue<T>) 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) {

View File

@ -37,7 +37,7 @@ public class TimeVariable<T extends IValue> implements ITimeVariable<T>, Seriali
private boolean readonly;
@Override
public ITimeValue<T> getValueAt(final Long time) {
public ITimeValue<T> getValueAt(long time) {
ITimeValue<T> tmp = null;
for (ITimeValue<T> value : this.container) {
if (value.getTime() <= time) {
@ -50,7 +50,7 @@ public class TimeVariable<T extends IValue> implements ITimeVariable<T>, Seriali
}
@Override
public void setValueAt(final Long time, final T targetValue) {
public void setValueAt(long time, final T targetValue) {
assertNotReadonly();
ITimeValue<T> current = getValueAt(time);
if (current != null && current.getTime().equals(time)) {
@ -61,15 +61,31 @@ public class TimeVariable<T extends IValue> implements ITimeVariable<T>, Seriali
}
@Override
public SortedSet<ITimeValue<T>> getFutureValues(final Long time) {
TimeValue<T> picker = new TimeValue<>(time, null);
return new TreeSet<>(this.container.tailSet(picker));
public SortedSet<ITimeValue<T>> getFutureValues(long time) {
return new TreeSet<>(this.container.tailSet(new TimeValue<>(time, null)));
}
@Override
public Collection<ITimeValue<T>> getPastValues(final Long time) {
TimeValue<T> picker = new TimeValue<>(time, null);
return new TreeSet<>(this.container.headSet(picker));
public Collection<ITimeValue<T>> removeFutureValues(long time) {
assertNotReadonly();
SortedSet<ITimeValue<T>> values = this.container.tailSet(new TimeValue<>(time, null));
TreeSet<ITimeValue<T>> result = new TreeSet<>(values);
values.clear();
return result;
}
@Override
public Collection<ITimeValue<T>> getPastValues(long time) {
return new TreeSet<>(this.container.headSet(new TimeValue<>(time, null)));
}
@Override
public Collection<ITimeValue<T>> removePastValues(long time) {
assertNotReadonly();
SortedSet<ITimeValue<T>> values = this.container.headSet(new TimeValue<>(time, null));
TreeSet<ITimeValue<T>> result = new TreeSet<>(values);
values.clear();
return result;
}
@Override
@ -103,6 +119,7 @@ public class TimeVariable<T extends IValue> implements ITimeVariable<T>, Seriali
@SuppressWarnings("unchecked")
@Override
public void compact() {
assertNotReadonly();
if (this.container.size() < 2) {
return;