ImmutableFieldTimeStampedCache.java

  1. /* Copyright 2002-2024 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.Field;
  25. import org.hipparchus.exception.LocalizedCoreFormats;
  26. import org.orekit.errors.OrekitException;
  27. import org.orekit.errors.OrekitIllegalArgumentException;
  28. import org.orekit.errors.OrekitIllegalStateException;
  29. import org.orekit.errors.OrekitMessages;
  30. import org.orekit.errors.TimeStampedCacheException;
  31. import org.orekit.time.FieldAbsoluteDate;
  32. import org.orekit.time.FieldChronologicalComparator;
  33. import org.orekit.time.FieldTimeStamped;
  34. import org.orekit.time.TimeStamped;

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

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

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

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

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

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

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

  87.     }

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

  94.     /**
  95.      * Get an empty immutable cache, cast to the correct type.
  96.      *
  97.      * @param <TS> the type of data
  98.      * @param <CFE> the type of the calculus field element
  99.      * @param ignored field to which the elements belong
  100.      * @return an empty {@link ImmutableTimeStampedCache}.
  101.      * @deprecated as of 12.1, replaced by {@link #emptyCache()}
  102.      */
  103.     @Deprecated
  104.     public static <TS extends FieldTimeStamped<CFE>, CFE extends CalculusFieldElement<CFE>>
  105.         ImmutableFieldTimeStampedCache<TS, CFE> emptyCache(final Field<CFE> ignored) {
  106.         return emptyCache();
  107.     }

  108.     /**
  109.      * Get an empty immutable cache.
  110.      *
  111.      * @param <TS> the type of data
  112.      * @param <CFE> the type of the calculus field element
  113.      * @return an empty {@link ImmutableTimeStampedCache}.
  114.      * @since 12.1
  115.      */
  116.     @SuppressWarnings("unchecked")
  117.     public static <TS extends FieldTimeStamped<CFE>, CFE extends CalculusFieldElement<CFE>>
  118.         ImmutableFieldTimeStampedCache<TS, CFE> emptyCache() {
  119.         return (ImmutableFieldTimeStampedCache<TS, CFE>) EMPTY_CACHE;
  120.     }

  121.     /** {@inheritDoc} */
  122.     public Stream<T> getNeighbors(final FieldAbsoluteDate<KK> central, final int n) {
  123.         if (n > maxNeighborsSize) {
  124.             throw new OrekitException(OrekitMessages.NOT_ENOUGH_DATA, maxNeighborsSize);
  125.         }
  126.         return new FieldSortedListTrimmer(n).getNeighborsSubList(central, data).stream();
  127.     }

  128.     /** {@inheritDoc} */
  129.     public int getMaxNeighborsSize() {
  130.         return this.maxNeighborsSize;
  131.     }

  132.     /** {@inheritDoc} */
  133.     public T getEarliest() {
  134.         return this.data.get(0);
  135.     }

  136.     /** {@inheritDoc} */
  137.     public T getLatest() {
  138.         return this.data.get(this.data.size() - 1);
  139.     }

  140.     /**
  141.      * Get all the data in this cache.
  142.      *
  143.      * @return a sorted collection of all data passed in the
  144.      * {@link #ImmutableFieldTimeStampedCache(int, Collection) constructor}.
  145.      */
  146.     public List<T> getAll() {
  147.         return Collections.unmodifiableList(this.data);
  148.     }

  149.     /** {@inheritDoc} */
  150.     @Override
  151.     public String toString() {
  152.         return "Immutable cache with " + this.data.size() + " entries";
  153.     }

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

  157.         /** {@inheritDoc} */
  158.         @Override
  159.         public Stream<T> getNeighbors(final FieldAbsoluteDate<KK> central) {
  160.             throw new TimeStampedCacheException(OrekitMessages.NO_CACHED_ENTRIES);
  161.         }

  162.         /** {@inheritDoc} */
  163.         @Override
  164.         public int getMaxNeighborsSize() {
  165.             return 0;
  166.         }

  167.         /** {@inheritDoc} */
  168.         @Override
  169.         public T getEarliest() {
  170.             throw new OrekitIllegalStateException(OrekitMessages.NO_CACHED_ENTRIES);
  171.         }

  172.         /** {@inheritDoc} */
  173.         @Override
  174.         public T getLatest() {
  175.             throw new OrekitIllegalStateException(OrekitMessages.NO_CACHED_ENTRIES);
  176.         }

  177.         /** {@inheritDoc} */
  178.         @Override
  179.         public List<T> getAll() {
  180.             return Collections.emptyList();
  181.         }

  182.         /** {@inheritDoc} */
  183.         @Override
  184.         public String toString() {
  185.             return "Empty immutable cache";
  186.         }

  187.     }

  188. }