1   /* Copyright 2002-2024 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  
19  import org.orekit.time.TimeStamped;
20  
21  
22  /** Base interface for comparing measurements regardless of their type.
23   * @author Luc Maisonobe
24   * @since 9.2
25   */
26  public interface ComparableMeasurement extends TimeStamped, Comparable<ComparableMeasurement> {
27  
28      /** Get the observed value.
29       * <p>
30       * The observed value is the value that was measured by the instrument.
31       * </p>
32       * @return observed value
33       */
34      double[] getObservedValue();
35  
36      /** {@inheritDoc}
37       * <p>
38       * Measurements comparison is primarily chronological, but measurements
39       * with the same date are sorted based on the observed value. Even if they
40       * have the same value too, they will <em>not</em> be considered equal if they
41       * correspond to different instances. This allows to store measurements in
42       * {@link java.util.SortedSet SortedSet} without losing any measurements, even
43       * redundant ones.
44       * </p>
45       */
46      @Override
47      default int compareTo(final ComparableMeasurement other) {
48  
49          if (this == other) {
50              // only case where measurements are considered equal
51              return 0;
52          }
53  
54          // Compare date first
55          int result = getDate().compareTo(other.getDate());
56          if (result == 0) {
57              // Simultaneous measurements, we compare the size of the measurements
58              final double[] thisV  = getObservedValue();
59              final double[] otherV = other.getObservedValue();
60  
61              // "Bigger" measurements after "smaller" measurement
62              if (thisV.length > otherV.length) {
63                  result = +1;
64              } else if (thisV.length < otherV.length) {
65                  result = -1;
66              } else {
67                  // Measurements have same size
68                  // Compare the first different value
69                  // "Bigger" measurements after "smaller" measurement
70                  for (int i = 0; i < thisV.length && result == 0; ++i) {
71                      result = Double.compare(thisV[i], otherV[i]);
72                  }
73                  if (result == 0) {
74                      // Measurements have the same value,
75                      // but we do not want them to appear as equal
76                      // we set up an arbitrary order
77                      result = -1;
78                  }
79              }
80          }
81  
82          return result;
83  
84      }
85  
86  }