View Javadoc
1   package org.opentrafficsim.road;
2   
3   import java.lang.reflect.Field;
4   import java.util.Set;
5   
6   import org.djutils.reflection.ClassUtil;
7   import org.opentrafficsim.base.parameters.ParameterException;
8   import org.opentrafficsim.base.parameters.ParameterSet;
9   import org.opentrafficsim.base.parameters.ParameterTypeBoolean;
10  import org.opentrafficsim.base.parameters.ParameterTypeDouble;
11  import org.opentrafficsim.base.parameters.ParameterTypeInteger;
12  import org.opentrafficsim.base.parameters.ParameterTypeNumeric;
13  import org.opentrafficsim.base.parameters.ParameterTypes;
14  
15  /**
16   * Creator of set of parameters with default values.
17   * <p>
18   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
19   * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
20   * <p>
21   * @version $Revision$, $LastChangedDate$, by $Author$, initial version Apr 15, 2016 <br>
22   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
23   */
24  public final class DefaultTestParameters
25  {
26  
27      /**
28       * Do not create instance.
29       */
30      private DefaultTestParameters()
31      {
32          // Do not instantiate
33      }
34  
35      /**
36       * Returns a default set of parameters.
37       * @return Default set of parameters.
38       */
39      @SuppressWarnings("unchecked")
40      public static ParameterSet create()
41      {
42          ParameterSet params = new ParameterSet();
43  
44          // set all default values using reflection
45          Set<Field> fields = ClassUtil.getAllFields(ParameterTypes.class);
46          try
47          {
48              for (Field field : fields)
49              {
50                  try
51                  {
52                      if (ParameterTypeNumeric.class.isAssignableFrom(field.getType()))
53                      {
54                          field.setAccessible(true);
55                          @SuppressWarnings("rawtypes")
56                          ParameterTypeNumeric p = (ParameterTypeNumeric) field.get(ParameterTypes.class);
57                          params.setParameter(p, p.getDefaultValue());
58                      }
59                      else if (ParameterTypeBoolean.class.equals(field.getType()))
60                      {
61                          field.setAccessible(true);
62                          ParameterTypeBoolean p = (ParameterTypeBoolean) field.get(ParameterTypes.class);
63                          params.setParameter(p, p.getDefaultValue());
64                      }
65                      else if (ParameterTypeDouble.class.equals(field.getType()))
66                      {
67                          field.setAccessible(true);
68                          ParameterTypeDouble p = (ParameterTypeDouble) field.get(ParameterTypes.class);
69                          params.setParameter(p, p.getDefaultValue());
70                      }
71                      else if (ParameterTypeInteger.class.equals(field.getType()))
72                      {
73                          field.setAccessible(true);
74                          ParameterTypeInteger p = (ParameterTypeInteger) field.get(ParameterTypes.class);
75                          params.setParameter(p, p.getDefaultValue());
76                      }
77                      // FIXME: add another else to catch any unanticipated cases?
78                  }
79                  catch (ParameterException pe)
80                  {
81                      // FIXME: Explain why this exception can/should be ignored.
82                      // do not set parameter without default value
83                  }
84              }
85          }
86          catch (IllegalArgumentException iare)
87          {
88              iare.printStackTrace();
89          }
90          catch (IllegalAccessException iace)
91          {
92              iace.printStackTrace();
93          }
94          return params;
95      }
96  
97  }