All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.orekit.utils.ImmutableFieldTimeStampedCache Maven / Gradle / Ivy

Go to download

OREKIT (ORbits Extrapolation KIT) is a low level space dynamics library. It provides basic elements (orbits, dates, attitude, frames ...) and various algorithms to handle them (conversions, analytical and numerical propagation, pointing ...).

There is a newer version: 12.2
Show newest version
/* Copyright 2002-2024 CS GROUP
 * Licensed to CS GROUP (CS) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * CS licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.orekit.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;

import org.hipparchus.CalculusFieldElement;
import org.hipparchus.Field;
import org.hipparchus.exception.LocalizedCoreFormats;
import org.orekit.errors.OrekitException;
import org.orekit.errors.OrekitIllegalArgumentException;
import org.orekit.errors.OrekitIllegalStateException;
import org.orekit.errors.OrekitMessages;
import org.orekit.errors.TimeStampedCacheException;
import org.orekit.time.FieldAbsoluteDate;
import org.orekit.time.FieldChronologicalComparator;
import org.orekit.time.FieldTimeStamped;
import org.orekit.time.TimeStamped;

/**
 * A cache of {@link TimeStamped} data that provides concurrency through immutability. This strategy is suitable when all the
 * cached data is stored in memory. (For example, {@link org.orekit.time.UTCScale UTCScale}) This class then provides
 * convenient methods for accessing the data.
 *
 * @param  the type of data
 * @param  the type the field element
 *
 * @author Evan Ward
 * @author Vincent Cucchietti
 */
public class ImmutableFieldTimeStampedCache, KK extends CalculusFieldElement>
        implements FieldTimeStampedCache {

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

    /**
     * the cached data. Be careful not to modify it after the constructor, or return a reference that allows mutating this
     * list.
     */
    private final List data;

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

    /**
     * Create a new cache with the given neighbors size and data.
     *
     * @param maxNeighborsSize the maximum size of the list returned from {@link #getNeighbors(FieldAbsoluteDate)}. Must be less than or
     * equal to {@code data.size()}.
     * @param data the backing data for this cache. The list will be copied to ensure immutability. To guarantee immutability
     * the entries in {@code data} must be immutable themselves. There must be more data than {@code maxNeighborsSize}.
     *
     * @throws IllegalArgumentException if {@code maxNeighborsSize > data.size()} or if {@code maxNeighborsSize} is negative
     */
    public ImmutableFieldTimeStampedCache(final int maxNeighborsSize,
                                          final Collection data) {
        // Parameter check
        if (maxNeighborsSize > data.size()) {
            throw new OrekitIllegalArgumentException(OrekitMessages.NOT_ENOUGH_CACHED_NEIGHBORS,
                                                     data.size(), maxNeighborsSize);
        }
        if (maxNeighborsSize < 1) {
            throw new OrekitIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_SMALL,
                                                     maxNeighborsSize, 1);
        }

        // Assign instance variables
        this.maxNeighborsSize = maxNeighborsSize;

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

    }

    /** Private constructor for {@link #EMPTY_CACHE}.
     */
    private ImmutableFieldTimeStampedCache() {
        this.data             = null;
        this.maxNeighborsSize = 0;
    }

    /**
     * Get an empty immutable cache, cast to the correct type.
     *
     * @param  the type of data
     * @param  the type of the calculus field element
     * @param ignored field to which the elements belong
     * @return an empty {@link ImmutableTimeStampedCache}.
     * @deprecated as of 12.1, replaced by {@link #emptyCache()}
     */
    @Deprecated
    public static , CFE extends CalculusFieldElement>
        ImmutableFieldTimeStampedCache emptyCache(final Field ignored) {
        return emptyCache();
    }

    /**
     * Get an empty immutable cache.
     *
     * @param  the type of data
     * @param  the type of the calculus field element
     * @return an empty {@link ImmutableTimeStampedCache}.
     * @since 12.1
     */
    @SuppressWarnings("unchecked")
    public static , CFE extends CalculusFieldElement>
        ImmutableFieldTimeStampedCache emptyCache() {
        return (ImmutableFieldTimeStampedCache) EMPTY_CACHE;
    }

    /** {@inheritDoc} */
    public Stream getNeighbors(final FieldAbsoluteDate central, final int n) {
        if (n > maxNeighborsSize) {
            throw new OrekitException(OrekitMessages.NOT_ENOUGH_DATA, maxNeighborsSize);
        }
        return new FieldSortedListTrimmer(n).getNeighborsSubList(central, data).stream();
    }

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

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

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

    /**
     * Get all the data in this cache.
     *
     * @return a sorted collection of all data passed in the
     * {@link #ImmutableFieldTimeStampedCache(int, Collection) constructor}.
     */
    public List getAll() {
        return Collections.unmodifiableList(this.data);
    }

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

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

        /** {@inheritDoc} */
        @Override
        public Stream getNeighbors(final FieldAbsoluteDate central) {
            throw new TimeStampedCacheException(OrekitMessages.NO_CACHED_ENTRIES);
        }

        /** {@inheritDoc} */
        @Override
        public int getMaxNeighborsSize() {
            return 0;
        }

        /** {@inheritDoc} */
        @Override
        public T getEarliest() {
            throw new OrekitIllegalStateException(OrekitMessages.NO_CACHED_ENTRIES);
        }

        /** {@inheritDoc} */
        @Override
        public T getLatest() {
            throw new OrekitIllegalStateException(OrekitMessages.NO_CACHED_ENTRIES);
        }

        /** {@inheritDoc} */
        @Override
        public List getAll() {
            return Collections.emptyList();
        }

        /** {@inheritDoc} */
        @Override
        public String toString() {
            return "Empty immutable cache";
        }

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy