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-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
19   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
20   * </p>
21   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
22   */
23  public final class DefaultTestParameters
24  {
25  
26      /**
27       * Do not create instance.
28       */
29      private DefaultTestParameters()
30      {
31          // Do not instantiate
32      }
33  
34      /**
35       * Returns a default set of parameters.
36       * @return Default set of parameters.
37       */
38      @SuppressWarnings("unchecked")
39      public static ParameterSet create()
40      {
41          ParameterSet params = new ParameterSet();
42  
43          // set all default values using reflection
44          Set<Field> fields = ClassUtil.getAllFields(ParameterTypes.class);
45          try
46          {
47              for (Field field : fields)
48              {
49                  try
50                  {
51                      if (ParameterTypeNumeric.class.isAssignableFrom(field.getType()))
52                      {
53                          field.setAccessible(true);
54                          @SuppressWarnings("rawtypes")
55                          ParameterTypeNumeric p = (ParameterTypeNumeric) field.get(ParameterTypes.class);
56                          params.setParameter(p, p.getDefaultValue());
57                      }
58                      else if (ParameterTypeBoolean.class.equals(field.getType()))
59                      {
60                          field.setAccessible(true);
61                          ParameterTypeBoolean p = (ParameterTypeBoolean) field.get(ParameterTypes.class);
62                          params.setParameter(p, p.getDefaultValue());
63                      }
64                      else if (ParameterTypeDouble.class.equals(field.getType()))
65                      {
66                          field.setAccessible(true);
67                          ParameterTypeDouble p = (ParameterTypeDouble) field.get(ParameterTypes.class);
68                          params.setParameter(p, p.getDefaultValue());
69                      }
70                      else if (ParameterTypeInteger.class.equals(field.getType()))
71                      {
72                          field.setAccessible(true);
73                          ParameterTypeInteger p = (ParameterTypeInteger) field.get(ParameterTypes.class);
74                          params.setParameter(p, p.getDefaultValue());
75                      }
76                      // FIXME: add another else to catch any unanticipated cases?
77                  }
78                  catch (ParameterException pe)
79                  {
80                      // FIXME: Explain why this exception can/should be ignored.
81                      // do not set parameter without default value
82                  }
83              }
84          }
85          catch (IllegalArgumentException iare)
86          {
87              iare.printStackTrace();
88          }
89          catch (IllegalAccessException iace)
90          {
91              iace.printStackTrace();
92          }
93          return params;
94      }
95  
96  }