FixedTransformProvider.java

  1. /* Copyright 2002-2022 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.         FieldTransform<T> ft = (FieldTransform<T>) cached.get(date.getField());
  52.         if (ft == null) {
  53.             ft = new FieldTransform<>(date.getField(), transform);
  54.             cached.put(date.getField(), ft);
  55.         }

  56.         return ft;

  57.     }

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

  67.     /** Internal class used only for serialization. */
  68.     private static class DataTransferObject implements Serializable {

  69.         /** Serializable UID. */
  70.         private static final long serialVersionUID = 20170106L;

  71.         /** Fixed transform. */
  72.         private final Transform transform;

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

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

  85.     }

  86. }