FieldStepHandlerMultiplexer.java

  1. /* Copyright 2002-2022 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.  */
  29. public class FieldStepHandlerMultiplexer<T extends CalculusFieldElement<T>> implements FieldOrekitStepHandler<T> {

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

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

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

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

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

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

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

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

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

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

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

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

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

  181. }