FieldTimeSpanMap.java

  1. /* Copyright 2002-2025 CS GROUP
  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.utils;

  18. import java.util.Collections;
  19. import java.util.NavigableSet;
  20. import java.util.SortedSet;
  21. import java.util.TreeSet;

  22. import org.orekit.time.FieldAbsoluteDate;
  23. import org.orekit.time.TimeStamped;
  24. import org.hipparchus.Field;
  25. import org.hipparchus.CalculusFieldElement;
  26. import org.orekit.time.AbsoluteDate;
  27. import org.orekit.time.ChronologicalComparator;

  28. /** Container for objects that apply to spans of time.

  29.  * @param <T> Type of the data.
  30.  * @param <D> type of the field elements

  31.  * @author Luc Maisonobe
  32.  * @since 7.1
  33.  */
  34. public class FieldTimeSpanMap<T, D extends CalculusFieldElement<D>> {

  35.     /** Container for the data. */
  36.     private final NavigableSet<Transition<T, D>> data;

  37.     /**Field.*/
  38.     private final Field<D> field;

  39.     /** Create a map containing a single object, initially valid throughout the timeline.
  40.      * <p>
  41.      * The real validity of this first entry will be truncated as other
  42.      * entries are either {@link #addValidBefore(Object, FieldAbsoluteDate)
  43.      * added before} it or {@link #addValidAfter(Object, FieldAbsoluteDate)
  44.      * added after} it.
  45.      * </p>
  46.      * @param entry entry (initially valid throughout the timeline)
  47.      * @param field_n field used by default.
  48.      */
  49.     public FieldTimeSpanMap(final T entry, final Field<D> field_n) {
  50.         data = new TreeSet<>(new ChronologicalComparator());
  51.         field = field_n;
  52.         data.add(new Transition<>(FieldAbsoluteDate.getArbitraryEpoch(field), entry, entry));
  53.     }

  54.     /** Add an entry valid before a limit date.
  55.      * <p>
  56.      * As an entry is valid, it truncates the validity of the neighboring
  57.      * entries already present in the map.
  58.      * </p>
  59.      * <p>
  60.      * The transition dates should be entered only once, either
  61.      * by a call to this method or by a call to {@link #addValidAfter(Object,
  62.      * FieldAbsoluteDate)}. Repeating a transition date will lead to unexpected
  63.      * result and is not supported.
  64.      * </p>
  65.      * @param entry entry to add
  66.      * @param latestValidityDate date before which the entry is valid
  67.      * (sould be different from <em>all</em> dates already used for transitions)
  68.      */
  69.     public void addValidBefore(final T entry, final FieldAbsoluteDate<D> latestValidityDate) {

  70.         if (data.size() == 1) {
  71.             final Transition<T, D> single = data.first();
  72.             if (single.getBefore() == single.getAfter()) {
  73.                 // the single entry was a dummy one, without a real transition
  74.                 // we replace it entirely
  75.                 data.clear();
  76.                 data.add(new Transition<>(latestValidityDate, entry, single.getAfter()));
  77.                 return;
  78.             }
  79.         }

  80.         final Transition<T, D> previous =
  81.                 data.floor(new Transition<>(latestValidityDate, entry, null));
  82.         if (previous == null) {
  83.             // the new transition will be the first one
  84.             data.add(new Transition<>(latestValidityDate, entry, data.first().getBefore()));
  85.         } else {
  86.             // the new transition will be after the previous one
  87.             data.remove(previous);
  88.             data.add(new Transition<>(previous.date, previous.getBefore(), entry));
  89.             data.add(new Transition<>(latestValidityDate, entry, previous.getAfter()));
  90.         }

  91.     }

  92.     /** Add an entry valid after a limit date.
  93.      * <p>
  94.      * As an entry is valid, it truncates the validity of the neighboring
  95.      * entries already present in the map.
  96.      * </p>
  97.      * <p>
  98.      * The transition dates should be entered only once, either
  99.      * by a call to this method or by a call to {@link #addValidBefore(Object,
  100.      * FieldAbsoluteDate)}. Repeating a transition date will lead to unexpected
  101.      * result and is not supported.
  102.      * </p>
  103.      * @param entry entry to add
  104.      * @param earliestValidityDate date after which the entry is valid
  105.      * (sould be different from <em>all</em> dates already used for transitions)
  106.      */
  107.     public void addValidAfter(final T entry, final FieldAbsoluteDate<D> earliestValidityDate) {

  108.         if (data.size() == 1) {
  109.             final Transition<T, D> single = data.first();
  110.             if (single.getBefore() == single.getAfter()) {
  111.                 // the single entry was a dummy one, without a real transition
  112.                 // we replace it entirely
  113.                 data.clear();
  114.                 data.add(new Transition<>(earliestValidityDate, single.getBefore(), entry));
  115.                 return;
  116.             }
  117.         }

  118.         final Transition<T, D> next =
  119.                 data.ceiling(new Transition<>(earliestValidityDate, entry, null));
  120.         if (next == null) {
  121.             // the new transition will be the last one
  122.             data.add(new Transition<>(earliestValidityDate, data.last().getAfter(), entry));
  123.         } else {
  124.             // the new transition will be before the next one
  125.             data.remove(next);
  126.             data.add(new Transition<>(earliestValidityDate, next.getBefore(), entry));
  127.             data.add(new Transition<>(next.date, entry, next.getAfter()));
  128.         }

  129.     }

  130.     /** Get the entry valid at a specified date.
  131.      * @param date date at which the entry must be valid
  132.      * @return valid entry at specified date
  133.      */
  134.     public T get(final FieldAbsoluteDate<D> date) {
  135.         final Transition<T, D> previous = data.floor(new Transition<>(date, null, null));
  136.         if (previous == null) {
  137.             // there are no transition before the specified date
  138.             // return the first valid entry
  139.             return data.first().getBefore();
  140.         } else {
  141.             return previous.getAfter();
  142.         }
  143.     }

  144.     /** Get an unmodifiable view of the sorted transitions.
  145.      * @return unmodifiable view of the sorted transitions
  146.      */
  147.     public SortedSet<Transition<T, D>> getTransitions() {
  148.         return Collections.unmodifiableSortedSet(data);
  149.     }

  150.     /** Local class holding transition times.
  151.      * @param <D> type of the field elements
  152.      * @param <S> type of the data
  153.      */
  154.     public static class Transition<S, D extends CalculusFieldElement<D>> implements TimeStamped {

  155.         /** Transition date. */
  156.         private final FieldAbsoluteDate<D> date;

  157.         /** Entry valid before the transition. */
  158.         private final S before;

  159.         /** Entry valid after the transition. */
  160.         private final S after;

  161.         /** Simple constructor.
  162.          * @param date transition date
  163.          * @param before entry valid before the transition
  164.          * @param after entry valid after the transition
  165.          */
  166.         private Transition(final FieldAbsoluteDate<D> date, final S before, final S after) {
  167.             this.date   = date;
  168.             this.before = before;
  169.             this.after  = after;
  170.         }

  171.         /** Get the transition field absolute date.
  172.          * @return transition date
  173.          */
  174.         public FieldAbsoluteDate<D> getAbsoluteDate() {
  175.             return date;
  176.         }
  177.         /** Get the transition absolute date.
  178.          * @return transition date
  179.          */

  180.         public AbsoluteDate getDate() {
  181.             return date.toAbsoluteDate();
  182.         }
  183.         /** Get the entry valid before transition.
  184.          * @return entry valid before transition
  185.          */
  186.         public S getBefore() {
  187.             return before;
  188.         }

  189.         /** Get the entry valid after transition.
  190.          * @return entry valid after transition
  191.          */
  192.         public S getAfter() {
  193.             return after;
  194.         }

  195.     }



  196. }