[New] TimeVariable returns NavigableSet instead of Collection
This commit is contained in:
parent
e3414fdfaa
commit
f5ce31efe1
|
@ -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<T extends IValue> implements ITimedState<T>, Serializabl
|
|||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public ITimeValue<T> getNextMatch(final Long time, final T value) {
|
||||
Collection<ITimeValue<T>> futureValues = this.timeVariable.getFutureValues(time);
|
||||
for (ITimeValue<T> 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<T> getPreviousMatch(final Long time, final T value) {
|
||||
Collection<ITimeValue<T>> pastValues = this.timeVariable.getPastValues(time);
|
||||
List<ITimeValue<T>> asList = new ArrayList<>(pastValues);
|
||||
Collections.reverse(asList);
|
||||
for (ITimeValue<T> iTimeValue : asList) {
|
||||
if (iTimeValue.getValue().matches(value)) {
|
||||
return iTimeValue;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
NavigableSet<ITimeValue<T>> pastValues = this.timeVariable.getPastValues(time);
|
||||
return pastValues.descendingSet().stream() //
|
||||
.filter(v -> v.getValue().matches(value)) //
|
||||
.findFirst().orElse(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -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<T extends IValue> {
|
|||
* @param time
|
||||
* the time the sequence starts with
|
||||
*
|
||||
* <b>Note:</b> The returned result is unmodifiable
|
||||
*
|
||||
* @return the sequence of {@link ITimeValue} objects in the future
|
||||
*/
|
||||
Collection<ITimeValue<T>> getFutureValues(long time);
|
||||
NavigableSet<ITimeValue<T>> 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<T extends IValue> {
|
|||
*
|
||||
* @return the sequence of {@link ITimeValue} objects removed
|
||||
*/
|
||||
Collection<ITimeValue<T>> removeFutureValues(long time);
|
||||
NavigableSet<ITimeValue<T>> 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<T extends IValue> {
|
|||
* @param time
|
||||
* the time the sequence starts with
|
||||
*
|
||||
* <b>Note:</b> The returned result is unmodifiable
|
||||
*
|
||||
* @return the sequence of {@link ITimeValue} objects in the future
|
||||
*/
|
||||
Collection<ITimeValue<T>> getPastValues(long time);
|
||||
NavigableSet<ITimeValue<T>> 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<T extends IValue> {
|
|||
*
|
||||
* @return the sequence of {@link ITimeValue} objects removed
|
||||
*/
|
||||
Collection<ITimeValue<T>> removePastValues(long time);
|
||||
NavigableSet<ITimeValue<T>> removePastValues(long time);
|
||||
|
||||
/**
|
||||
* Get all {@link ITimeValue} objects
|
||||
* Returns all {@link ITimeValue} objects in an unmodifiable {@link NavigableSet}
|
||||
*
|
||||
* <b>Note:</b> The returned result is unmodifiable
|
||||
*
|
||||
* @return a defensive copy of the {@link ITimeValue}s
|
||||
*/
|
||||
SortedSet<ITimeValue<T>> getValues();
|
||||
NavigableSet<ITimeValue<T>> getValues();
|
||||
|
||||
/**
|
||||
* Returns a {@link Stream} over all {@link ITimeValue} objects
|
||||
|
@ -132,11 +137,11 @@ public interface ITimeVariable<T extends IValue> {
|
|||
*
|
||||
* @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 <code>getClone()</code> on the
|
||||
* parent
|
||||
*/
|
||||
public void setReadonly();
|
||||
void setReadonly();
|
||||
}
|
||||
|
|
|
@ -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<T extends IValue> implements ITimeVariable<T>, Serializable {
|
||||
|
||||
public SortedSet<ITimeValue<T>> container = new TreeSet<>();
|
||||
public NavigableSet<ITimeValue<T>> container = new TreeSet<>();
|
||||
private boolean readonly;
|
||||
|
||||
@Override
|
||||
|
@ -62,12 +61,12 @@ public class TimeVariable<T extends IValue> implements ITimeVariable<T>, Seriali
|
|||
}
|
||||
|
||||
@Override
|
||||
public SortedSet<ITimeValue<T>> getFutureValues(long time) {
|
||||
public NavigableSet<ITimeValue<T>> getFutureValues(long time) {
|
||||
return new TreeSet<>(this.container.tailSet(new TimeValue<>(time, null)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<ITimeValue<T>> removeFutureValues(long time) {
|
||||
public NavigableSet<ITimeValue<T>> removeFutureValues(long time) {
|
||||
assertNotReadonly();
|
||||
SortedSet<ITimeValue<T>> values = this.container.tailSet(new TimeValue<>(time, null));
|
||||
TreeSet<ITimeValue<T>> result = new TreeSet<>(values);
|
||||
|
@ -76,12 +75,12 @@ public class TimeVariable<T extends IValue> implements ITimeVariable<T>, Seriali
|
|||
}
|
||||
|
||||
@Override
|
||||
public Collection<ITimeValue<T>> getPastValues(long time) {
|
||||
public NavigableSet<ITimeValue<T>> getPastValues(long time) {
|
||||
return new TreeSet<>(this.container.headSet(new TimeValue<>(time, null)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<ITimeValue<T>> removePastValues(long time) {
|
||||
public NavigableSet<ITimeValue<T>> removePastValues(long time) {
|
||||
assertNotReadonly();
|
||||
SortedSet<ITimeValue<T>> values = this.container.headSet(new TimeValue<>(time, null));
|
||||
TreeSet<ITimeValue<T>> result = new TreeSet<>(values);
|
||||
|
@ -90,8 +89,8 @@ public class TimeVariable<T extends IValue> implements ITimeVariable<T>, Seriali
|
|||
}
|
||||
|
||||
@Override
|
||||
public SortedSet<ITimeValue<T>> getValues() {
|
||||
return new TreeSet<>(this.container);
|
||||
public NavigableSet<ITimeValue<T>> getValues() {
|
||||
return unmodifiableNavigableSet(this.container);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -103,7 +102,7 @@ public class TimeVariable<T extends IValue> implements ITimeVariable<T>, Seriali
|
|||
public void applyChange(final IValueChange<T> change, boolean compact) {
|
||||
assertNotReadonly();
|
||||
|
||||
SortedSet<ITimeValue<T>> futureValues = getFutureValues(change.getTime());
|
||||
NavigableSet<ITimeValue<T>> futureValues = getFutureValues(change.getTime());
|
||||
for (ITimeValue<T> value : futureValues) {
|
||||
value.add(change.getValue());
|
||||
}
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
/*
|
||||
* Copyright 2013 Martin Smock <smock.martin@gmail.com>
|
||||
*
|
||||
*
|
||||
* 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<FloatValue> state = new TimedState<>();
|
||||
private final ITimedState<FloatValue> 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<FloatValue> stateAt11 = this.state.getStateAt(11L);
|
||||
assertNotNull(stateAt11);
|
||||
assertEquals(true, stateAt11.getValue().matches(this.expectedValue1));
|
||||
assertTrue(stateAt11.getValue().matches(this.expectedValue1));
|
||||
|
||||
final IValueChange<FloatValue> change2 = new ValueChange<>(this.t30, this.expectedValue1);
|
||||
this.state.applyChange(change2, true);
|
||||
|
||||
final ITimeValue<FloatValue> stateAt31 = this.state.getStateAt(31L);
|
||||
assertNotNull(stateAt31);
|
||||
assertEquals(true, stateAt31.getValue().matches(this.expectedValue2));
|
||||
assertTrue(stateAt31.getValue().matches(this.expectedValue2));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
Loading…
Reference in New Issue