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