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