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.FieldEventDetector;
27  
28  /**
29   * Handler that will record every time an event occurs and always return {@link
30   * Action#CONTINUE}.
31   *
32   * <p> As this handler stores all observed events it may consume large amounts
33   * of memory depending on the duration of propagation and the frequency of events.
34   * </p>
35   *
36   * @author Evan Ward
37   * @see RecordAndContinue
38   * @since 9.3
39   * @param <T> type of the field element
40   */
41  public class FieldRecordAndContinue <T extends CalculusFieldElement<T>> implements FieldEventHandler<T> {
42  
43      /** A single event detected during propagation.
44       * @param <T> type of the field element
45       */
46      public static class Event<T extends CalculusFieldElement<T>> {
47  
48          /** The observed state. */
49          private final FieldSpacecraftState<T> state;
50          /** The detector. */
51          private final FieldEventDetector<T> detector;
52          /** The sign of the derivative of the g function. */
53          private final boolean increasing;
54  
55          /**
56           * Create a new event.
57           *
58           * @param detector   of the event.
59           * @param state      of the event.
60           * @param increasing if the g function is increasing.
61           */
62          private Event(final FieldEventDetector<T> detector,
63                        final FieldSpacecraftState<T> state,
64                        final boolean increasing) {
65              this.detector = detector;
66              this.state = state;
67              this.increasing = increasing;
68          }
69  
70          /**
71           * Get the detector.
72           *
73           * @return the detector that found the event.
74           */
75          public FieldEventDetector<T> getDetector() {
76              return detector;
77          }
78  
79          /**
80           * Check if the g() function is increasing.
81           *
82           * @return if the sign of the derivative of the g function is positive (true) or
83           * negative (false).
84           */
85          public boolean isIncreasing() {
86              return increasing;
87          }
88  
89          /**
90           * Get the spacecraft's state at the event.
91           *
92           * @return the satellite's state when the event was triggered.
93           */
94          public FieldSpacecraftState<T> 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<T>> events;
110 
111     /** Create a new handler using an {@link ArrayList} to store events. */
112     public FieldRecordAndContinue() {
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 FieldRecordAndContinue(final List<Event<T>> 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(FieldSpacecraftState,
130      * FieldEventDetector, 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 both the
134      * underlying collection and the returned view. If a snapshot of the events up to a
135      * certain point is needed create a copy of the returned collection.
136      *
137      * @return the events observed by the handler in the order they were observed.
138      */
139     public List<Event<T>> getEvents() {
140         return Collections.unmodifiableList(this.events);
141     }
142 
143     /** Clear all stored events. */
144     public void clear() {
145         this.events.clear();
146     }
147 
148     @Override
149     public Action eventOccurred(final FieldSpacecraftState<T> s,
150                                 final FieldEventDetector<T> detector,
151                                 final boolean increasing) {
152         events.add(new Event<>(detector, s, increasing));
153         return Action.CONTINUE;
154     }
155 
156 }