AbstractForceModel.java

  1. /* Copyright 2002-2020 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.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.         for (final ParameterDriver driver : getParametersDrivers()) {
  29.             if (name.equals(driver.getName())) {
  30.                 // we have found a parameter with that name
  31.                 return driver;
  32.             }
  33.         }

  34.         throw notSupportedException(name);

  35.     }

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

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

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

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

  71.         return new OrekitException(OrekitMessages.UNSUPPORTED_PARAMETER_NAME,
  72.                                    name, builder.toString());

  73.     }

  74. }