ComparableMeasurement.java

  1. /* Copyright 2002-2018 CS Systèmes d'Information
  2.  * Licensed to CS Systèmes d'Information (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 thei type.
  20.  * @author Luc Maisonobe
  21.      * @since 9.2
  22.  */
  23. public interface ComparableMeasurement extends TimeStamped, Comparable<ComparableMeasurement> {

  24.     /** Get the observed value.
  25.      * <p>
  26.      * The observed value is the value that was measured by the instrument.
  27.      * </p>
  28.      * @return observed value (array of size {@link #getDimension()}
  29.      */
  30.     double[] getObservedValue();

  31.     /** {@inheritDoc}
  32.      * <p>
  33.      * Measurements comparison is primarily chronological, but measurements
  34.      * with the same date are sorted based on the observed value. Even if they
  35.      * have the same value too, they will <em>not</em> be considered equal if they
  36.      * correspond to different instances. This allows to store measurements in
  37.      * {@link java.util.SortedSet SortedSet} without losing any measurements, even
  38.      * redundant ones.
  39.      * </p>
  40.      */
  41.     @Override
  42.     default int compareTo(final ComparableMeasurement other) {

  43.         if (this == other) {
  44.             // only case where measurements are considered equal
  45.             return 0;
  46.         }

  47.         int result = getDate().compareTo(other.getDate());
  48.         if (result == 0) {
  49.             // simultaneous measurements, we compare values
  50.             final double[] thisV  = getObservedValue();
  51.             final double[] otherV = other.getObservedValue();
  52.             if (thisV.length > otherV.length) {
  53.                 result = +1;
  54.             } else if (thisV.length < otherV.length) {
  55.                 result = 1;
  56.             } else {
  57.                 for (int i = 0; i < thisV.length && result == 0; ++i) {
  58.                     result = Double.compare(thisV[i], otherV[i]);
  59.                 }
  60.                 if (result == 0) {
  61.                     // measurements have the same value,
  62.                     // but we do not want them to appear as equal
  63.                     // we set up an arbitrary order
  64.                     result = -1;
  65.                 }
  66.             }
  67.         }

  68.         return result;

  69.     }

  70. }