1   /* Copyright 2002-2016 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  
19  
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import org.orekit.errors.OrekitException;
24  import org.orekit.errors.OrekitMessages;
25  import org.orekit.utils.ParameterDriver;
26  
27  /** Base clas for force models.
28   * @author Luc Maisonobe
29   * @since 8.0
30   */
31  public abstract class AbstractForceModel implements ForceModel {
32  
33      /** {@inheritDoc} */
34      public ParameterDriver getParameterDriver(final String name)
35          throws OrekitException {
36  
37          for (final ParameterDriver driver : getParametersDrivers()) {
38              if (name.equals(driver.getName())) {
39                  // we have found a parameter with that name
40                  return driver;
41              }
42          }
43  
44          throw notSupportedException(name);
45  
46      }
47  
48      /** {@inheritDoc} */
49      @Override
50      public boolean isSupported(final String name) {
51          for (final ParameterDriver driver : getParametersDrivers()) {
52              if (name.equals(driver.getName())) {
53                  // we have found a parameter with that name
54                  return true;
55              }
56          }
57          // the parameter is not supported
58          return false;
59      }
60  
61      /** Complain if a parameter is not supported.
62       * @param name name of the parameter
63       * @exception OrekitException if the parameter is not supported
64       */
65      protected void complainIfNotSupported(final String name)
66          throws OrekitException {
67          if (!isSupported(name)) {
68              throw notSupportedException(name);
69          }
70      }
71  
72      /** {@inheritDoc} */
73      @Deprecated
74      @Override
75      public List<String> getParametersNames() {
76          final ParameterDriver[] parameters = getParametersDrivers();
77          final List<String> names = new ArrayList<String>(parameters.length);
78          for (final ParameterDriver driver : parameters) {
79              names.add(driver.getName());
80          }
81          return names;
82      }
83  
84      /** {@inheritDoc} */
85      @Deprecated
86      public double getParameter(final String name)
87          throws OrekitException {
88          return getParameterDriver(name).getValue();
89      }
90  
91      /** {@inheritDoc} */
92      @Deprecated
93      public void setParameter(final String name, final double value)
94          throws OrekitException {
95          getParameterDriver(name).setValue(value);
96      }
97  
98      /** Generate an exception for unsupported parameter.
99       * @param name unsupported parameter name
100      * @return exception with appropriate message
101      */
102     private OrekitException notSupportedException(final String name) {
103 
104         final StringBuilder builder = new StringBuilder();
105         for (final ParameterDriver driver : getParametersDrivers()) {
106             if (builder.length() > 0) {
107                 builder.append(", ");
108             }
109             builder.append(driver.getName());
110         }
111         if (builder.length() == 0) {
112             builder.append("<none>");
113         }
114 
115         return new OrekitException(OrekitMessages.UNSUPPORTED_PARAMETER_NAME,
116                                    name, builder.toString());
117 
118     }
119 
120 }