ComparableMeasurement.java

  1. /* Copyright 2002-2025 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.estimation.measurements;

  18. import org.orekit.time.TimeStamped;


  19. /** Base interface for comparing measurements regardless of their type.
  20.  * @author Luc Maisonobe
  21.  * @author Evan M. Ward
  22.  * @since 9.2
  23.  */
  24. public interface ComparableMeasurement extends TimeStamped, Comparable<ComparableMeasurement> {

  25.     /** Get the observed value.
  26.      * <p>
  27.      * The observed value is the value that was measured by the instrument.
  28.      * </p>
  29.      * @return observed value
  30.      */
  31.     double[] getObservedValue();

  32.     /** Set the observed value.
  33.      * <p>
  34.      * The observed value is the value that was measured by the instrument.
  35.      * </p>
  36.      * @param newObserved observed value
  37.      * @since 13.0
  38.      */
  39.     void setObservedValue(double[] newObserved);

  40.     /**
  41.      * {@inheritDoc}
  42.      *
  43.      * <p>Measurements comparison is primarily chronological, but measurements with
  44.      * the same date are sorted based on the observed value. Even if they have
  45.      * the same value too, they will <em>likely</em> not be considered equal if
  46.      * they correspond to different instances.
  47.      *
  48.      * <p>Care should be taken before storing measurements in a
  49.      * {@link java.util.SortedSet SortedSet} as it may lose redundant
  50.      * measurements if they, by chance, have the same identity hash code.
  51.      *
  52.      * @see System#identityHashCode(Object)
  53.      */
  54.     @Override
  55.     default int compareTo(final ComparableMeasurement other) {

  56.         if (this == other) {
  57.             // quick return for comparing a measurement to itself
  58.             return 0;
  59.         }

  60.         // Compare date first
  61.         int result = getDate().compareTo(other.getDate());
  62.         if (result != 0) {
  63.             return result;
  64.         }

  65.         // Simultaneous measurements, we compare the size of the measurements
  66.         final double[] thisV  = getObservedValue();
  67.         final double[] otherV = other.getObservedValue();
  68.         // "Bigger" measurements after "smaller" measurement
  69.         if (thisV.length > otherV.length) {
  70.             return +1;
  71.         } else if (thisV.length < otherV.length) {
  72.             return -1;
  73.         }

  74.         // Measurements have same size
  75.         // Compare the first different value
  76.         // "Bigger" measurements after "smaller" measurement
  77.         for (int i = 0; i < thisV.length; ++i) {
  78.             result = Double.compare(thisV[i], otherV[i]);
  79.             if (result != 0) {
  80.                 return result;
  81.             }
  82.         }

  83.         // Measurements have the same value,
  84.         // but we do not want them to appear as equal
  85.         // we set up an arbitrary order based on hash code
  86.         result = Integer.compare(this.hashCode(), other.hashCode());
  87.         if (result != 0) {
  88.             return result;
  89.         }
  90.         // next try identity hash code
  91.         result = Integer.compare(
  92.                 System.identityHashCode(this),
  93.                 System.identityHashCode(other));
  94.         // Tried all the fields to compare, and though we want an arbitrary
  95.         // total order, we still must obey the contract of compareTo, see #1364.
  96.         // So this may return zero if this==other, or for equal objects that by
  97.         // chance have the same identity hash code.
  98.         return result;

  99.     }

  100. }