FieldTimeSpanMap.java

  1. /* Copyright 2002-2018 CS Systèmes d'Information
  2.  * Licensed to CS Systèmes d'Information (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.RealFieldElement;
  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.  * @author Luc Maisonobe
  31.  * @since 7.1
  32.  */
  33. public class FieldTimeSpanMap<T, D extends RealFieldElement<D>> {

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

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

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

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

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

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

  90.     }

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

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

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

  128.     }

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

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

  149.     /** Local class holding transition times. */
  150.     public static class Transition<S, D extends RealFieldElement<D>> implements TimeStamped {

  151.         /** Transition date. */
  152.         private final FieldAbsoluteDate<D> date;

  153.         /** Entry valid before the transition. */
  154.         private final S before;

  155.         /** Entry valid after the transition. */
  156.         private final S after;

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

  167.         /** Get the transition field absolute date.
  168.          * @return transition date
  169.          */
  170.         public FieldAbsoluteDate<D> getAbsoluteDate() {
  171.             return date;
  172.         }
  173.         /** Get the transition absolute date.
  174.          * @return transition date
  175.          */

  176.         public AbsoluteDate getDate() {
  177.             return date.toAbsoluteDate();
  178.         }
  179.         /** Get the entry valid before transition.
  180.          * @return entry valid before transition
  181.          */
  182.         public S getBefore() {
  183.             return before;
  184.         }

  185.         /** Get the entry valid after transition.
  186.          * @return entry valid after transition
  187.          */
  188.         public S getAfter() {
  189.             return after;
  190.         }

  191.     }



  192. }