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  package org.orekit.propagation.events.handlers;
18  
19  import org.hipparchus.ode.events.Action;
20  import org.orekit.propagation.SpacecraftState;
21  import org.orekit.propagation.events.EventDetector;
22  import org.orekit.time.AbsoluteDate;
23  
24  /**
25   * Event handler wrapping another, arbitrary one whilst remembering date of last detection.
26   * If never used, the cache is null.
27   * If used but nothing detected, it returns past infinity in case of forward propagation and future infinity otherwise.
28   * @author Romain Serra
29   * @see RecordAndContinue
30   * @since 12.1
31   */
32  public class RecallLastOccurrence implements EventHandler {
33  
34      /** Wrapped event handler. */
35      private final EventHandler wrappedHandler;
36  
37      /** Last date at which the wrapped event occurred. */
38      private AbsoluteDate lastOccurrence;
39  
40      /** Constructor.
41       * @param wrappedHandler event handler to wrap
42       */
43      public RecallLastOccurrence(final EventHandler wrappedHandler) {
44          this.wrappedHandler = wrappedHandler;
45      }
46  
47      /** Getter for last occurrence.
48       * @return last date when underlying event was detected
49       */
50      public AbsoluteDate getLastOccurrence() {
51          return lastOccurrence;
52      }
53  
54      /** {@inheritDoc} */
55      @Override
56      public void init(final SpacecraftState initialState, final AbsoluteDate target, final EventDetector detector) {
57          final boolean isForward = target.isAfter(initialState.getDate());
58          lastOccurrence = isForward ? AbsoluteDate.PAST_INFINITY : AbsoluteDate.FUTURE_INFINITY;
59          wrappedHandler.init(initialState, target, detector);
60      }
61  
62      /** {@inheritDoc} */
63      @Override
64      public Action eventOccurred(final SpacecraftState s, final EventDetector detector, final boolean increasing) {
65          lastOccurrence = s.getDate();
66          return wrappedHandler.eventOccurred(s, detector, increasing);
67      }
68  
69      /** {@inheritDoc} */
70      @Override
71      public SpacecraftState resetState(final EventDetector detector, final SpacecraftState oldState) {
72          return wrappedHandler.resetState(detector, oldState);
73      }
74  
75      /** {@inheritDoc} */
76      @Override
77      public void finish(final SpacecraftState finalState, final EventDetector detector) {
78          wrappedHandler.finish(finalState, detector);
79      }
80  }