AbstractForceModel.java

  1. /* Copyright 2002-2017 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.forces;


  18. import org.orekit.errors.OrekitException;
  19. import org.orekit.errors.OrekitMessages;
  20. import org.orekit.utils.ParameterDriver;

  21. /** Base class for force models.
  22.  * @author Luc Maisonobe
  23.  * @since 8.0
  24.  */
  25. public abstract class AbstractForceModel implements ForceModel {

  26.     /** {@inheritDoc} */
  27.     public ParameterDriver getParameterDriver(final String name)
  28.         throws OrekitException {

  29.         for (final ParameterDriver driver : getParametersDrivers()) {
  30.             if (name.equals(driver.getName())) {
  31.                 // we have found a parameter with that name
  32.                 return driver;
  33.             }
  34.         }

  35.         throw notSupportedException(name);

  36.     }

  37.     /** {@inheritDoc} */
  38.     @Override
  39.     public boolean isSupported(final String name) {
  40.         for (final ParameterDriver driver : getParametersDrivers()) {
  41.             if (name.equals(driver.getName())) {
  42.                 // we have found a parameter with that name
  43.                 return true;
  44.             }
  45.         }
  46.         // the parameter is not supported
  47.         return false;
  48.     }

  49.     /** Complain if a parameter is not supported.
  50.      * @param name name of the parameter
  51.      * @exception OrekitException if the parameter is not supported
  52.      */
  53.     protected void complainIfNotSupported(final String name)
  54.         throws OrekitException {
  55.         if (!isSupported(name)) {
  56.             throw notSupportedException(name);
  57.         }
  58.     }

  59.     /** Generate an exception for unsupported parameter.
  60.      * @param name unsupported parameter name
  61.      * @return exception with appropriate message
  62.      */
  63.     private OrekitException notSupportedException(final String name) {

  64.         final StringBuilder builder = new StringBuilder();
  65.         for (final ParameterDriver driver : getParametersDrivers()) {
  66.             if (builder.length() > 0) {
  67.                 builder.append(", ");
  68.             }
  69.             builder.append(driver.getName());
  70.         }
  71.         if (builder.length() == 0) {
  72.             builder.append("<none>");
  73.         }

  74.         return new OrekitException(OrekitMessages.UNSUPPORTED_PARAMETER_NAME,
  75.                                    name, builder.toString());

  76.     }

  77. }