FieldStepHandlerMultiplexer.java

  1. /* Copyright 2002-2025 CS GROUP
  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.sampling;

  18. import java.util.ArrayList;
  19. import java.util.Collections;
  20. import java.util.Iterator;
  21. import java.util.List;

  22. import org.hipparchus.CalculusFieldElement;
  23. import org.orekit.propagation.FieldSpacecraftState;
  24. import org.orekit.time.FieldAbsoluteDate;

  25. /** This class gathers several {@link OrekitStepHandler} instances into one.
  26.  *
  27.  * @author Luc Maisonobe
  28.  * @param <T> type of the field elements
  29.  */
  30. public class FieldStepHandlerMultiplexer<T extends CalculusFieldElement<T>> implements FieldOrekitStepHandler<T> {

  31.     /** Underlying step handlers. */
  32.     private final List<FieldOrekitStepHandler<T>> handlers;

  33.     /** Target time. */
  34.     private FieldAbsoluteDate<T> target;

  35.     /** Last known state. */
  36.     private FieldSpacecraftState<T> last;

  37.     /** Simple constructor.
  38.      */
  39.     public FieldStepHandlerMultiplexer() {
  40.         handlers = new ArrayList<>();
  41.     }

  42.     /** Add a handler for variable size step.
  43.      * <p>
  44.      * If propagation is ongoing (i.e. global {@link #init(FieldSpacecraftState, FieldAbsoluteDate)
  45.      * init} already called and global {@link #finish(FieldSpacecraftState) finish} not called
  46.      * yet), then the local {@link FieldOrekitStepHandler#init(FieldSpacecraftState, FieldAbsoluteDate)
  47.      * FieldOrekitStepHandler.init} method of the added handler will be called with the last
  48.      * known state, so the handler starts properly.
  49.      * </p>
  50.      * @param handler step handler to add
  51.      */
  52.     public void add(final FieldOrekitStepHandler<T> handler) {
  53.         handlers.add(handler);
  54.         if (last != null) {
  55.             // propagation is ongoing, we need to call init now for this handler
  56.             handler.init(last, target);
  57.         }
  58.     }

  59.     /** Add a handler for fixed size step.
  60.      * <p>
  61.      * If propagation is ongoing (i.e. global {@link #init(FieldSpacecraftState, FieldAbsoluteDate)
  62.      * init} already called and global {@link #finish(FieldSpacecraftState) finish} not called
  63.      * yet), then the local {@link FieldOrekitFixedStepHandler#init(FieldSpacecraftState, FieldAbsoluteDate,
  64.      * CalculusFieldElement) FieldOrekitStepHandler.init} method of the added handler will be
  65.      * called with the last known state, so the handler starts properly.
  66.      * </p>
  67.      * @param h fixed stepsize (s)
  68.      * @param handler handler called at the end of each finalized step
  69.      * @since 11.0
  70.      */
  71.     public void add(final T h, final FieldOrekitFixedStepHandler<T> handler) {
  72.         final FieldOrekitStepHandler<T> normalized = new FieldOrekitStepNormalizer<>(h, handler);
  73.         handlers.add(normalized);
  74.         if (last != null) {
  75.             // propagation is ongoing, we need to call init now for this handler
  76.             normalized.init(last, target);
  77.         }
  78.     }

  79.     /** Get an unmodifiable view of all handlers.
  80.      * <p>
  81.      * Note that if {@link FieldOrekitFixedStepHandler fixed step handlers} have
  82.      * been {@link #add(CalculusFieldElement, FieldOrekitFixedStepHandler)}, then they will
  83.      * show up wrapped within {@link FieldOrekitStepNormalizer step normalizers}.
  84.      * </p>
  85.      * @return an unmodifiable view of all handlers
  86.      * @since 11.0
  87.      */
  88.     public List<FieldOrekitStepHandler<T>> getHandlers() {
  89.         return Collections.unmodifiableList(handlers);
  90.     }

  91.     /** Remove a handler.
  92.      * <p>
  93.      * If propagation is ongoing (i.e. global {@link #init(FieldSpacecraftState, FieldAbsoluteDate)
  94.      * init} already called and global {@link #finish(FieldSpacecraftState) finish} not called
  95.      * yet), then the local {@link FieldOrekitStepHandler#finish(FieldSpacecraftState)
  96.      * FieldOrekitStepHandler.finish} method of the removed handler will be called with the last
  97.      * known state, so the handler stops properly.
  98.      * </p>
  99.      * @param handler step handler to remove
  100.      * @since 11.0
  101.      */
  102.     public void remove(final FieldOrekitStepHandler<T> handler) {
  103.         final Iterator<FieldOrekitStepHandler<T>> iterator = handlers.iterator();
  104.         while (iterator.hasNext()) {
  105.             if (iterator.next() == handler) {
  106.                 if (last != null) {
  107.                     // propagation is ongoing, we need to call finish now for this handler
  108.                     handler.finish(last);
  109.                 }
  110.                 iterator.remove();
  111.                 return;
  112.             }
  113.         }
  114.     }

  115.     /** Remove a handler.
  116.      * <p>
  117.      * If propagation is ongoing (i.e. global {@link #init(FieldSpacecraftState, FieldAbsoluteDate)
  118.      * init} already called and global {@link #finish(FieldSpacecraftState) finish} not called
  119.      * yet), then the local {@link FieldOrekitFixedStepHandler#finish(FieldSpacecraftState)
  120.      * FieldOrekitFixedStepHandler.finish} method of the removed handler will be called with the last
  121.      * known state, so the handler stops properly.
  122.      * </p>
  123.      * @param handler step handler to remove
  124.      * @since 11.0
  125.      */
  126.     public void remove(final FieldOrekitFixedStepHandler<T> handler) {
  127.         final Iterator<FieldOrekitStepHandler<T>> iterator = handlers.iterator();
  128.         while (iterator.hasNext()) {
  129.             final FieldOrekitStepHandler<T> current = iterator.next();
  130.             if (current instanceof FieldOrekitStepNormalizer &&
  131.                 ((FieldOrekitStepNormalizer<T>) current).getFixedStepHandler() == handler) {
  132.                 if (last != null) {
  133.                     // propagation is ongoing, we need to call finish now for this handler
  134.                     current.finish(last);
  135.                 }
  136.                 iterator.remove();
  137.                 return;
  138.             }
  139.         }
  140.     }

  141.     /** Remove all handlers managed by this multiplexer.
  142.      * <p>
  143.      * If propagation is ongoing (i.e. global {@link #init(FieldSpacecraftState, FieldAbsoluteDate)
  144.      * init} already called and global {@link #finish(FieldSpacecraftState) finish} not called
  145.      * yet), then the local {@link FieldOrekitStepHandler#finish(FieldSpacecraftState)
  146.      * FieldOrekitStepHandler.finish} and {@link FieldOrekitFixedStepHandler#finish(FieldSpacecraftState)
  147.      * FieldOrekitFixedStepHandler.finish} methods of the removed handlers will be called with the last
  148.      * known state, so the handlers stop properly.
  149.      * </p>
  150.      * @since 11.0
  151.      */
  152.     public void clear() {
  153.         if (last != null) {
  154.             // propagation is ongoing, we need to call finish now for all handlers
  155.             handlers.forEach(h -> h.finish(last));
  156.         }
  157.         handlers.clear();
  158.     }

  159.     /** {@inheritDoc} */
  160.     public void init(final FieldSpacecraftState<T> s0, final FieldAbsoluteDate<T> t) {
  161.         this.target = t;
  162.         this.last   = s0;
  163.         for (final FieldOrekitStepHandler<T> handler : handlers) {
  164.             handler.init(s0, t);
  165.         }
  166.     }

  167.     /** {@inheritDoc} */
  168.     public void handleStep(final FieldOrekitStepInterpolator<T> interpolator) {
  169.         this.last = interpolator.getCurrentState();
  170.         for (final FieldOrekitStepHandler<T> handler : handlers) {
  171.             handler.handleStep(interpolator);
  172.         }
  173.     }

  174.     /** {@inheritDoc} */
  175.     public void finish(final FieldSpacecraftState<T> finalState) {
  176.         this.target = null;
  177.         this.last   = null;
  178.         for (final FieldOrekitStepHandler<T> handler : handlers) {
  179.             handler.finish(finalState);
  180.         }
  181.     }

  182. }