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.  * @param <T> the type of {@link EventDetector} that this event handler will
  33.  *            handle events for.
  34.  * @author Evan Ward
  35.  */
  36. public class RecordAndContinue<T extends EventDetector>
  37.         implements EventHandler<T> {

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

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

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

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

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

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

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

  100.     /** Observed events. */
  101.     private final List<Event<T>> events;

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

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

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

  133.     /** Clear all stored events. */
  134.     public void clear() {
  135.         this.events.clear();
  136.     }

  137.     @Override
  138.     public Action eventOccurred(final SpacecraftState s,
  139.                                 final T detector,
  140.                                 final boolean increasing) {
  141.         events.add(new Event<T>(detector, s, increasing));
  142.         return Action.CONTINUE;
  143.     }

  144. }