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;
18  
19  import java.util.function.Function;
20  
21  import org.hipparchus.Field;
22  import org.hipparchus.CalculusFieldElement;
23  import org.orekit.propagation.FieldSpacecraftState;
24  import org.orekit.propagation.events.handlers.ContinueOnEvent;
25  import org.orekit.propagation.events.handlers.FieldContinueOnEvent;
26  import org.orekit.propagation.events.handlers.FieldEventHandler;
27  import org.orekit.propagation.events.intervals.FieldAdaptableInterval;
28  
29  /**
30   * A detector that implements the {@link #g(FieldSpacecraftState)} function using a lambda
31   * that can be set using {@link #withFunction(Function)}.
32   *
33   * <p>For example, to create a simple date detector use:
34   *
35   * <pre>
36   * FieldFunctionalDetector&lt;T&gt; d = new FieldFunctionalDetector&lt;&gt;(field)
37   *     .withGFunction((s) -&gt; s.getDate().durationFrom(triggerDate))
38   *     .withMaxCheck(field.getZero().add(1e10));
39   * </pre>
40   *
41   * @param <T> the type of numbers this detector uses.
42   * @author Evan Ward
43   * @since 10.2
44   */
45  public class FieldFunctionalDetector<T extends CalculusFieldElement<T>>
46          extends FieldAbstractDetector<FieldFunctionalDetector<T>, T> {
47  
48      /** The g function. */
49      private final Function<FieldSpacecraftState<T>, T> function;
50  
51      /**
52       * Create an event detector with the default values. These are {@link
53       * #DEFAULT_MAX_CHECK}, {@link #DEFAULT_THRESHOLD}, {@link #DEFAULT_MAX_ITER}, {@link
54       * ContinueOnEvent}, and a g function that is identically unity.
55       *
56       * @param field on which this detector is defined.
57       */
58      public FieldFunctionalDetector(final Field<T> field) {
59          this(new FieldEventDetectionSettings<>(field, EventDetectionSettings.getDefaultEventDetectionSettings()),
60               new FieldContinueOnEvent<>(), value -> field.getOne());
61      }
62  
63      /**
64       * Protected constructor.
65       *
66       * @param detectionSettings event detection settings
67       * @param handler   event handler to call at event occurrences
68       * @param function  the switching function.
69       * @since 13.0
70       */
71      protected FieldFunctionalDetector(
72              final FieldEventDetectionSettings<T> detectionSettings, final FieldEventHandler<T> handler,
73              final Function<FieldSpacecraftState<T>, T> function) {
74          super(detectionSettings, handler);
75          this.function = function;
76      }
77  
78  
79      @Override
80      public T g(final FieldSpacecraftState<T> s) {
81          return function.apply(s);
82      }
83  
84      @Override
85      protected FieldFunctionalDetector<T> create(
86              final FieldEventDetectionSettings<T> detectionSettings,
87              final FieldEventHandler<T> newHandler) {
88  
89          return new FieldFunctionalDetector<>(detectionSettings, newHandler, function);
90      }
91  
92      /**
93       * Create a new event detector with a new g function, keeping all other attributes the
94       * same. It is recommended to use {@link #withMaxCheck(FieldAdaptableInterval)} and {@link
95       * #withThreshold(CalculusFieldElement)} to set appropriate values for this g function.
96       *
97       * @param newGFunction the new g function.
98       * @return a new detector with the new g function.
99       */
100     public FieldFunctionalDetector<T> withFunction(
101             final Function<FieldSpacecraftState<T>, T> newGFunction) {
102         return new FieldFunctionalDetector<>(getDetectionSettings(), getHandler(), newGFunction);
103     }
104 
105     /**
106      * Get the switching function.
107      *
108      * @return the function used in {@link #g(FieldSpacecraftState)}.
109      */
110     public Function<FieldSpacecraftState<T>, T> getFunction() {
111         return function;
112     }
113 
114 }