diff --git a/li.strolch.model/src/main/java/li/strolch/model/timedstate/TimedState.java b/li.strolch.model/src/main/java/li/strolch/model/timedstate/TimedState.java index 16eec0679..6dbc6d1b0 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/timedstate/TimedState.java +++ b/li.strolch.model/src/main/java/li/strolch/model/timedstate/TimedState.java @@ -15,11 +15,11 @@ */ package li.strolch.model.timedstate; +import static java.util.Spliterators.spliteratorUnknownSize; +import static java.util.stream.StreamSupport.stream; + import java.io.Serializable; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.List; +import java.util.NavigableSet; import li.strolch.model.timevalue.ITimeValue; import li.strolch.model.timevalue.ITimeVariable; @@ -38,27 +38,18 @@ public class TimedState implements ITimedState, Serializabl @Override @SuppressWarnings("unchecked") public ITimeValue getNextMatch(final Long time, final T value) { - Collection> futureValues = this.timeVariable.getFutureValues(time); - for (ITimeValue iTimeValue : futureValues) { - if (iTimeValue.getValue().matches(value)) { - return iTimeValue; - } - } - return null; + return this.timeVariable.getFutureValues(time).stream() // + .filter(v -> v.getValue().matches(value)) // + .findFirst().orElse(null); } @Override @SuppressWarnings("unchecked") public ITimeValue getPreviousMatch(final Long time, final T value) { - Collection> pastValues = this.timeVariable.getPastValues(time); - List> asList = new ArrayList<>(pastValues); - Collections.reverse(asList); - for (ITimeValue iTimeValue : asList) { - if (iTimeValue.getValue().matches(value)) { - return iTimeValue; - } - } - return null; + NavigableSet> pastValues = this.timeVariable.getPastValues(time); + return pastValues.descendingSet().stream() // + .filter(v -> v.getValue().matches(value)) // + .findFirst().orElse(null); } @Override 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 590d5ae05..2aec59217 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 @@ -15,8 +15,7 @@ */ package li.strolch.model.timevalue; -import java.util.Collection; -import java.util.SortedSet; +import java.util.NavigableSet; import java.util.stream.Stream; /** @@ -61,9 +60,11 @@ public interface ITimeVariable { * @param time * the time the sequence starts with * + * Note: The returned result is unmodifiable + * * @return the sequence of {@link ITimeValue} objects in the future */ - Collection> getFutureValues(long time); + NavigableSet> getFutureValues(long time); /** * Removes all {@link ITimeValue} objects whose time field is greater or equal to the given time @@ -73,7 +74,7 @@ public interface ITimeVariable { * * @return the sequence of {@link ITimeValue} objects removed */ - Collection> removeFutureValues(long time); + NavigableSet> removeFutureValues(long time); /** * Get all {@link ITimeValue} objects whose time field is strictly smaller than the given time @@ -81,9 +82,11 @@ public interface ITimeVariable { * @param time * the time the sequence starts with * + * Note: The returned result is unmodifiable + * * @return the sequence of {@link ITimeValue} objects in the future */ - Collection> getPastValues(long time); + NavigableSet> getPastValues(long time); /** * Remove all {@link ITimeValue} objects whose time field is strictly smaller than the given time @@ -93,14 +96,16 @@ public interface ITimeVariable { * * @return the sequence of {@link ITimeValue} objects removed */ - Collection> removePastValues(long time); + NavigableSet> removePastValues(long time); /** - * Get all {@link ITimeValue} objects + * Returns all {@link ITimeValue} objects in an unmodifiable {@link NavigableSet} + * + * Note: The returned result is unmodifiable * * @return a defensive copy of the {@link ITimeValue}s */ - SortedSet> getValues(); + NavigableSet> getValues(); /** * Returns a {@link Stream} over all {@link ITimeValue} objects @@ -132,11 +137,11 @@ public interface ITimeVariable { * * @return true if this element is read only */ - public boolean isReadonly(); + boolean isReadonly(); /** * Sets this element to readOnly, so that it may not be modified. To modify it, call getClone() on the * parent */ - public void setReadonly(); + void setReadonly(); } 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 f2b56f4c1..050adbbb4 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 @@ -15,11 +15,10 @@ */ package li.strolch.model.timevalue.impl; +import static java.util.Collections.*; + import java.io.Serializable; -import java.util.Collection; -import java.util.Iterator; -import java.util.SortedSet; -import java.util.TreeSet; +import java.util.*; import java.util.stream.Stream; import li.strolch.exception.StrolchModelException; @@ -34,7 +33,7 @@ import li.strolch.model.timevalue.IValueChange; @SuppressWarnings("rawtypes") public class TimeVariable implements ITimeVariable, Serializable { - public SortedSet> container = new TreeSet<>(); + public NavigableSet> container = new TreeSet<>(); private boolean readonly; @Override @@ -62,12 +61,12 @@ public class TimeVariable implements ITimeVariable, Seriali } @Override - public SortedSet> getFutureValues(long time) { + public NavigableSet> getFutureValues(long time) { return new TreeSet<>(this.container.tailSet(new TimeValue<>(time, null))); } @Override - public Collection> removeFutureValues(long time) { + public NavigableSet> removeFutureValues(long time) { assertNotReadonly(); SortedSet> values = this.container.tailSet(new TimeValue<>(time, null)); TreeSet> result = new TreeSet<>(values); @@ -76,12 +75,12 @@ public class TimeVariable implements ITimeVariable, Seriali } @Override - public Collection> getPastValues(long time) { + public NavigableSet> getPastValues(long time) { return new TreeSet<>(this.container.headSet(new TimeValue<>(time, null))); } @Override - public Collection> removePastValues(long time) { + public NavigableSet> removePastValues(long time) { assertNotReadonly(); SortedSet> values = this.container.headSet(new TimeValue<>(time, null)); TreeSet> result = new TreeSet<>(values); @@ -90,8 +89,8 @@ public class TimeVariable implements ITimeVariable, Seriali } @Override - public SortedSet> getValues() { - return new TreeSet<>(this.container); + public NavigableSet> getValues() { + return unmodifiableNavigableSet(this.container); } @Override @@ -103,7 +102,7 @@ public class TimeVariable implements ITimeVariable, Seriali public void applyChange(final IValueChange change, boolean compact) { assertNotReadonly(); - SortedSet> futureValues = getFutureValues(change.getTime()); + NavigableSet> futureValues = getFutureValues(change.getTime()); for (ITimeValue value : futureValues) { value.add(change.getValue()); } diff --git a/li.strolch.model/src/test/java/li/strolch/model/timedstate/TimeStateTest.java b/li.strolch.model/src/test/java/li/strolch/model/timedstate/TimeStateTest.java index 3543f5f6c..664b6bb6c 100644 --- a/li.strolch.model/src/test/java/li/strolch/model/timedstate/TimeStateTest.java +++ b/li.strolch.model/src/test/java/li/strolch/model/timedstate/TimeStateTest.java @@ -1,12 +1,12 @@ /* * Copyright 2013 Martin Smock - * + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -15,29 +15,27 @@ */ package li.strolch.model.timedstate; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; +import static org.junit.Assert.*; + import li.strolch.model.timevalue.ITimeValue; import li.strolch.model.timevalue.IValueChange; import li.strolch.model.timevalue.impl.FloatValue; import li.strolch.model.timevalue.impl.ValueChange; - import org.junit.Before; import org.junit.Test; public class TimeStateTest { - private ITimedState state = new TimedState<>(); + private final ITimedState state = new TimedState<>(); - final FloatValue expectedValue1 = new FloatValue(Double.valueOf(100D)); - final FloatValue expectedValue2 = new FloatValue(Double.valueOf(200D)); + final FloatValue expectedValue1 = new FloatValue(100D); + final FloatValue expectedValue2 = new FloatValue(200D); - final Long t0 = Long.valueOf(0); - final Long t10 = Long.valueOf(10); - final Long t20 = Long.valueOf(20); - final Long t30 = Long.valueOf(30); - final Long t100 = Long.valueOf(100); + final Long t0 = 0L; + final Long t10 = 10L; + final Long t20 = 20L; + final Long t30 = 30L; + final Long t100 = 100L; @Before public void before() { @@ -50,14 +48,14 @@ public class TimeStateTest { final ITimeValue stateAt11 = this.state.getStateAt(11L); assertNotNull(stateAt11); - assertEquals(true, stateAt11.getValue().matches(this.expectedValue1)); + assertTrue(stateAt11.getValue().matches(this.expectedValue1)); final IValueChange change2 = new ValueChange<>(this.t30, this.expectedValue1); this.state.applyChange(change2, true); final ITimeValue stateAt31 = this.state.getStateAt(31L); assertNotNull(stateAt31); - assertEquals(true, stateAt31.getValue().matches(this.expectedValue2)); + assertTrue(stateAt31.getValue().matches(this.expectedValue2)); } @Test