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.CalculusFieldElement;
25  import org.hipparchus.Field;
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      @SuppressWarnings("unchecked")
59      @Override
60      public <T extends CalculusFieldElement<T>> FieldTransform<T> getTransform(final FieldAbsoluteDate<T> date) {
61          synchronized (cached) {
62              return (FieldTransform<T>) cached.computeIfAbsent(date.getField(),
63                                                              f -> new FieldTransform<>((Field<T>) f, transform));
64          }
65      }
66  
67      /** Replace the instance with a data transfer object for serialization.
68       * <p>
69       * This intermediate class serializes nothing.
70       * </p>
71       * @return data transfer object that will be serialized
72       */
73      private Object writeReplace() {
74          return new DataTransferObject(transform);
75      }
76  
77      /** Internal class used only for serialization. */
78      private static class DataTransferObject implements Serializable {
79  
80          /** Serializable UID. */
81          private static final long serialVersionUID = 20170106L;
82  
83          /** Fixed transform. */
84          private final Transform transform;
85  
86          /** Simple constructor.
87           * @param transform fixed transform
88           */
89          private DataTransferObject(final Transform transform) {
90              this.transform = transform;
91          }
92  
93          /** Replace the deserialized data transfer object with a {@link FixedTransformProvider}.
94           * @return replacement {@link FixedTransformProvider}
95           */
96          private Object readResolve() {
97              return new FixedTransformProvider(transform);
98          }
99  
100     }
101 
102 }