1   /* Copyright 2022-2025 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.generation;
18  
19  import java.util.Collections;
20  import java.util.Comparator;
21  import java.util.SortedSet;
22  import java.util.TreeSet;
23  
24  import org.orekit.estimation.measurements.ComparableMeasurement;
25  import org.orekit.estimation.measurements.EstimatedMeasurementBase;
26  import org.orekit.time.AbsoluteDate;
27  
28  
29  /** Subscriber that gather all generated measurements in a sorted set.
30   * @author Luc Maisonobe
31   * @since 12.0
32   */
33  public class GatheringSubscriber implements GeneratedMeasurementSubscriber {
34  
35      /** Set for holding measurements. */
36      private SortedSet<EstimatedMeasurementBase<?>> measurements;
37  
38      /** Simple constructor.
39       */
40      public GatheringSubscriber() {
41          measurements = Collections.emptySortedSet();
42      }
43  
44      /** {@inheritDoc} */
45      @Override
46      public void init(final AbsoluteDate start, final AbsoluteDate end) {
47          final Comparator<EstimatedMeasurementBase<?>> comparator = end.isAfterOrEqualTo(start) ?
48                                                                     Comparator.naturalOrder() :
49                                                                     Comparator.reverseOrder();
50          measurements = new TreeSet<>(comparator);
51      }
52  
53      /** {@inheritDoc} */
54      @Override
55      public void handleGeneratedMeasurement(final EstimatedMeasurementBase<?> measurement) {
56          measurements.add(measurement);
57      }
58  
59      /** Get generated measurements.
60       * <p>
61       * The measurements are sorted according to {@link ComparableMeasurement} if
62       * generation was chronological, or reversed {@link ComparableMeasurement} if
63       * generation was non-chronological.
64       * </p>
65       * @return unmodifiable view of generated measurements
66       */
67      public SortedSet<EstimatedMeasurementBase<?>> getGeneratedMeasurements() {
68          return Collections.unmodifiableSortedSet(measurements);
69      }
70  
71  }