AmbiguityCache.java

  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. import org.hipparchus.util.Precision;

  19. import java.util.HashMap;
  20. import java.util.Map;

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

  26.     /** Default cache.
  27.      * @deprecated this default cache is only a temporary hack for compatibility purposes
  28.      * it will be removed in Orekit 13.0
  29.      */
  30.     @Deprecated
  31.     public static final AmbiguityCache DEFAULT_CACHE = new AmbiguityCache();

  32.     /** Cache map. */
  33.     private final Map<Key, AmbiguityDriver> cache;

  34.     /** Simple constructor.
  35.      */
  36.     public AmbiguityCache() {
  37.         cache = new HashMap<>();
  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.         return cache.computeIfAbsent(new Key(emitter, receiver, wavelength),
  52.                                      k -> new AmbiguityDriver(emitter, receiver, wavelength));
  53.     }

  54.     /** Key for the map. */
  55.     private static class Key {

  56.         /** Emitter id. */
  57.         private final String emitter;

  58.         /** Receiver id. */
  59.         private final String receiver;

  60.         /** Wavelength. */
  61.         private final double wavelength;

  62.         /** Simple constructor.
  63.          * @param emitter emitter id
  64.          * @param receiver receiver id
  65.          * @param wavelength signal wavelength
  66.          */
  67.         Key(final String emitter, final String receiver, final double wavelength) {
  68.             this.emitter    = emitter;
  69.             this.receiver   = receiver;
  70.             this.wavelength = wavelength;
  71.         }

  72.         /** {@inheritDoc} */
  73.         @Override
  74.         public int hashCode() {
  75.             return (emitter.hashCode() ^ receiver.hashCode()) ^ Double.hashCode(wavelength);
  76.         }

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

  87.     }

  88. }