View Javadoc
1   package org.opentrafficsim.core.definitions;
2   
3   import java.lang.reflect.Field;
4   import java.util.Locale;
5   
6   /**
7    * This class houses defaults instances for different types, such as GTU types and link types. The static fields should only be
8    * accessed in the setup of a simulation. The simulation itself should be fed the relevant types, and not assume any specific or
9    * more generic super type. Only in this way can simulations be run with entirely different type structures.
10   * <p>
11   * Copyright (c) 2022-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
12   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
13   * </p>
14   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
15   * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
16   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
17   */
18  public abstract class Defaults
19  {
20  
21      /** Defaults for locale nl_NL. */
22      public static final DefaultsNl NL = new DefaultsNl();
23  
24      /** Locale. */
25      private final Locale locale;
26  
27      /**
28       * Constructor.
29       * @param locale Locale; locale.
30       */
31      protected Defaults(final Locale locale)
32      {
33          this.locale = locale;
34      }
35  
36      /**
37       * Returns the locale.
38       * @return Locale; locale.
39       */
40      public Locale getLocale()
41      {
42          return this.locale;
43      }
44  
45      /**
46       * Returns a default value of a type, indicated by its name. This should only be used by parsers. Simulations defined in
47       * code should access the relevant static fields directly for code maintainability.
48       * @param clazz Class&lt;T&gt;; class instance of type T.
49       * @param name String; name referring to a default through static field names, e.g. "NL.VEHICLE".
50       * @param <T> type of the value.
51       * @return T; returned default value, {@code null} if the default could not be found.
52       */
53      public static <T> T getByName(final Class<T> clazz, final String name)
54      {
55          return getByName(Defaults.class, clazz, name);
56      }
57      
58      /**
59       * Returns a default value of a type, indicated by its name. This should only be used by parsers. Simulations defined in
60       * code should access the relevant static fields directly for code maintainability.
61       * @param defaultsClass Class&lt;? extends Defaults&gt;; defaults class.
62       * @param clazz Class&lt;T&gt;; class instance of type T.
63       * @param name String; name referring to a default through static field names, e.g. "NL.VEHICLE".
64       * @param <T> type of the value.
65       * @return T; returned default value, {@code null} if the default could not be found.
66       */
67      @SuppressWarnings("unchecked")
68      protected static <T> T getByName(final Class<? extends Defaults> defaultsClass, final Class<T> clazz, final String name)
69      {
70          try
71          {
72              String[] subNames = name.split("\\.");
73              Field field1 = defaultsClass.getDeclaredField(subNames[0]);
74              Object defaults = field1.get(defaultsClass);
75              Field field2 = defaults.getClass().getDeclaredField(subNames[1]);
76              return (T) field2.get(defaults.getClass());
77          }
78          catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException ex)
79          {
80              return null;
81          }
82      }
83  
84  }