AbstractReconfigurableDetector.java

  1. /* Copyright 2002-2013 CS Systèmes d'Information
  2.  * Licensed to CS Systèmes d'Information (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. import org.orekit.errors.OrekitException;
  19. import org.orekit.propagation.SpacecraftState;
  20. import org.orekit.propagation.events.handlers.EventHandler;

  21. /** Builder reconfiguration API for detectors.
  22.  * <p>
  23.  * This class is only a temporary one introduced with Orekit 6.1 between the
  24.  * legacy {@link AbstractDetector} and all the Orekit provided detectors. It
  25.  * will be removed in 7.0, when all its methods will be pushed up in the
  26.  * hierarchy, with declarations at the {@link EventDetector} interface level
  27.  * and implementation at the {@link AbstractDetector} abstract class level.
  28.  * In other words, the methods will stay, but the intermediate class will
  29.  * disappear.
  30.  * </p>
  31.  * @author Luc Maisonobe
  32.  * @param <T> class type for the generic version
  33.  * @since 6.1
  34.  */
  35. public abstract class AbstractReconfigurableDetector<T extends EventDetector> extends AbstractDetector {

  36.     /** Serializable UID. */
  37.     private static final long serialVersionUID = 20131202L;

  38.     /** Default handler for event overrides. */
  39.     private final EventHandler<T> handler;

  40.     /** Build a new instance.
  41.      * @param maxCheck maximum checking interval (s)
  42.      * @param threshold convergence threshold (s)
  43.      * @param maxIter maximum number of iterations in the event time search
  44.      * @param handler event handler to call at event occurrences
  45.      */
  46.     protected AbstractReconfigurableDetector(final double maxCheck, final double threshold,
  47.                                              final int maxIter, final EventHandler<T> handler) {
  48.         super(maxCheck, threshold, maxIter);
  49.         this.handler = handler;
  50.     }

  51.     /** Build a new instance.
  52.      * @param newMaxCheck maximum checking interval (s)
  53.      * @param newThreshold convergence threshold (s)
  54.      * @param newMaxIter maximum number of iterations in the event time search
  55.      * @param newHandler event handler to call at event occurrences
  56.      * @return a new instance of the appropriate sub-type
  57.      */
  58.     protected abstract T create(final double newMaxCheck, final double newThreshold,
  59.                                 final int newMaxIter, final EventHandler<T> newHandler);

  60.     /**
  61.      * Setup the maximum checking interval.
  62.      * <p>
  63.      * This will override a maximum checking interval if it has been configured previously.
  64.      * </p>
  65.      * @param newMaxCheck maximum checking interval (s)
  66.      * @return a new detector with updated configuration (the instance is not changed)
  67.      * @since 6.1
  68.      */
  69.     public T withMaxCheck(final double newMaxCheck) {
  70.         return create(newMaxCheck, getThreshold(), getMaxIterationCount(), getHandler());
  71.     }

  72.     /**
  73.      * Setup the maximum number of iterations in the event time search.
  74.      * <p>
  75.      * This will override a number of iterations if it has been configured previously.
  76.      * </p>
  77.      * @param newMaxIter maximum number of iterations in the event time search
  78.      * @return a new detector with updated configuration (the instance is not changed)
  79.      * @since 6.1
  80.      */
  81.     public T withMaxIter(final int newMaxIter) {
  82.         return create(getMaxCheckInterval(), getThreshold(), newMaxIter,  getHandler());
  83.     }

  84.     /**
  85.      * Setup the convergence threshold.
  86.      * <p>
  87.      * This will override a convergence threshold if it has been configured previously.
  88.      * </p>
  89.      * @param newThreshold convergence threshold (s)
  90.      * @return a new detector with updated configuration (the instance is not changed)
  91.      * @since 6.1
  92.      */
  93.     public T withThreshold(final double newThreshold) {
  94.         return create(getMaxCheckInterval(), newThreshold, getMaxIterationCount(),  getHandler());
  95.     }

  96.     /**
  97.      * Setup the event handler to call at event occurrences.
  98.      * <p>
  99.      * This will override a handler if it has been configured previously.
  100.      * </p>
  101.      * @param newHandler event handler to call at event occurrences
  102.      * @return a new detector with updated configuration (the instance is not changed)
  103.      * @since 6.1
  104.      */
  105.     public T withHandler(final EventHandler<T> newHandler) {
  106.         return create(getMaxCheckInterval(), getThreshold(), getMaxIterationCount(), newHandler);
  107.     }

  108.     /** Get the handler.
  109.      * @return event handler to call at event occurrences
  110.      */
  111.     public EventHandler<T> getHandler() {
  112.         return handler;
  113.     }

  114.     /** {@inheritDoc}
  115.      * @deprecated as of 6.1 replaced by {@link
  116.      * EventHandler#eventOccurred(SpacecraftState, EventDetector, boolean)}
  117.      */
  118.     @Deprecated
  119.     public Action eventOccurred(final SpacecraftState s, final boolean increasing)
  120.         throws OrekitException {
  121.         @SuppressWarnings("unchecked")
  122.         final T self = (T) this;
  123.         return convert(handler.eventOccurred(s, self, increasing));
  124.     }

  125.     /** Conversion between pre-6.1 EventDetector.Action and post-6.1 EventHandler.Action.
  126.      * @param action action to convert
  127.      * @return converted action
  128.      */
  129.     @SuppressWarnings("deprecation")
  130.     public static EventHandler.Action convert(final EventDetector.Action action) {
  131.         switch (action) {
  132.         case STOP :
  133.             return EventHandler.Action.STOP;
  134.         case RESET_STATE :
  135.             return EventHandler.Action.RESET_STATE;
  136.         case RESET_DERIVATIVES :
  137.             return EventHandler.Action.RESET_DERIVATIVES;
  138.         case CONTINUE :
  139.             return EventHandler.Action.CONTINUE;
  140.         default :
  141.             // this should never occur
  142.             throw OrekitException.createInternalError(null);
  143.         }
  144.     }

  145.     /** Conversion between post 6.1 EventHandler.Action and pre-6.1 EventDetector.Action.
  146.      * @param action action to convert
  147.      * @return converted action
  148.      */
  149.     @SuppressWarnings("deprecation")
  150.     public static EventDetector.Action convert(final EventHandler.Action action) {
  151.         switch (action) {
  152.         case STOP :
  153.             return EventDetector.Action.STOP;
  154.         case RESET_STATE :
  155.             return EventDetector.Action.RESET_STATE;
  156.         case RESET_DERIVATIVES :
  157.             return EventDetector.Action.RESET_DERIVATIVES;
  158.         case CONTINUE :
  159.             return EventDetector.Action.CONTINUE;
  160.         default :
  161.             // this should never occur
  162.             throw OrekitException.createInternalError(null);
  163.         }
  164.     }

  165.     /** {@inheritDoc}
  166.      * @deprecated as of 6.1 replaced by {@link EventHandler#resetState(EventDetector, SpacecraftState)}
  167.      */
  168.     @Deprecated
  169.     public SpacecraftState resetState(final SpacecraftState oldState)
  170.         throws OrekitException {
  171.         @SuppressWarnings("unchecked")
  172.         final T self = (T) this;
  173.         return handler.resetState(self, oldState);
  174.     }

  175. }