[New] Added StrolchTimedState.trim()
This commit is contained in:
parent
30495b345d
commit
a9e895b2c0
|
@ -15,6 +15,10 @@
|
|||
*/
|
||||
package li.strolch.model.timedstate;
|
||||
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.Iterator;
|
||||
import java.util.NavigableSet;
|
||||
|
||||
import li.strolch.model.Resource;
|
||||
import li.strolch.model.StrolchElement;
|
||||
import li.strolch.model.StrolchModelConstants;
|
||||
|
@ -162,4 +166,50 @@ public interface StrolchTimedState<T extends IValue> extends StrolchElement {
|
|||
StrolchTimedState<T> getClone();
|
||||
|
||||
void clear();
|
||||
|
||||
/**
|
||||
* Trims this timed state, so it has at most the given number of values
|
||||
*
|
||||
* @param maxValues
|
||||
* the number of values to keep
|
||||
*
|
||||
* @return true if the state was trimmed, false if not
|
||||
*/
|
||||
default boolean trim(int maxValues) {
|
||||
assertNotReadonly();
|
||||
|
||||
ITimeVariable<T> timeEvolution = getTimeEvolution();
|
||||
NavigableSet<? extends ITimeValue<?>> values = timeEvolution.getValues();
|
||||
if (values.size() < maxValues)
|
||||
return false;
|
||||
|
||||
Iterator<? extends ITimeValue<?>> iterator = values.descendingIterator();
|
||||
ITimeValue<?> next = iterator.next();
|
||||
for (int i = 0; i < maxValues - 1; i++) {
|
||||
next = iterator.next();
|
||||
}
|
||||
|
||||
return !timeEvolution.removePastValues(next.getTime()).isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Trims this timed state, so all values before the given time stamp
|
||||
*
|
||||
* @param timeStamp
|
||||
* the max date the values may have
|
||||
* @param keepLastValue
|
||||
* if true, and the last value is before the max
|
||||
*
|
||||
* @return true if the state was trimmed, false if not
|
||||
*/
|
||||
default boolean trim(ZonedDateTime timeStamp, boolean keepLastValue) {
|
||||
assertNotReadonly();
|
||||
ITimeVariable<T> timeEvolution = getTimeEvolution();
|
||||
|
||||
long time = timeStamp.toInstant().toEpochMilli();
|
||||
if (keepLastValue && timeEvolution.getFutureValues(time).isEmpty())
|
||||
time = timeEvolution.getValueAt(time).getTime();
|
||||
|
||||
return !timeEvolution.removePastValues(time).isEmpty();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,8 +18,10 @@ package li.strolch.model.timedstate;
|
|||
import static java.util.Arrays.asList;
|
||||
import static java.util.Collections.singletonList;
|
||||
import static li.strolch.model.ModelGenerator.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
|
@ -129,6 +131,75 @@ public class StrolchTimedStateTest {
|
|||
assertEquals(asSet(STATE_STRING_TIME_30), valueAt30.getValue().getValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTrimTimedState1() {
|
||||
|
||||
Resource myRes = createResource("@1", "Test With States", "Stated");
|
||||
FloatTimedState floatState = myRes.getTimedState(STATE_FLOAT_ID);
|
||||
|
||||
boolean trimmed = floatState.trim(20);
|
||||
assertFalse(trimmed);
|
||||
assertEquals(4, floatState.getTimeEvolution().getValues().size());
|
||||
|
||||
long now = ZonedDateTime.now().truncatedTo(ChronoUnit.DAYS).minusDays(20).toInstant().toEpochMilli();
|
||||
|
||||
for (int i = 0; i < 16; i++) {
|
||||
floatState.getTimeEvolution().setValueAt(now, new FloatValue(i));
|
||||
now += 10;
|
||||
}
|
||||
|
||||
assertEquals(20, floatState.getTimeEvolution().getValues().size());
|
||||
trimmed = floatState.trim(20);
|
||||
assertFalse(trimmed);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTrimTimedState2() {
|
||||
|
||||
Resource myRes = createResource("@1", "Test With States", "Stated");
|
||||
FloatTimedState floatState = myRes.getTimedState(STATE_FLOAT_ID);
|
||||
|
||||
assertFalse(floatState.trim(20));
|
||||
assertEquals(4, floatState.getTimeEvolution().getValues().size());
|
||||
|
||||
long now = ZonedDateTime.now().truncatedTo(ChronoUnit.DAYS).minusDays(20).toInstant().toEpochMilli();
|
||||
|
||||
for (int i = 0; i < 50; i++) {
|
||||
floatState.getTimeEvolution().setValueAt(now, new FloatValue(i));
|
||||
now += 10;
|
||||
}
|
||||
|
||||
assertEquals(54, floatState.getTimeEvolution().getValues().size());
|
||||
assertTrue(floatState.trim(20));
|
||||
assertEquals(20, floatState.getTimeEvolution().getValues().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTrimTimedState3() {
|
||||
ZonedDateTime now = ZonedDateTime.now().truncatedTo(ChronoUnit.DAYS).minusDays(20);
|
||||
|
||||
Resource myRes = createResource("@1", "Test With States", "Stated");
|
||||
FloatTimedState floatState = myRes.getTimedState(STATE_FLOAT_ID);
|
||||
assertEquals(4, floatState.getTimeEvolution().getValues().size());
|
||||
|
||||
assertTrue(floatState.trim(now, true));
|
||||
assertEquals(1, floatState.getTimeEvolution().getValues().size());
|
||||
assertEquals(STATE_TIME_30, floatState.getTimeEvolution().getValues().iterator().next().getTime().longValue());
|
||||
|
||||
assertFalse(floatState.trim(now, true));
|
||||
assertEquals(1, floatState.getTimeEvolution().getValues().size());
|
||||
|
||||
assertTrue(floatState.trim(now, false));
|
||||
assertEquals(0, floatState.getTimeEvolution().getValues().size());
|
||||
|
||||
ZonedDateTime later = now.plusDays(1);
|
||||
floatState.setStateFromStringAt(later.toInstant().toEpochMilli(), "55.0");
|
||||
floatState.setStateFromStringAt(now.toInstant().toEpochMilli(), "56.0");
|
||||
assertEquals(2, floatState.getTimeEvolution().getValues().size());
|
||||
assertFalse(floatState.trim(now, true));
|
||||
assertEquals(2, floatState.getTimeEvolution().getValues().size());
|
||||
}
|
||||
|
||||
private static Set<AString> asSet(String value) {
|
||||
HashSet<AString> hashSet = new HashSet<>();
|
||||
hashSet.add(new AString(value));
|
||||
|
|
Loading…
Reference in New Issue