MinSelector.java

  1. /* Copyright 2013-2022 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. package org.orekit.rugged.utils;

  18. /** Selector for min value.
  19.  * <p>
  20.  * This selector considers {@code Double.NaN} values correspond
  21.  * to non-initialized data that should be ignored rather than
  22.  * selected.
  23.  * </p>
  24.  * @see MaxSelector
  25.  * @author Luc Maisonobe
  26.  */
  27. public class MinSelector extends Selector {

  28.     /** Private constructor for singleton.
  29.      */
  30.     private MinSelector() {
  31.     }

  32.     /** Get the unique instance.
  33.      * @return unique instance of the min selector.
  34.      */
  35.     public static MinSelector getInstance() {
  36.         return LazyHolder.INSTANCE;
  37.     }

  38.     /** Check if first value should be selected.
  39.      * @param v1 first value
  40.      * @param v2 second value
  41.      * @return true if v1 is lower than v2, or if v2 is {@code Double.NaN}
  42.      */
  43.     @Override
  44.     public boolean selectFirst(final double v1, final double v2) {
  45.         return v1 < v2 || Double.isNaN(v2);
  46.     }

  47.     /** Holder for the min selector singleton.
  48.      * <p>
  49.      * We use the Initialization On Demand Holder Idiom to store
  50.      * the singletons, as it is both thread-safe, efficient (no
  51.      * synchronization) and works with all versions of java.
  52.      * </p>
  53.      */
  54.     private static class LazyHolder {

  55.         /** Unique instance. */
  56.         private static final MinSelector INSTANCE = new MinSelector();

  57.         /** Private constructor.
  58.          * <p>This class is a utility class, it should neither have a public
  59.          * nor a default constructor. This private constructor prevents
  60.          * the compiler from generating one automatically.</p>
  61.          */
  62.         private LazyHolder() {
  63.         }

  64.     }

  65. }