FixedTransformProvider.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.frames;

  18. import java.io.Serializable;
  19. import java.util.HashMap;
  20. import java.util.Map;

  21. import org.hipparchus.Field;
  22. import org.hipparchus.CalculusFieldElement;
  23. import org.orekit.time.AbsoluteDate;
  24. import org.orekit.time.FieldAbsoluteDate;

  25. /**
  26. ** Transform provider using fixed transform.
  27.  * @author Luc Maisonobe
  28.  */
  29. public class FixedTransformProvider implements TransformProvider {

  30.     /** Serializable UID. */
  31.     private static final long serialVersionUID = 20170106L;

  32.     /** Fixed transform. */
  33.     private final Transform transform;

  34.     /** Cached field-based transforms. */
  35.     private final transient Map<Field<? extends CalculusFieldElement<?>>, FieldTransform<? extends CalculusFieldElement<?>>> cached;

  36.     /** Simple constructor.
  37.      * @param transform fixed transform
  38.      */
  39.     public FixedTransformProvider(final Transform transform) {
  40.         this.transform = transform;
  41.         this.cached    = new HashMap<>();
  42.     }

  43.     /** {@inheritDoc} */
  44.     public Transform getTransform(final AbsoluteDate date) {
  45.         return transform;
  46.     }

  47.     /** {@inheritDoc} */
  48.     @Override
  49.     public <T extends CalculusFieldElement<T>> FieldTransform<T> getTransform(final FieldAbsoluteDate<T> date) {

  50.         @SuppressWarnings("unchecked")
  51.         final FieldTransform<T> ft =
  52.                 (FieldTransform<T>) cached.computeIfAbsent(date.getField(),
  53.                                                            f -> new FieldTransform<>((Field<T>) f, transform));

  54.         return ft;

  55.     }

  56.     /** Replace the instance with a data transfer object for serialization.
  57.      * <p>
  58.      * This intermediate class serializes nothing.
  59.      * </p>
  60.      * @return data transfer object that will be serialized
  61.      */
  62.     private Object writeReplace() {
  63.         return new DataTransferObject(transform);
  64.     }

  65.     /** Internal class used only for serialization. */
  66.     private static class DataTransferObject implements Serializable {

  67.         /** Serializable UID. */
  68.         private static final long serialVersionUID = 20170106L;

  69.         /** Fixed transform. */
  70.         private final Transform transform;

  71.         /** Simple constructor.
  72.          * @param transform fixed transform
  73.          */
  74.         private DataTransferObject(final Transform transform) {
  75.             this.transform = transform;
  76.         }

  77.         /** Replace the deserialized data transfer object with a {@link FixedTransformProvider}.
  78.          * @return replacement {@link FixedTransformProvider}
  79.          */
  80.         private Object readResolve() {
  81.             return new FixedTransformProvider(transform);
  82.         }

  83.     }

  84. }