TimeInterval.java

  1. /* Copyright 2022-2025 Romain Serra
  2.  * Licensed to CS GROUP (CS) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * CS licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *   http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.orekit.time;

  18. /**
  19.  * Interface representing a closed time interval i.e. [a, b], possibly of infinite length.
  20.  *
  21.  * @author Romain Serra
  22.  * @since 13.1
  23.  * @see AbsoluteDate
  24.  */
  25. public interface TimeInterval {

  26.     /**
  27.      * Getter for the left end of the interval.
  28.      * @return left end
  29.      */
  30.     AbsoluteDate getStartDate();

  31.     /**
  32.      * Getter for the right end of the interval.
  33.      * @return right end
  34.      */
  35.     AbsoluteDate getEndDate();

  36.     /**
  37.      * Computes the interval length in seconds.
  38.      * @return duration
  39.      */
  40.     default double duration() {
  41.         return getEndDate().durationFrom(getStartDate());
  42.     }

  43.     /**
  44.      * Method returning true if and only if the dated input is contained within the closed interval.
  45.      * @param timeStamped time stamped object
  46.      * @return boolean on inclusion
  47.      */
  48.     default boolean contains(final TimeStamped timeStamped) {
  49.         final AbsoluteDate date = timeStamped.getDate();
  50.         return getStartDate().isBeforeOrEqualTo(date) && getEndDate().isAfterOrEqualTo(date);
  51.     }

  52.     /**
  53.      * Method returning true if and only if input (also a closed time interval) contains the instance.
  54.      * @param interval time interval
  55.      * @return boolean on inclusion
  56.      */
  57.     default boolean contains(final TimeInterval interval) {
  58.         return (getEndDate().isAfterOrEqualTo(interval.getEndDate())) && (getStartDate().isBeforeOrEqualTo(interval.getStartDate()));
  59.     }

  60.     /**
  61.      * Method returning true if and only if input (also a closed time interval) intersects the instance.
  62.      * @param interval time interval
  63.      * @return boolean on intersection
  64.      */
  65.     default boolean intersects(final TimeInterval interval) {
  66.         return (getEndDate().isAfterOrEqualTo(interval.getStartDate())) && (getStartDate().isBeforeOrEqualTo(interval.getEndDate()));
  67.     }

  68.     /**
  69.      * Create instance from two dates in arbitrary order.
  70.      * @param date date
  71.      * @param otherDate other date
  72.      * @return time interval
  73.      */
  74.     static TimeInterval of(final AbsoluteDate date, final AbsoluteDate otherDate) {
  75.         if (otherDate.isBefore(date)) {
  76.             return of(otherDate, date);
  77.         }
  78.         return new TimeInterval() {

  79.             @Override
  80.             public AbsoluteDate getStartDate() {
  81.                 return date;
  82.             }

  83.             @Override
  84.             public AbsoluteDate getEndDate() {
  85.                 return otherDate;
  86.             }
  87.         };
  88.     }
  89. }