FieldFunctionalDetector.java

/* Contributed in the public domain.
 * Licensed to CS Group (CS) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * CS licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.orekit.propagation.events;

import java.util.function.Function;

import org.hipparchus.Field;
import org.hipparchus.CalculusFieldElement;
import org.orekit.propagation.FieldSpacecraftState;
import org.orekit.propagation.events.handlers.ContinueOnEvent;
import org.orekit.propagation.events.handlers.FieldContinueOnEvent;
import org.orekit.propagation.events.handlers.FieldEventHandler;
import org.orekit.propagation.events.intervals.FieldAdaptableInterval;

/**
 * A detector that implements the {@link #g(FieldSpacecraftState)} function using a lambda
 * that can be set using {@link #withFunction(Function)}.
 *
 * <p>For example, to create a simple date detector use:
 *
 * <pre>
 * FieldFunctionalDetector&lt;T&gt; d = new FieldFunctionalDetector&lt;&gt;(field)
 *     .withGFunction((s) -&gt; s.getDate().durationFrom(triggerDate))
 *     .withMaxCheck(field.getZero().add(1e10));
 * </pre>
 *
 * @param <T> the type of numbers this detector uses.
 * @author Evan Ward
 * @since 10.2
 */
public class FieldFunctionalDetector<T extends CalculusFieldElement<T>>
        extends FieldAbstractDetector<FieldFunctionalDetector<T>, T> {

    /** The g function. */
    private final Function<FieldSpacecraftState<T>, T> function;

    /**
     * Create an event detector with the default values. These are {@link
     * #DEFAULT_MAX_CHECK}, {@link #DEFAULT_THRESHOLD}, {@link #DEFAULT_MAX_ITER}, {@link
     * ContinueOnEvent}, and a g function that is identically unity.
     *
     * @param field on which this detector is defined.
     */
    public FieldFunctionalDetector(final Field<T> field) {
        this(new FieldEventDetectionSettings<>(field, EventDetectionSettings.getDefaultEventDetectionSettings()),
             new FieldContinueOnEvent<>(), value -> field.getOne());
    }

    /**
     * Protected constructor.
     *
     * @param detectionSettings event detection settings
     * @param handler   event handler to call at event occurrences
     * @param function  the switching function.
     * @since 13.0
     */
    protected FieldFunctionalDetector(
            final FieldEventDetectionSettings<T> detectionSettings, final FieldEventHandler<T> handler,
            final Function<FieldSpacecraftState<T>, T> function) {
        super(detectionSettings, handler);
        this.function = function;
    }


    @Override
    public T g(final FieldSpacecraftState<T> s) {
        return function.apply(s);
    }

    @Override
    protected FieldFunctionalDetector<T> create(
            final FieldEventDetectionSettings<T> detectionSettings,
            final FieldEventHandler<T> newHandler) {

        return new FieldFunctionalDetector<>(detectionSettings, newHandler, function);
    }

    /**
     * Create a new event detector with a new g function, keeping all other attributes the
     * same. It is recommended to use {@link #withMaxCheck(FieldAdaptableInterval)} and {@link
     * #withThreshold(CalculusFieldElement)} to set appropriate values for this g function.
     *
     * @param newGFunction the new g function.
     * @return a new detector with the new g function.
     */
    public FieldFunctionalDetector<T> withFunction(
            final Function<FieldSpacecraftState<T>, T> newGFunction) {
        return new FieldFunctionalDetector<>(getDetectionSettings(), getHandler(), newGFunction);
    }

    /**
     * Get the switching function.
     *
     * @return the function used in {@link #g(FieldSpacecraftState)}.
     */
    public Function<FieldSpacecraftState<T>, T> getFunction() {
        return function;
    }

}