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