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