ImmutableFieldTimeStampedCache.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.ArrayList;
  19. import java.util.Collection;
  20. import java.util.Collections;
  21. import java.util.List;
  22. import java.util.stream.Stream;

  23. import org.hipparchus.CalculusFieldElement;
  24. import org.hipparchus.exception.LocalizedCoreFormats;
  25. import org.orekit.errors.OrekitException;
  26. import org.orekit.errors.OrekitIllegalArgumentException;
  27. import org.orekit.errors.OrekitIllegalStateException;
  28. import org.orekit.errors.OrekitMessages;
  29. import org.orekit.errors.TimeStampedCacheException;
  30. import org.orekit.time.FieldAbsoluteDate;
  31. import org.orekit.time.FieldChronologicalComparator;
  32. import org.orekit.time.FieldTimeStamped;
  33. import org.orekit.time.TimeStamped;

  34. /**
  35.  * A cache of {@link TimeStamped} data that provides concurrency through immutability. This strategy is suitable when all the
  36.  * cached data is stored in memory. (For example, {@link org.orekit.time.UTCScale UTCScale}) This class then provides
  37.  * convenient methods for accessing the data.
  38.  *
  39.  * @param <T> the type of data
  40.  * @param <KK> the type the field element
  41.  *
  42.  * @author Evan Ward
  43.  * @author Vincent Cucchietti
  44.  */
  45. public class ImmutableFieldTimeStampedCache<T extends FieldTimeStamped<KK>, KK extends CalculusFieldElement<KK>>
  46.         implements FieldTimeStampedCache<T, KK> {

  47.     /** An empty immutable cache that always throws an exception on attempted access.
  48.      * @since 12.1
  49.      */
  50.     @SuppressWarnings("rawtypes")
  51.     private static final ImmutableFieldTimeStampedCache EMPTY_CACHE =
  52.         new EmptyFieldTimeStampedCache();

  53.     /**
  54.      * the cached data. Be careful not to modify it after the constructor, or return a reference that allows mutating this
  55.      * list.
  56.      */
  57.     private final List<T> data;

  58.     /** the maximum size list to return from {@link #getNeighbors(FieldAbsoluteDate)}. */
  59.     private final int maxNeighborsSize;

  60.     /**
  61.      * Create a new cache with the given neighbors size and data.
  62.      *
  63.      * @param maxNeighborsSize the maximum size of the list returned from {@link #getNeighbors(FieldAbsoluteDate)}. Must be less than or
  64.      * equal to {@code data.size()}.
  65.      * @param data the backing data for this cache. The list will be copied to ensure immutability. To guarantee immutability
  66.      * the entries in {@code data} must be immutable themselves. There must be more data than {@code maxNeighborsSize}.
  67.      *
  68.      * @throws IllegalArgumentException if {@code maxNeighborsSize > data.size()} or if {@code maxNeighborsSize} is negative
  69.      */
  70.     public ImmutableFieldTimeStampedCache(final int maxNeighborsSize,
  71.                                           final Collection<? extends T> data) {
  72.         // Parameter check
  73.         if (maxNeighborsSize > data.size()) {
  74.             throw new OrekitIllegalArgumentException(OrekitMessages.NOT_ENOUGH_CACHED_NEIGHBORS,
  75.                                                      data.size(), maxNeighborsSize);
  76.         }
  77.         if (maxNeighborsSize < 1) {
  78.             throw new OrekitIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_SMALL,
  79.                                                      maxNeighborsSize, 1);
  80.         }

  81.         // Assign instance variables
  82.         this.maxNeighborsSize = maxNeighborsSize;

  83.         // Sort and copy data first
  84.         this.data = new ArrayList<>(data);
  85.         this.data.sort(new FieldChronologicalComparator<>());

  86.     }

  87.     /** Private constructor for {@link #EMPTY_CACHE}.
  88.      */
  89.     private ImmutableFieldTimeStampedCache() {
  90.         this.data             = null;
  91.         this.maxNeighborsSize = 0;
  92.     }

  93.     /**
  94.      * Get an empty immutable cache.
  95.      *
  96.      * @param <TS> the type of data
  97.      * @param <CFE> the type of the calculus field element
  98.      * @return an empty {@link ImmutableTimeStampedCache}.
  99.      * @since 12.1
  100.      */
  101.     @SuppressWarnings("unchecked")
  102.     public static <TS extends FieldTimeStamped<CFE>, CFE extends CalculusFieldElement<CFE>>
  103.         ImmutableFieldTimeStampedCache<TS, CFE> emptyCache() {
  104.         return (ImmutableFieldTimeStampedCache<TS, CFE>) EMPTY_CACHE;
  105.     }

  106.     /** {@inheritDoc} */
  107.     public Stream<T> getNeighbors(final FieldAbsoluteDate<KK> central, final int n) {
  108.         if (n > maxNeighborsSize) {
  109.             throw new OrekitException(OrekitMessages.NOT_ENOUGH_DATA, maxNeighborsSize);
  110.         }
  111.         return new FieldSortedListTrimmer(n).getNeighborsSubList(central, data).stream();
  112.     }

  113.     /** {@inheritDoc} */
  114.     public int getMaxNeighborsSize() {
  115.         return this.maxNeighborsSize;
  116.     }

  117.     /** {@inheritDoc} */
  118.     public T getEarliest() {
  119.         return this.data.get(0);
  120.     }

  121.     /** {@inheritDoc} */
  122.     public T getLatest() {
  123.         return this.data.get(this.data.size() - 1);
  124.     }

  125.     /**
  126.      * Get all the data in this cache.
  127.      *
  128.      * @return a sorted collection of all data passed in the
  129.      * {@link #ImmutableFieldTimeStampedCache(int, Collection) constructor}.
  130.      */
  131.     public List<T> getAll() {
  132.         return Collections.unmodifiableList(this.data);
  133.     }

  134.     /** {@inheritDoc} */
  135.     @Override
  136.     public String toString() {
  137.         return "Immutable cache with " + this.data.size() + " entries";
  138.     }

  139.     /** An empty immutable cache that always throws an exception on attempted access. */
  140.     private static class EmptyFieldTimeStampedCache<T extends FieldTimeStamped<KK>, KK extends CalculusFieldElement<KK>>
  141.             extends ImmutableFieldTimeStampedCache<T, KK> {

  142.         /** {@inheritDoc} */
  143.         @Override
  144.         public Stream<T> getNeighbors(final FieldAbsoluteDate<KK> central) {
  145.             throw new TimeStampedCacheException(OrekitMessages.NO_CACHED_ENTRIES);
  146.         }

  147.         /** {@inheritDoc} */
  148.         @Override
  149.         public int getMaxNeighborsSize() {
  150.             return 0;
  151.         }

  152.         /** {@inheritDoc} */
  153.         @Override
  154.         public T getEarliest() {
  155.             throw new OrekitIllegalStateException(OrekitMessages.NO_CACHED_ENTRIES);
  156.         }

  157.         /** {@inheritDoc} */
  158.         @Override
  159.         public T getLatest() {
  160.             throw new OrekitIllegalStateException(OrekitMessages.NO_CACHED_ENTRIES);
  161.         }

  162.         /** {@inheritDoc} */
  163.         @Override
  164.         public List<T> getAll() {
  165.             return Collections.emptyList();
  166.         }

  167.         /** {@inheritDoc} */
  168.         @Override
  169.         public String toString() {
  170.             return "Empty immutable cache";
  171.         }

  172.     }

  173. }