FieldRecallLastOccurrence.java

  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. import org.hipparchus.CalculusFieldElement;
  19. import org.hipparchus.Field;
  20. import org.hipparchus.ode.events.Action;
  21. import org.orekit.propagation.FieldSpacecraftState;
  22. import org.orekit.propagation.events.FieldEventDetector;
  23. import org.orekit.time.AbsoluteDate;
  24. import org.orekit.time.FieldAbsoluteDate;

  25. /**
  26.  * Event handler wrapping another, arbitrary one whilst remembering date of last detection.
  27.  * If never used, the cache is null.
  28.  * If used but nothing detected, it returns past infinity in case of forward propagation and future infinity otherwise.
  29.  * @author Romain Serra
  30.  * @see RecallLastOccurrence
  31.  * @since 12.1
  32.  * @param <T> field type
  33.  */
  34. public class FieldRecallLastOccurrence<T extends CalculusFieldElement<T>> implements FieldEventHandler<T> {

  35.     /** Wrapped event handler. */
  36.     private final FieldEventHandler<T> wrappedHandler;

  37.     /** Last date at which the wrapped event occurred. */
  38.     private FieldAbsoluteDate<T> lastOccurrence;

  39.     /** Constructor.
  40.      * @param wrappedHandler event handler to wrap
  41.      */
  42.     public FieldRecallLastOccurrence(final FieldEventHandler<T> wrappedHandler) {
  43.         this.wrappedHandler = wrappedHandler;
  44.     }

  45.     /** Getter for last occurrence.
  46.      * @return last date when underlying event was detected
  47.      */
  48.     public FieldAbsoluteDate<T> getLastOccurrence() {
  49.         return lastOccurrence;
  50.     }

  51.     /** {@inheritDoc} */
  52.     @Override
  53.     public void init(final FieldSpacecraftState<T> initialState, final FieldAbsoluteDate<T> target,
  54.                      final FieldEventDetector<T> detector) {
  55.         final boolean isForward = target.isAfter(initialState.getDate());
  56.         final Field<T> field = target.getField();
  57.         final AbsoluteDate date = isForward ? AbsoluteDate.PAST_INFINITY : AbsoluteDate.FUTURE_INFINITY;
  58.         lastOccurrence = new FieldAbsoluteDate<>(field, date);
  59.         wrappedHandler.init(initialState, target, detector);
  60.     }

  61.     /** {@inheritDoc} */
  62.     @Override
  63.     public Action eventOccurred(final FieldSpacecraftState<T> s, final FieldEventDetector<T> detector,
  64.                                 final boolean increasing) {
  65.         lastOccurrence = s.getDate();
  66.         return wrappedHandler.eventOccurred(s, detector, increasing);
  67.     }

  68.     /** {@inheritDoc} */
  69.     @Override
  70.     public FieldSpacecraftState<T> resetState(final FieldEventDetector<T> detector,
  71.                                               final FieldSpacecraftState<T> oldState) {
  72.         return wrappedHandler.resetState(detector, oldState);
  73.     }

  74.     /** {@inheritDoc} */
  75.     @Override
  76.     public void finish(final FieldSpacecraftState<T> finalState, final FieldEventDetector<T> detector) {
  77.         wrappedHandler.finish(finalState, detector);
  78.     }
  79. }