RecordAndContinue.java

  1. /* Contributed in the public domain.
  2.  * Licensed to CS Systèmes d'Information (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.orekit.propagation.SpacecraftState;
  19. import org.orekit.propagation.events.EventDetector;

  20. import java.util.ArrayList;
  21. import java.util.Collections;
  22. import java.util.List;

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

  37.     /** A single event detected during propagation. */
  38.     public static class Event<T> {

  39.         /** The observed state. */
  40.         private final SpacecraftState state;
  41.         /** The detector. */
  42.         private final T detector;
  43.         /** The sign of the derivative of the g function. */
  44.         private final boolean increasing;

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

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

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

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

  90.     }

  91.     /** Observed events. */
  92.     private final List<Event<T>> events;

  93.     /** Create a new handler using an {@link ArrayList} to store events. */
  94.     public RecordAndContinue() {
  95.         this(new ArrayList<>());
  96.     }

  97.     /**
  98.      * Create a handler using the given collection to store events.
  99.      *
  100.      * @param events collection.
  101.      */
  102.     public RecordAndContinue(final List<Event<T>> events) {
  103.         this.events = events;
  104.     }

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

  124.     /** Clear all stored events. */
  125.     public void clear() {
  126.         this.events.clear();
  127.     }

  128.     @Override
  129.     public Action eventOccurred(final SpacecraftState s,
  130.                                 final T detector,
  131.                                 final boolean increasing) {
  132.         events.add(new Event<T>(detector, s, increasing));
  133.         return Action.CONTINUE;
  134.     }

  135.     @Override
  136.     public SpacecraftState resetState(final T detector,
  137.                                       final SpacecraftState oldState) {
  138.         return null;
  139.     }

  140. }