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