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  
18  package org.orekit.frames;
19  
20  import java.io.Serializable;
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import org.hipparchus.Field;
25  import org.hipparchus.CalculusFieldElement;
26  import org.orekit.time.AbsoluteDate;
27  import org.orekit.time.FieldAbsoluteDate;
28  
29  /**
30  ** Transform provider using fixed transform.
31   * @author Luc Maisonobe
32   */
33  public class FixedTransformProvider implements TransformProvider {
34  
35      /** Serializable UID. */
36      private static final long serialVersionUID = 20170106L;
37  
38      /** Fixed transform. */
39      private final Transform transform;
40  
41      /** Cached field-based transforms. */
42      private final transient Map<Field<? extends CalculusFieldElement<?>>, FieldTransform<? extends CalculusFieldElement<?>>> cached;
43  
44      /** Simple constructor.
45       * @param transform fixed transform
46       */
47      public FixedTransformProvider(final Transform transform) {
48          this.transform = transform;
49          this.cached    = new HashMap<>();
50      }
51  
52      /** {@inheritDoc} */
53      public Transform getTransform(final AbsoluteDate date) {
54          return transform;
55      }
56  
57      /** {@inheritDoc} */
58      @Override
59      public <T extends CalculusFieldElement<T>> FieldTransform<T> getTransform(final FieldAbsoluteDate<T> date) {
60  
61          @SuppressWarnings("unchecked")
62          final FieldTransform<T> ft =
63                  (FieldTransform<T>) cached.computeIfAbsent(date.getField(),
64                                                             f -> new FieldTransform<>((Field<T>) f, transform));
65  
66          return ft;
67  
68      }
69  
70      /** Replace the instance with a data transfer object for serialization.
71       * <p>
72       * This intermediate class serializes nothing.
73       * </p>
74       * @return data transfer object that will be serialized
75       */
76      private Object writeReplace() {
77          return new DataTransferObject(transform);
78      }
79  
80      /** Internal class used only for serialization. */
81      private static class DataTransferObject implements Serializable {
82  
83          /** Serializable UID. */
84          private static final long serialVersionUID = 20170106L;
85  
86          /** Fixed transform. */
87          private final Transform transform;
88  
89          /** Simple constructor.
90           * @param transform fixed transform
91           */
92          private DataTransferObject(final Transform transform) {
93              this.transform = transform;
94          }
95  
96          /** Replace the deserialized data transfer object with a {@link FixedTransformProvider}.
97           * @return replacement {@link FixedTransformProvider}
98           */
99          private Object readResolve() {
100             return new FixedTransformProvider(transform);
101         }
102 
103     }
104 
105 }