1   /* Copyright 2002-2024 Luc Maisonobe
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.estimation.measurements.gnss;
18  
19  import org.hipparchus.util.Precision;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  /** Cache for {@link AmbiguityDriver}.
25   * @author Luc Maisonobe
26   * @since 12.1
27   */
28  public class AmbiguityCache {
29  
30      /** Default cache.
31       * @deprecated this default cache is only a temporary hack for compatibility purposes
32       * it will be removed in Orekit 13.0
33       */
34      @Deprecated
35      public static final AmbiguityCache DEFAULT_CACHE = new AmbiguityCache();
36  
37      /** Cache map. */
38      private final Map<Key, AmbiguityDriver> cache;
39  
40      /** Simple constructor.
41       */
42      public AmbiguityCache() {
43          cache = new HashMap<>();
44      }
45  
46      /** Get a cached driver for ambiguity.
47       * <p>
48       * A new parameter driver is created and cached the first time an
49       * emitter/receiver/wavelength triplet is used; after that, the cached
50       * driver will be returned when the same triplet is passed again
51       * </p>
52       * @param emitter emitter id
53       * @param receiver receiver id
54       * @param wavelength signal wavelength
55       * @return parameter driver for the emitter/receiver/wavelength triplet
56       */
57      public AmbiguityDriver getAmbiguity(final String emitter, final String receiver, final double wavelength) {
58          return cache.computeIfAbsent(new Key(emitter, receiver, wavelength),
59                                       k -> new AmbiguityDriver(emitter, receiver, wavelength));
60      }
61  
62      /** Key for the map. */
63      private static class Key {
64  
65          /** Emitter id. */
66          private final String emitter;
67  
68          /** Receiver id. */
69          private final String receiver;
70  
71          /** Wavelength. */
72          private final double wavelength;
73  
74          /** Simple constructor.
75           * @param emitter emitter id
76           * @param receiver receiver id
77           * @param wavelength signal wavelength
78           */
79          Key(final String emitter, final String receiver, final double wavelength) {
80              this.emitter    = emitter;
81              this.receiver   = receiver;
82              this.wavelength = wavelength;
83          }
84  
85          /** {@inheritDoc} */
86          @Override
87          public int hashCode() {
88              return (emitter.hashCode() ^ receiver.hashCode()) ^ Double.hashCode(wavelength);
89          }
90  
91          /** {@inheritDoc} */
92          @Override
93          public boolean equals(final Object object) {
94              if (object instanceof Key) {
95                  final Key other = (Key) object;
96                  return emitter.equals(other.emitter) && receiver.equals(other.receiver) &&
97                         Precision.equals(wavelength, other.wavelength, 1);
98              }
99              return false;
100         }
101 
102     }
103 
104 }