RecordAndContinue.java

  1. /* Contributed in the public domain.
  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 java.util.ArrayList;
  22. import java.util.Collections;
  23. import java.util.List;

  24. /**
  25.  * Handler that will record every time an event occurs and always return {@link
  26.  * Action#CONTINUE}.
  27.  *
  28.  * <p> As this handler stores all observed events it may consume large amounts
  29.  * of memory depending on the duration of propagation and the frequency of
  30.  * events.
  31.  *
  32.  * @author Evan Ward
  33.  */
  34. public class RecordAndContinue implements EventHandler {

  35.     /** A single event detected during propagation. */
  36.     public static class Event {

  37.         /** The observed state. */
  38.         private final SpacecraftState state;
  39.         /** The detector. */
  40.         private final EventDetector detector;
  41.         /** The sign of the derivative of the g function. */
  42.         private final boolean increasing;

  43.         /**
  44.          * Create a new event.
  45.          *
  46.          * @param detector   of the event.
  47.          * @param state      of the event.
  48.          * @param increasing if the g function is increasing.
  49.          */
  50.         private Event(final EventDetector detector,
  51.                       final SpacecraftState state,
  52.                       final boolean increasing) {
  53.             this.detector = detector;
  54.             this.state = state;
  55.             this.increasing = increasing;
  56.         }

  57.         /**
  58.          * Get the detector.
  59.          *
  60.          * @return the detector that found the event.
  61.          * @see EventHandler#eventOccurred(SpacecraftState, EventDetector,
  62.          * boolean)
  63.          */
  64.         public EventDetector getDetector() {
  65.             return detector;
  66.         }

  67.         /**
  68.          * Is the g() function increasing?
  69.          *
  70.          * @return if the sign of the derivative of the g function is positive
  71.          * (true) or negative (false).
  72.          * @see EventHandler#eventOccurred(SpacecraftState, EventDetector,
  73.          * boolean)
  74.          */
  75.         public boolean isIncreasing() {
  76.             return increasing;
  77.         }

  78.         /**
  79.          * Get the spacecraft's state at the event.
  80.          *
  81.          * @return the satellite's state when the event was triggered.
  82.          * @see EventHandler#eventOccurred(SpacecraftState, EventDetector,
  83.          * boolean)
  84.          */
  85.         public SpacecraftState getState() {
  86.             return state;
  87.         }

  88.         @Override
  89.         public String toString() {
  90.             return "Event{" +
  91.                     "state=" + state +
  92.                     ", increasing=" + increasing +
  93.                     ", detector=" + detector +
  94.                     '}';
  95.         }
  96.     }

  97.     /** Observed events. */
  98.     private final List<Event> events;

  99.     /** Create a new handler using an {@link ArrayList} to store events. */
  100.     public RecordAndContinue() {
  101.         this(new ArrayList<>());
  102.     }

  103.     /**
  104.      * Create a handler using the given collection to store events.
  105.      *
  106.      * @param events collection.
  107.      */
  108.     public RecordAndContinue(final List<Event> events) {
  109.         this.events = events;
  110.     }

  111.     /**
  112.      * Get the events passed to this handler.
  113.      *
  114.      * <p> Note the returned list of events is in the order the events were
  115.      * passed to this handler by calling {@link #eventOccurred(SpacecraftState,
  116.      * EventDetector, boolean)}. This may or may not be chronological order.
  117.      *
  118.      * <p> Also not that this method returns a view of the internal collection
  119.      * used to store events and calling any of this handler's methods may modify
  120.      * both the underlying collection and the returned view. If a snapshot of
  121.      * the events up to a certain point is needed create a copy of the returned
  122.      * collection.
  123.      *
  124.      * @return the events observed by the handler in the order they were
  125.      * observed.
  126.      */
  127.     public List<Event> getEvents() {
  128.         return Collections.unmodifiableList(this.events);
  129.     }

  130.     /** Clear all stored events. */
  131.     public void clear() {
  132.         this.events.clear();
  133.     }

  134.     @Override
  135.     public Action eventOccurred(final SpacecraftState s,
  136.                                 final EventDetector detector,
  137.                                 final boolean increasing) {
  138.         events.add(new Event(detector, s, increasing));
  139.         return Action.CONTINUE;
  140.     }

  141. }