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