diff --git a/src/main/java/org/orekit/time/AbsoluteDate.java b/src/main/java/org/orekit/time/AbsoluteDate.java index a8e41998f06ce4faa47fc164cb133992e24a6068..4273179388f9e33cdeba31968c13dceb293276dc 100644 --- a/src/main/java/org/orekit/time/AbsoluteDate.java +++ b/src/main/java/org/orekit/time/AbsoluteDate.java @@ -976,7 +976,7 @@ public class AbsoluteDate return this; } - /** Check if the instance represent the same time as another instance. + /** Check if the instance represents the same time as another instance. * @param date other date * @return true if the instance and the other date refer to the same instant */ @@ -995,6 +995,105 @@ public class AbsoluteDate } + /** Check if the instance represents the same time as another. + * @param other the instant to compare this date to + * @return true if the instance and the argument refer to the same instant + * @see #isCloseTo(TimeStamped, double) + * @since 10.1 + */ + public boolean isEqualTo(final TimeStamped other) { + return this.equals(other.getDate()); + } + + /** Check if the instance time is close to another. + * @param other the instant to compare this date to + * @param tolerance the separation, in seconds, under which the two instants will be considered close to each other + * @return true if the duration between the instance and the argument is strictly below the tolerance + * @see #isEqualTo(TimeStamped) + * @since 10.1 + */ + public boolean isCloseTo(final TimeStamped other, final double tolerance) { + return FastMath.abs(this.durationFrom(other.getDate())) < tolerance; + } + + /** Check if the instance represents a time that is strictly before another. + * @param other the instant to compare this date to + * @return true if the instance is strictly before the argument when ordering chronologically + * @see #isBeforeOrEqualTo(TimeStamped) + * @since 10.1 + */ + public boolean isBefore(final TimeStamped other) { + return this.compareTo(other.getDate()) < 0; + } + + /** Check if the instance represents a time that is strictly after another. + * @param other the instant to compare this date to + * @return true if the instance is strictly after the argument when ordering chronologically + * @see #isAfterOrEqualTo(TimeStamped) + * @since 10.1 + */ + public boolean isAfter(final TimeStamped other) { + return this.compareTo(other.getDate()) > 0; + } + + /** Check if the instance represents a time that is before or equal to another. + * @param other the instant to compare this date to + * @return true if the instance is before (or equal to) the argument when ordering chronologically + * @see #isBefore(TimeStamped) + * @since 10.1 + */ + public boolean isBeforeOrEqualTo(final TimeStamped other) { + return this.isEqualTo(other) || this.isBefore(other); + } + + /** Check if the instance represents a time that is after or equal to another. + * @param other the instant to compare this date to + * @return true if the instance is after (or equal to) the argument when ordering chronologically + * @see #isAfterOrEqualTo(TimeStamped) + * @since 10.1 + */ + public boolean isAfterOrEqualTo(final TimeStamped other) { + return this.isEqualTo(other) || this.isAfter(other); + } + + /** Check if the instance represents a time that is strictly between two others representing + * the boundaries of a time span. The two boundaries can be provided in any order: in other words, + * whether boundary represents a time that is before or after otherBoundary will + * not change the result of this method. + * @param boundary one end of the time span + * @param otherBoundary the other end of the time span + * @return true if the instance is strictly between the two arguments when ordering chronologically + * @see #isBetweenOrEqualTo(TimeStamped, TimeStamped) + * @since 10.1 + */ + public boolean isBetween(final TimeStamped boundary, final TimeStamped otherBoundary) { + final TimeStamped beginning; + final TimeStamped end; + if (boundary.getDate().isBefore(otherBoundary)) { + beginning = boundary; + end = otherBoundary; + } else { + beginning = otherBoundary; + end = boundary; + } + return this.isAfter(beginning) && this.isBefore(end); + } + + /** Check if the instance represents a time that is between two others representing + * the boundaries of a time span, or equal to one of them. The two boundaries can be provided in any order: + * in other words, whether boundary represents a time that is before or after + * otherBoundary will not change the result of this method. + * @param boundary one end of the time span + * @param otherBoundary the other end of the time span + * @return true if the instance is between the two arguments (or equal to at least one of them) + * when ordering chronologically + * @see #isBetween(TimeStamped, TimeStamped) + * @since 10.1 + */ + public boolean isBetweenOrEqualTo(final TimeStamped boundary, final TimeStamped otherBoundary) { + return this.isEqualTo(boundary) || this.isEqualTo(otherBoundary) || this.isBetween(boundary, otherBoundary); + } + /** Get a hashcode for this date. * @return hashcode */ diff --git a/src/main/java/org/orekit/time/FieldAbsoluteDate.java b/src/main/java/org/orekit/time/FieldAbsoluteDate.java index 484d17b88c9b577801492e9eb6f87d9bbfa8fac0..077c9447b34b4980496936868f9be6e820f2f667 100644 --- a/src/main/java/org/orekit/time/FieldAbsoluteDate.java +++ b/src/main/java/org/orekit/time/FieldAbsoluteDate.java @@ -1065,7 +1065,7 @@ public class FieldAbsoluteDate> } - /** Check if the instance represent the same time as another instance. + /** Check if the instance represents the same time as another instance. * @param date other date * @return true if the instance and the other date refer to the same instant */ @@ -1085,6 +1085,105 @@ public class FieldAbsoluteDate> } + /** Check if the instance represents the same time as another. + * @param other the instant to compare this date to + * @return true if the instance and the argument refer to the same instant + * @see #isCloseTo(FieldTimeStamped, double) + * @since 10.1 + */ + public boolean isEqualTo(final FieldTimeStamped other) { + return this.equals(other.getDate()); + } + + /** Check if the instance time is close to another. + * @param other the instant to compare this date to + * @param tolerance the separation, in seconds, under which the two instants will be considered close to each other + * @return true if the duration between the instance and the argument is strictly below the tolerance + * @see #isEqualTo(FieldTimeStamped) + * @since 10.1 + */ + public boolean isCloseTo(final FieldTimeStamped other, final double tolerance) { + return FastMath.abs(this.durationFrom(other.getDate()).getReal()) < tolerance; + } + + /** Check if the instance represents a time that is strictly before another. + * @param other the instant to compare this date to + * @return true if the instance is strictly before the argument when ordering chronologically + * @see #isBeforeOrEqualTo(FieldTimeStamped) + * @since 10.1 + */ + public boolean isBefore(final FieldTimeStamped other) { + return this.compareTo(other.getDate()) < 0; + } + + /** Check if the instance represents a time that is strictly after another. + * @param other the instant to compare this date to + * @return true if the instance is strictly after the argument when ordering chronologically + * @see #isAfterOrEqualTo(FieldTimeStamped) + * @since 10.1 + */ + public boolean isAfter(final FieldTimeStamped other) { + return this.compareTo(other.getDate()) > 0; + } + + /** Check if the instance represents a time that is before or equal to another. + * @param other the instant to compare this date to + * @return true if the instance is before (or equal to) the argument when ordering chronologically + * @see #isBefore(FieldTimeStamped) + * @since 10.1 + */ + public boolean isBeforeOrEqualTo(final FieldTimeStamped other) { + return this.isEqualTo(other) || this.isBefore(other); + } + + /** Check if the instance represents a time that is after or equal to another. + * @param other the instant to compare this date to + * @return true if the instance is after (or equal to) the argument when ordering chronologically + * @see #isAfterOrEqualTo(FieldTimeStamped) + * @since 10.1 + */ + public boolean isAfterOrEqualTo(final FieldTimeStamped other) { + return this.isEqualTo(other) || this.isAfter(other); + } + + /** Check if the instance represents a time that is strictly between two others representing + * the boundaries of a time span. The two boundaries can be provided in any order: in other words, + * whether boundary represents a time that is before or after otherBoundary will + * not change the result of this method. + * @param boundary one end of the time span + * @param otherBoundary the other end of the time span + * @return true if the instance is strictly between the two arguments when ordering chronologically + * @see #isBetweenOrEqualTo(FieldTimeStamped, FieldTimeStamped) + * @since 10.1 + */ + public boolean isBetween(final FieldTimeStamped boundary, final FieldTimeStamped otherBoundary) { + final FieldTimeStamped beginning; + final FieldTimeStamped end; + if (boundary.getDate().isBefore(otherBoundary)) { + beginning = boundary; + end = otherBoundary; + } else { + beginning = otherBoundary; + end = boundary; + } + return this.isAfter(beginning) && this.isBefore(end); + } + + /** Check if the instance represents a time that is between two others representing + * the boundaries of a time span, or equal to one of them. The two boundaries can be provided in any order: + * in other words, whether boundary represents a time that is before or after + * otherBoundary will not change the result of this method. + * @param boundary one end of the time span + * @param otherBoundary the other end of the time span + * @return true if the instance is between the two arguments (or equal to at least one of them) + * when ordering chronologically + * @see #isBetween(FieldTimeStamped, FieldTimeStamped) + * @since 10.1 + */ + public boolean isBetweenOrEqualTo(final FieldTimeStamped boundary, final FieldTimeStamped otherBoundary) { + return this.isEqualTo(boundary) || this.isEqualTo(otherBoundary) || this.isBetween(boundary, otherBoundary); + } + /** Get a hashcode for this date. * @return hashcode */ diff --git a/src/test/java/org/orekit/time/AbsoluteDateTest.java b/src/test/java/org/orekit/time/AbsoluteDateTest.java index bf28900c08b8b0ebbc3aefb6b51c18082a8b4514..074a9ae5a053c3e37b9efc8df063b9ebe0ba0329 100644 --- a/src/test/java/org/orekit/time/AbsoluteDateTest.java +++ b/src/test/java/org/orekit/time/AbsoluteDateTest.java @@ -682,6 +682,87 @@ public class AbsoluteDateTest { // Assert.assertTrue(d.compareTo(AbsoluteDate.PAST_INFINITY) > 0); } + @Test + public void testIsEqualTo() { + Assert.assertTrue(present.isEqualTo(present)); + Assert.assertTrue(present.isEqualTo(presentToo)); + Assert.assertFalse(present.isEqualTo(past)); + Assert.assertFalse(present.isEqualTo(future)); + } + + @Test + public void testIsCloseTo() { + double tolerance = 10; + TimeStamped closeToPresent = new AnyTimeStamped(present.shiftedBy(5)); + Assert.assertTrue(present.isCloseTo(present, tolerance)); + Assert.assertTrue(present.isCloseTo(presentToo, tolerance)); + Assert.assertTrue(present.isCloseTo(closeToPresent, tolerance)); + Assert.assertFalse(present.isCloseTo(past, tolerance)); + Assert.assertFalse(present.isCloseTo(future, tolerance)); + } + + @Test + public void testIsBefore() { + Assert.assertFalse(present.isBefore(past)); + Assert.assertFalse(present.isBefore(present)); + Assert.assertFalse(present.isBefore(presentToo)); + Assert.assertTrue(present.isBefore(future)); + } + + @Test + public void testIsAfter() { + Assert.assertTrue(present.isAfter(past)); + Assert.assertFalse(present.isAfter(present)); + Assert.assertFalse(present.isAfter(presentToo)); + Assert.assertFalse(present.isAfter(future)); + } + + @Test + public void testIsBeforeOrEqualTo() { + Assert.assertFalse(present.isBeforeOrEqualTo(past)); + Assert.assertTrue(present.isBeforeOrEqualTo(present)); + Assert.assertTrue(present.isBeforeOrEqualTo(presentToo)); + Assert.assertTrue(present.isBeforeOrEqualTo(future)); + } + + @Test + public void testIsAfterOrEqualTo() { + Assert.assertTrue(present.isAfterOrEqualTo(past)); + Assert.assertTrue(present.isAfterOrEqualTo(present)); + Assert.assertTrue(present.isAfterOrEqualTo(presentToo)); + Assert.assertFalse(present.isAfterOrEqualTo(future)); + } + + @Test + public void testIsBetween() { + Assert.assertTrue(present.isBetween(past, future)); + Assert.assertTrue(present.isBetween(future, past)); + Assert.assertFalse(past.getDate().isBetween(present, future)); + Assert.assertFalse(past.getDate().isBetween(future, present)); + Assert.assertFalse(future.getDate().isBetween(past, present)); + Assert.assertFalse(future.getDate().isBetween(present, past)); + Assert.assertFalse(present.isBetween(present, future)); + Assert.assertFalse(present.isBetween(past, present)); + Assert.assertFalse(present.isBetween(past, past)); + Assert.assertFalse(present.isBetween(present, present)); + Assert.assertFalse(present.isBetween(present, presentToo)); + } + + @Test + public void testIsBetweenOrEqualTo() { + Assert.assertTrue(present.isBetweenOrEqualTo(past, future)); + Assert.assertTrue(present.isBetweenOrEqualTo(future, past)); + Assert.assertFalse(past.getDate().isBetweenOrEqualTo(present, future)); + Assert.assertFalse(past.getDate().isBetweenOrEqualTo(future, present)); + Assert.assertFalse(future.getDate().isBetweenOrEqualTo(past, present)); + Assert.assertFalse(future.getDate().isBetweenOrEqualTo(present, past)); + Assert.assertTrue(present.isBetweenOrEqualTo(present, future)); + Assert.assertTrue(present.isBetweenOrEqualTo(past, present)); + Assert.assertFalse(present.isBetweenOrEqualTo(past, past)); + Assert.assertTrue(present.isBetweenOrEqualTo(present, present)); + Assert.assertTrue(present.isBetweenOrEqualTo(present, presentToo)); + } + @Test public void testAccuracy() { TimeScale tai = TimeScalesFactory.getTAI(); @@ -863,8 +944,29 @@ public class AbsoluteDateTest { public void setUp() { Utils.setDataRoot("regular-data"); utc = TimeScalesFactory.getUTC(); + present = new AbsoluteDate(new DateComponents(2000, 1, 1), + new TimeComponents(12, 00, 00), utc); + presentToo = new AnyTimeStamped(present.shiftedBy(0)); + past = new AnyTimeStamped(present.shiftedBy(-1000)); + future = new AnyTimeStamped(present.shiftedBy(1000)); } private TimeScale utc; + private AbsoluteDate present; + private AnyTimeStamped past; + private AnyTimeStamped presentToo; + private AnyTimeStamped future; + + static class AnyTimeStamped implements TimeStamped { + AbsoluteDate date; + public AnyTimeStamped(AbsoluteDate date) { + this.date = date; + } + + @Override + public AbsoluteDate getDate() { + return date; + } + } } diff --git a/src/test/java/org/orekit/time/FieldAbsoluteDateTest.java b/src/test/java/org/orekit/time/FieldAbsoluteDateTest.java index 5084ade1ba856bdb92ab0a0992b426cbcfb636bd..c26c089680e7e9b48576b934009d5bb1704a9171 100644 --- a/src/test/java/org/orekit/time/FieldAbsoluteDateTest.java +++ b/src/test/java/org/orekit/time/FieldAbsoluteDateTest.java @@ -18,7 +18,9 @@ package org.orekit.time; import java.io.IOException; +import java.util.Arrays; import java.util.Date; +import java.util.List; import java.util.TimeZone; import org.hipparchus.Field; @@ -170,6 +172,30 @@ public class FieldAbsoluteDateTest { doTestEquals(Decimal64Field.getInstance()); } + @Test + public void testIsEqualTo() { doTestIsEqualTo(Decimal64Field.getInstance()); } + + @Test + public void testIsCloseTo() { doTestIsCloseTo(Decimal64Field.getInstance()); } + + @Test + public void testIsBefore() { doTestIsBefore(Decimal64Field.getInstance()); } + + @Test + public void testIsAfter() { doTestIsAfter(Decimal64Field.getInstance()); } + + @Test + public void testIsBeforeOrEqualTo() { doTestIsBeforeOrEqualTo(Decimal64Field.getInstance()); } + + @Test + public void testIsAfterOrEqualTo() { doTestIsAfterOrEqualTo(Decimal64Field.getInstance()); } + + @Test + public void testIsBetween() { doTestIsBetween(Decimal64Field.getInstance()); } + + @Test + public void testIsBetweenOrEqualTo() { doTestIsBetweenOrEqualTo(Decimal64Field.getInstance()); } + @Test public void testComponents() { doTestComponents(Decimal64Field.getInstance()); @@ -610,6 +636,95 @@ public class FieldAbsoluteDateTest { Assert.assertFalse(d1.equals(this)); } + private > void doTestIsEqualTo(final Field field) { + TestDates dates = new TestDates().setField(field); + FieldAbsoluteDate present = dates.getPresentFieldAbsoluteDate(); + Assert.assertTrue(present.isEqualTo(dates.present)); + Assert.assertTrue(present.isEqualTo(dates.presentToo)); + Assert.assertFalse(present.isEqualTo(dates.past)); + Assert.assertFalse(present.isEqualTo(dates.future)); + } + + private > void doTestIsCloseTo(final Field field) { + double tolerance = 10; + TestDates dates = new TestDates().setField(field); + FieldAbsoluteDate present = dates.getPresentFieldAbsoluteDate(); + FieldTimeStamped closeToPresent = new AnyFieldTimeStamped(dates.presentDate.shiftedBy(5)).setField(field); + Assert.assertTrue(present.isCloseTo(present, tolerance)); + Assert.assertTrue(present.isCloseTo(dates.presentToo, tolerance)); + Assert.assertTrue(present.isCloseTo(closeToPresent, tolerance)); + Assert.assertFalse(present.isCloseTo(dates.past, tolerance)); + Assert.assertFalse(present.isCloseTo(dates.future, tolerance)); + } + + private > void doTestIsBefore(final Field field) { + TestDates dates = new TestDates().setField(field); + FieldAbsoluteDate present = dates.getPresentFieldAbsoluteDate(); + Assert.assertFalse(present.isBefore(dates.past)); + Assert.assertFalse(present.isBefore(present)); + Assert.assertFalse(present.isBefore(dates.presentToo)); + Assert.assertTrue(present.isBefore(dates.future)); + } + + private > void doTestIsAfter(final Field field) { + TestDates dates = new TestDates().setField(field); + FieldAbsoluteDate present = dates.getPresentFieldAbsoluteDate(); + Assert.assertTrue(present.isAfter(dates.past)); + Assert.assertFalse(present.isAfter(present)); + Assert.assertFalse(present.isAfter(dates.presentToo)); + Assert.assertFalse(present.isAfter(dates.future)); + } + + private > void doTestIsBeforeOrEqualTo(final Field field) { + TestDates dates = new TestDates().setField(field); + FieldAbsoluteDate present = dates.getPresentFieldAbsoluteDate(); + Assert.assertFalse(present.isBeforeOrEqualTo(dates.past)); + Assert.assertTrue(present.isBeforeOrEqualTo(present)); + Assert.assertTrue(present.isBeforeOrEqualTo(dates.presentToo)); + Assert.assertTrue(present.isBeforeOrEqualTo(dates.future)); + } + + private > void doTestIsAfterOrEqualTo(final Field field) { + TestDates dates = new TestDates().setField(field); + FieldAbsoluteDate present = dates.getPresentFieldAbsoluteDate(); + Assert.assertTrue(present.isAfterOrEqualTo(dates.past)); + Assert.assertTrue(present.isAfterOrEqualTo(present)); + Assert.assertTrue(present.isAfterOrEqualTo(dates.presentToo)); + Assert.assertFalse(present.isAfterOrEqualTo(dates.future)); + } + + private > void doTestIsBetween(final Field field) { + TestDates dates = new TestDates().setField(field); + FieldAbsoluteDate present = dates.getPresentFieldAbsoluteDate(); + Assert.assertTrue(present.isBetween(dates.past, dates.future)); + Assert.assertTrue(present.isBetween(dates.future, dates.past)); + Assert.assertFalse(dates.past.getDate().isBetween(present, dates.future)); + Assert.assertFalse(dates.past.getDate().isBetween(dates.future, present)); + Assert.assertFalse(dates.future.getDate().isBetween(dates.past, present)); + Assert.assertFalse(dates.future.getDate().isBetween(present, dates.past)); + Assert.assertFalse(present.isBetween(present, dates.future)); + Assert.assertFalse(present.isBetween(dates.past, present)); + Assert.assertFalse(present.isBetween(dates.past, dates.past)); + Assert.assertFalse(present.isBetween(present, present)); + Assert.assertFalse(present.isBetween(present, dates.presentToo)); + } + + private > void doTestIsBetweenOrEqualTo(final Field field) { + TestDates dates = new TestDates().setField(field); + FieldAbsoluteDate present = dates.getPresentFieldAbsoluteDate(); + Assert.assertTrue(present.isBetweenOrEqualTo(dates.past, dates.future)); + Assert.assertTrue(present.isBetweenOrEqualTo(dates.future, dates.past)); + Assert.assertFalse(dates.past.getDate().isBetweenOrEqualTo(present, dates.future)); + Assert.assertFalse(dates.past.getDate().isBetweenOrEqualTo(dates.future, present)); + Assert.assertFalse(dates.future.getDate().isBetweenOrEqualTo(dates.past, present)); + Assert.assertFalse(dates.future.getDate().isBetweenOrEqualTo(present, dates.past)); + Assert.assertTrue(present.isBetweenOrEqualTo(present, dates.future)); + Assert.assertTrue(present.isBetweenOrEqualTo(dates.past, present)); + Assert.assertFalse(present.isBetweenOrEqualTo(dates.past, dates.past)); + Assert.assertTrue(present.isBetweenOrEqualTo(present, present)); + Assert.assertTrue(present.isBetweenOrEqualTo(present, dates.presentToo)); + } + private > void doTestComponents(final Field field) { // this is NOT J2000.0, // it is either a few seconds before or after depending on time scale @@ -978,4 +1093,52 @@ public class FieldAbsoluteDateTest { Assert.assertEquals(0.0, tA.durationFrom(tB).getReal(), Precision.SAFE_MIN); } + static class AnyFieldTimeStamped implements FieldTimeStamped { + AbsoluteDate date; + Field field; + + public AnyFieldTimeStamped(AbsoluteDate date) { + this.date = date; + } + + public AnyFieldTimeStamped setField(Field field) { + this.field = field; + return this; + } + + @Override + public FieldAbsoluteDate getDate() { + return new FieldAbsoluteDate(field, date); + } + } + + static class TestDates { + private final AbsoluteDate presentDate; + private final AnyFieldTimeStamped present; + private final AnyFieldTimeStamped past; + private final AnyFieldTimeStamped presentToo; + private final AnyFieldTimeStamped future; + private final List datesList; + + public TestDates() { + presentDate = new AbsoluteDate(new DateComponents(2000, 1, 1), + new TimeComponents(12, 00, 00), + TimeScalesFactory.getUTC()); + present = new AnyFieldTimeStamped(presentDate); + presentToo = new AnyFieldTimeStamped(presentDate.shiftedBy(0)); + past = new AnyFieldTimeStamped(presentDate.shiftedBy(-1000)); + future = new AnyFieldTimeStamped(presentDate.shiftedBy(1000)); + datesList = Arrays.asList(present, past, presentToo, future); + } + + public TestDates setField(Field field) { + datesList.forEach(date -> date.setField(field)); + return this; + } + + public FieldAbsoluteDate getPresentFieldAbsoluteDate() { + return present.getDate(); + } + } + }