1   /* Copyright 2022-2025 Romain Serra
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  
18  package org.orekit.propagation.events;
19  
20  import org.orekit.propagation.SpacecraftState;
21  import org.orekit.propagation.events.handlers.EventHandler;
22  import org.orekit.propagation.events.intervals.DateDetectionAdaptableIntervalFactory;
23  import org.orekit.time.AbsoluteDate;
24  import org.orekit.time.TimeInterval;
25  
26  
27  /**
28   * Detector for time intervals. Positive whenever the date is inside, negative otherwise.
29   *
30   * @author Romain Serra
31   * @since 13.1
32   * @see TimeInterval
33   */
34  public class TimeIntervalDetector extends AbstractDetector<TimeIntervalDetector> {
35  
36      /** Time interval for detection. */
37      private final TimeInterval timeInterval;
38  
39      /**
40       * Constructor with default detection settings.
41       * @param handler event handler
42       * @param timeInterval time interval
43       */
44      public TimeIntervalDetector(final EventHandler handler, final TimeInterval timeInterval) {
45          this(new EventDetectionSettings(DateDetectionAdaptableIntervalFactory.getDatesDetectionInterval(timeInterval.getStartDate(), timeInterval.getEndDate()),
46                  DateDetector.DEFAULT_THRESHOLD, DEFAULT_MAX_ITER), handler, timeInterval);
47      }
48  
49      /**
50       * Constructor.
51       * @param detectionSettings event detection settings
52       * @param handler event handler
53       * @param timeInterval time interval
54       */
55      public TimeIntervalDetector(final EventDetectionSettings detectionSettings, final EventHandler handler,
56                                  final TimeInterval timeInterval) {
57          super(detectionSettings, handler);
58          this.timeInterval = timeInterval;
59      }
60  
61      /**
62       * Getter for the time interval.
63       * @return interval
64       */
65      public TimeInterval getTimeInterval() {
66          return timeInterval;
67      }
68  
69      @Override
70      protected TimeIntervalDetector create(final EventDetectionSettings detectionSettings, final EventHandler newHandler) {
71          return new TimeIntervalDetector(detectionSettings, newHandler, timeInterval);
72      }
73  
74      /** {@inheritDoc} */
75      @Override
76      public boolean dependsOnTimeOnly() {
77          return true;
78      }
79  
80      @Override
81      public double g(final SpacecraftState s) {
82          final AbsoluteDate date = s.getDate();
83          return (date.durationFrom(timeInterval.getStartDate())) * (timeInterval.getEndDate().durationFrom(date));
84      }
85  }