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  
19  import org.hipparchus.ode.events.Action;
20  import org.orekit.propagation.SpacecraftState;
21  import org.orekit.propagation.events.EventDetector;
22  
23  import java.util.ArrayList;
24  import java.util.Collections;
25  import java.util.List;
26  
27  /**
28   * Handler that will record every time an event occurs and always return {@link
29   * Action#CONTINUE}.
30   *
31   * <p> As this handler stores all observed events it may consume large amounts
32   * of memory depending on the duration of propagation and the frequency of
33   * events.
34   *
35   * @param <T> the type of {@link EventDetector} that this event handler will
36   *            handle events for.
37   * @author Evan Ward
38   */
39  public class RecordAndContinue<T extends EventDetector>
40          implements EventHandler<T> {
41  
42      /** A single event detected during propagation. */
43      public static class Event<T> {
44  
45          /** The observed state. */
46          private final SpacecraftState state;
47          /** The detector. */
48          private final T detector;
49          /** The sign of the derivative of the g function. */
50          private final boolean increasing;
51  
52          /**
53           * Create a new event.
54           *
55           * @param detector   of the event.
56           * @param state      of the event.
57           * @param increasing if the g function is increasing.
58           */
59          private Event(final T detector,
60                        final SpacecraftState state,
61                        final boolean increasing) {
62              this.detector = detector;
63              this.state = state;
64              this.increasing = increasing;
65          }
66  
67          /**
68           * Get the detector.
69           *
70           * @return the detector that found the event.
71           * @see EventHandler#eventOccurred(SpacecraftState, EventDetector,
72           * boolean)
73           */
74          public T getDetector() {
75              return detector;
76          }
77  
78          /**
79           * Is the g() function increasing?
80           *
81           * @return if the sign of the derivative of the g function is positive
82           * (true) or negative (false).
83           * @see EventHandler#eventOccurred(SpacecraftState, EventDetector,
84           * boolean)
85           */
86          public boolean isIncreasing() {
87              return increasing;
88          }
89  
90          /**
91           * Get the spacecraft's state at the event.
92           *
93           * @return the satellite's state when the event was triggered.
94           * @see EventHandler#eventOccurred(SpacecraftState, EventDetector,
95           * boolean)
96           */
97          public SpacecraftState getState() {
98              return state;
99          }
100 
101         @Override
102         public String toString() {
103             return "Event{" +
104                     "state=" + state +
105                     ", increasing=" + increasing +
106                     ", detector=" + detector +
107                     '}';
108         }
109     }
110 
111     /** Observed events. */
112     private final List<Event<T>> events;
113 
114     /** Create a new handler using an {@link ArrayList} to store events. */
115     public RecordAndContinue() {
116         this(new ArrayList<>());
117     }
118 
119     /**
120      * Create a handler using the given collection to store events.
121      *
122      * @param events collection.
123      */
124     public RecordAndContinue(final List<Event<T>> events) {
125         this.events = events;
126     }
127 
128     /**
129      * Get the events passed to this handler.
130      *
131      * <p> Note the returned list of events is in the order the events were
132      * passed to this handler by calling {@link #eventOccurred(SpacecraftState,
133      * EventDetector, boolean)}. This may or may not be chronological order.
134      *
135      * <p> Also not that this method returns a view of the internal collection
136      * used to store events and calling any of this handler's methods may modify
137      * both the underlying collection and the returned view. If a snapshot of
138      * the events up to a certain point is needed create a copy of the returned
139      * collection.
140      *
141      * @return the events observed by the handler in the order they were
142      * observed.
143      */
144     public List<Event<T>> getEvents() {
145         return Collections.unmodifiableList(this.events);
146     }
147 
148     /** Clear all stored events. */
149     public void clear() {
150         this.events.clear();
151     }
152 
153     @Override
154     public Action eventOccurred(final SpacecraftState s,
155                                 final T detector,
156                                 final boolean increasing) {
157         events.add(new Event<T>(detector, s, increasing));
158         return Action.CONTINUE;
159     }
160 
161 }