RecallLastOccurrence.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.ode.events.Action;
  19. import org.orekit.propagation.SpacecraftState;
  20. import org.orekit.propagation.events.EventDetector;
  21. import org.orekit.time.AbsoluteDate;

  22. /**
  23.  * Event handler wrapping another, arbitrary one whilst remembering date of last detection.
  24.  * If never used, the cache is null.
  25.  * If used but nothing detected, it returns past infinity in case of forward propagation and future infinity otherwise.
  26.  * @author Romain Serra
  27.  * @see RecordAndContinue
  28.  * @since 12.1
  29.  */
  30. public class RecallLastOccurrence implements EventHandler {

  31.     /** Wrapped event handler. */
  32.     private final EventHandler wrappedHandler;

  33.     /** Last date at which the wrapped event occurred. */
  34.     private AbsoluteDate lastOccurrence;

  35.     /** Constructor.
  36.      * @param wrappedHandler event handler to wrap
  37.      */
  38.     public RecallLastOccurrence(final EventHandler wrappedHandler) {
  39.         this.wrappedHandler = wrappedHandler;
  40.     }

  41.     /** Getter for last occurrence.
  42.      * @return last date when underlying event was detected
  43.      */
  44.     public AbsoluteDate getLastOccurrence() {
  45.         return lastOccurrence;
  46.     }

  47.     /** {@inheritDoc} */
  48.     @Override
  49.     public void init(final SpacecraftState initialState, final AbsoluteDate target, final EventDetector detector) {
  50.         final boolean isForward = target.isAfter(initialState.getDate());
  51.         lastOccurrence = isForward ? AbsoluteDate.PAST_INFINITY : AbsoluteDate.FUTURE_INFINITY;
  52.         wrappedHandler.init(initialState, target, detector);
  53.     }

  54.     /** {@inheritDoc} */
  55.     @Override
  56.     public Action eventOccurred(final SpacecraftState s, final EventDetector detector, final boolean increasing) {
  57.         lastOccurrence = s.getDate();
  58.         return wrappedHandler.eventOccurred(s, detector, increasing);
  59.     }

  60.     /** {@inheritDoc} */
  61.     @Override
  62.     public SpacecraftState resetState(final EventDetector detector, final SpacecraftState oldState) {
  63.         return wrappedHandler.resetState(detector, oldState);
  64.     }

  65.     /** {@inheritDoc} */
  66.     @Override
  67.     public void finish(final SpacecraftState finalState, final EventDetector detector) {
  68.         wrappedHandler.finish(finalState, detector);
  69.     }
  70. }