Commit aa0e7f6d authored by Luc Maisonobe's avatar Luc Maisonobe
Browse files

Added navigation features based on Span and Transition in TimeSpanMap.

Fixes #883
parent 270f8745
......@@ -299,6 +299,38 @@ public class TimeSpanMap<T> {
}
}
/** Get the first (earliest) transition.
* @return first (earliest) transition, or null if there are no transitions
* @since 11.1
*/
public Transition<T> getFirstTransition() {
return hasSingleDummyTransition() ? null : data.first();
}
/** Get the last (latest) transition.
* @return last (latest) transition, or null if there are no transitions
* @since 11.1
*/
public Transition<T> getLastTransition() {
return hasSingleDummyTransition() ? null : data.last();
}
/** Get the first (earliest) span.
* @return first (earliest) span
* @since 11.1
*/
public Span<T> getFirstSpan() {
return data.first().getSpanBefore();
}
/** Get the last (latest) span.
* @return last (latest) span
* @since 11.1
*/
public Span<T> getLastSpan() {
return data.last().getSpanAfter();
}
/** Extract a range of the map.
* <p>
* The object returned will be a new independent instance that will contain
......@@ -422,6 +454,22 @@ public class TimeSpanMap<T> {
return date;
}
/** Get the previous transition.
* @return previous transition, or null if this transition was the first one
* @since 11.1
*/
public Transition<S> previous() {
return before.getStartTransition();
}
/** Get the next transition.
* @return next transition, or null if this transition was the last one
* @since 11.1
*/
public Transition<S> next() {
return after.getEndTransition();
}
/** Get the entry valid before transition.
* @return entry valid before transition
* @see #getSpanBefore()
......@@ -492,6 +540,22 @@ public class TimeSpanMap<T> {
return data;
}
/** Get the previous time span.
* @return previous time span, or null if this time span was the first one
* @since 11.1
*/
public Span<S> previous() {
return start == null ? null : start.getSpanBefore();
}
/** Get the next time span.
* @return next time span, or null if this time span was the last one
* @since 11.1
*/
public Span<S> next() {
return end == null ? null : end.getSpanAfter();
}
/** Get the start of this time span.
* @return start of this time span (will be {@link AbsoluteDate#PAST_INFINITY}
* if {@link #getStartTransition() returns null)
......
......@@ -22,6 +22,7 @@ import org.junit.Before;
import org.junit.Test;
import org.orekit.Utils;
import org.orekit.time.AbsoluteDate;
import org.orekit.utils.TimeSpanMap.Transition;
public class TimeSpanMapTest {
......@@ -336,6 +337,93 @@ public class TimeSpanMapTest {
}
@Test
public void tesFirstLastEmpty() {
TimeSpanMap<Integer> map = new TimeSpanMap<>(Integer.valueOf(0));
Assert.assertNull(map.getFirstTransition());
Assert.assertNull(map.getLastTransition());
Assert.assertSame(map.getFirstSpan(), map.getLastSpan());
Assert.assertNull(map.getFirstSpan().getStartTransition());
Assert.assertNull(map.getFirstSpan().getEndTransition());
Assert.assertNull(map.getFirstSpan().previous());
Assert.assertNull(map.getLastSpan().next());
}
@Test
public void testSpansNavigation() {
final AbsoluteDate ref = AbsoluteDate.ARBITRARY_EPOCH;
TimeSpanMap<Integer> map = new TimeSpanMap<>(Integer.valueOf(0));
map.addValidAfter(Integer.valueOf(10), ref.shiftedBy(10.0), false);
map.addValidAfter(Integer.valueOf( 3), ref.shiftedBy( 2.0), false);
map.addValidAfter(Integer.valueOf( 9), ref.shiftedBy( 5.0), false);
map.addValidBefore(Integer.valueOf( 2), ref.shiftedBy( 3.0), false);
map.addValidBefore(Integer.valueOf( 5), ref.shiftedBy( 9.0), false);
Assert.assertNull(map.getFirstSpan().previous());
Assert.assertNull(map.getLastSpan().next());
TimeSpanMap.Span<Integer> span = map.getFirstSpan();
Assert.assertEquals(0, span.getData().intValue());
span = span.next();
Assert.assertEquals(2, span.getData().intValue());
span = span.next();
Assert.assertEquals(3, span.getData().intValue());
span = span.next();
Assert.assertEquals(5, span.getData().intValue());
span = span.next();
Assert.assertEquals(9, span.getData().intValue());
span = span.next();
Assert.assertEquals(10, span.getData().intValue());
Assert.assertNull(span.next());
span = span.previous();
Assert.assertEquals(9, span.getData().intValue());
span = span.previous();
Assert.assertEquals(5, span.getData().intValue());
span = span.previous();
Assert.assertEquals(3, span.getData().intValue());
span = span.previous();
Assert.assertEquals(2, span.getData().intValue());
span = span.previous();
Assert.assertEquals(0, span.getData().intValue());
Assert.assertNull(span.previous());
}
@Test
public void testTransitionsNavigation() {
final AbsoluteDate ref = AbsoluteDate.ARBITRARY_EPOCH;
TimeSpanMap<Integer> map = new TimeSpanMap<>(Integer.valueOf(0));
map.addValidAfter(Integer.valueOf(10), ref.shiftedBy(10.0), false);
map.addValidAfter(Integer.valueOf( 3), ref.shiftedBy( 2.0), false);
map.addValidAfter(Integer.valueOf( 9), ref.shiftedBy( 5.0), false);
map.addValidBefore(Integer.valueOf( 2), ref.shiftedBy( 3.0), false);
map.addValidBefore(Integer.valueOf( 5), ref.shiftedBy( 9.0), false);
Assert.assertEquals( 2.0, map.getFirstTransition().getDate().durationFrom(ref), 1.0e-15);
Assert.assertEquals(10.0, map.getLastTransition().getDate().durationFrom(ref), 1.0e-15);
Transition<Integer> transition = map.getLastTransition();
Assert.assertEquals(10.0, transition.getDate().durationFrom(ref), 1.0e-15);
transition = transition.previous();
Assert.assertEquals( 9.0, transition.getDate().durationFrom(ref), 1.0e-15);
transition = transition.previous();
Assert.assertEquals( 5.0, transition.getDate().durationFrom(ref), 1.0e-15);
transition = transition.previous();
Assert.assertEquals( 3.0, transition.getDate().durationFrom(ref), 1.0e-15);
transition = transition.previous();
Assert.assertEquals( 2.0, transition.getDate().durationFrom(ref), 1.0e-15);
Assert.assertNull(transition.previous());
transition = transition.next();
Assert.assertEquals( 3.0, transition.getDate().durationFrom(ref), 1.0e-15);
transition = transition.next();
Assert.assertEquals( 5.0, transition.getDate().durationFrom(ref), 1.0e-15);
transition = transition.next();
Assert.assertEquals( 9.0, transition.getDate().durationFrom(ref), 1.0e-15);
transition = transition.next();
Assert.assertEquals(10.0, transition.getDate().durationFrom(ref), 1.0e-15);
Assert.assertNull(transition.next());
}
@Before
public void setUp() {
Utils.setDataRoot("regular-data");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment