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.forces;
18  
19  
20  import org.orekit.errors.OrekitException;
21  import org.orekit.errors.OrekitMessages;
22  import org.orekit.utils.ParameterDriver;
23  
24  /** Base class for force models.
25   * @author Luc Maisonobe
26   * @since 8.0
27   */
28  public abstract class AbstractForceModel implements ForceModel {
29  
30      /** {@inheritDoc} */
31      public ParameterDriver getParameterDriver(final String name) {
32  
33          for (final ParameterDriver driver : getParametersDrivers()) {
34              if (name.equals(driver.getName())) {
35                  // we have found a parameter with that name
36                  return driver;
37              }
38          }
39  
40          throw notSupportedException(name);
41  
42      }
43  
44      /** {@inheritDoc} */
45      @Override
46      public boolean isSupported(final String name) {
47          for (final ParameterDriver driver : getParametersDrivers()) {
48              if (name.equals(driver.getName())) {
49                  // we have found a parameter with that name
50                  return true;
51              }
52          }
53          // the parameter is not supported
54          return false;
55      }
56  
57      /** Complain if a parameter is not supported.
58       * @param name name of the parameter
59       */
60      protected void complainIfNotSupported(final String name) {
61          if (!isSupported(name)) {
62              throw notSupportedException(name);
63          }
64      }
65  
66      /** Generate an exception for unsupported parameter.
67       * @param name unsupported parameter name
68       * @return exception with appropriate message
69       */
70      private OrekitException notSupportedException(final String name) {
71  
72          final StringBuilder builder = new StringBuilder();
73          for (final ParameterDriver driver : getParametersDrivers()) {
74              if (builder.length() > 0) {
75                  builder.append(", ");
76              }
77              builder.append(driver.getName());
78          }
79          if (builder.length() == 0) {
80              builder.append("<none>");
81          }
82  
83          return new OrekitException(OrekitMessages.UNSUPPORTED_PARAMETER_NAME,
84                                     name, builder.toString());
85  
86      }
87  
88  }