[Fix] Fixed NPE in StrolchTimedState.trim()

This commit is contained in:
Robert von Burg 2023-02-17 13:25:58 +01:00
parent 8f71812654
commit 687396f295
Signed by: eitch
GPG Key ID: 75DB9C85C74331F7
2 changed files with 14 additions and 2 deletions

View File

@ -207,8 +207,12 @@ public interface StrolchTimedState<T extends IValue> extends StrolchElement {
ITimeVariable<T> timeEvolution = getTimeEvolution();
long time = timeStamp.toInstant().toEpochMilli();
if (keepLastValue && timeEvolution.getFutureValues(time).isEmpty())
time = timeEvolution.getValueAt(time).getTime();
if (keepLastValue && timeEvolution.getFutureValues(time).isEmpty()) {
ITimeValue<T> valueAt = timeEvolution.getValueAt(time);
if (valueAt == null)
return false;
time = valueAt.getTime();
}
return !timeEvolution.removePastValues(time).isEmpty();
}

View File

@ -200,6 +200,14 @@ public class StrolchTimedStateTest {
assertEquals(2, floatState.getTimeEvolution().getValues().size());
}
@Test
public void testTrimTimedState4() {
ZonedDateTime now = ZonedDateTime.now().truncatedTo(ChronoUnit.DAYS).minusDays(20);
FloatTimedState floatState = new FloatTimedState(STATE_FLOAT_ID, STATE_FLOAT_ID);
assertFalse(floatState.trim(now, true));
assertFalse(floatState.trim(20));
}
private static Set<AString> asSet(String value) {
HashSet<AString> hashSet = new HashSet<>();
hashSet.add(new AString(value));