AmbiguityCache.java

  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. import java.util.HashMap;
  19. import java.util.Map;

  20. import org.hipparchus.util.Precision;

  21. /** Cache for {@link AmbiguityDriver}.
  22.  * @author Luc Maisonobe
  23.  * @since 12.1
  24.  */
  25. public class AmbiguityCache {

  26.     /** Cache map. */
  27.     private final Map<Key, AmbiguityDriver> cache;

  28.     /** Simple constructor.
  29.      */
  30.     public AmbiguityCache() {
  31.         cache = new HashMap<>();
  32.     }

  33.     /** Get a cached driver for ambiguity.
  34.      * <p>
  35.      * A new parameter driver is created and cached the first time an
  36.      * emitter/receiver/wavelength triplet is used; after that, the cached
  37.      * driver will be returned when the same triplet is passed again
  38.      * </p>
  39.      * @param emitter emitter id
  40.      * @param receiver receiver id
  41.      * @param wavelength signal wavelength
  42.      * @return parameter driver for the emitter/receiver/wavelength triplet
  43.      */
  44.     public AmbiguityDriver getAmbiguity(final String emitter, final String receiver, final double wavelength) {
  45.         final Key key = new Key(emitter, receiver, wavelength);
  46.         synchronized (cache) {
  47.             return cache.computeIfAbsent(key, k -> new AmbiguityDriver(emitter, receiver, wavelength));
  48.         }
  49.     }

  50.     /** Key for the map. */
  51.     private static class Key {

  52.         /** Emitter id. */
  53.         private final String emitter;

  54.         /** Receiver id. */
  55.         private final String receiver;

  56.         /** Wavelength. */
  57.         private final double wavelength;

  58.         /** Simple constructor.
  59.          * @param emitter emitter id
  60.          * @param receiver receiver id
  61.          * @param wavelength signal wavelength
  62.          */
  63.         Key(final String emitter, final String receiver, final double wavelength) {
  64.             this.emitter    = emitter;
  65.             this.receiver   = receiver;
  66.             this.wavelength = wavelength;
  67.         }

  68.         /** {@inheritDoc} */
  69.         @Override
  70.         public int hashCode() {
  71.             return (emitter.hashCode() ^ receiver.hashCode()) ^ Double.hashCode(wavelength);
  72.         }

  73.         /** {@inheritDoc} */
  74.         @Override
  75.         public boolean equals(final Object object) {
  76.             if (object instanceof Key) {
  77.                 final Key other = (Key) object;
  78.                 return emitter.equals(other.emitter) && receiver.equals(other.receiver) &&
  79.                        Precision.equals(wavelength, other.wavelength, 1);
  80.             }
  81.             return false;
  82.         }

  83.     }

  84. }