View Javadoc
1   package org.djunits.generator;
2   
3   import java.lang.reflect.Field;
4   import java.lang.reflect.Modifier;
5   
6   import org.djunits.unit.Unit;
7   
8   /**
9    * <p>
10   * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
11   * BSD-style license. See <a href="http://djunits.org/docs/license.html">DJUNITS License</a>.
12   * </p>
13   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
14   * initial version Apr 12, 2017 <br>
15   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
16   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
17   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
18   */
19  public final class GenerateStaticUNITS
20  {
21  
22      /**
23       * 
24       */
25      private GenerateStaticUNITS()
26      {
27          // utility class
28      }
29  
30      /**
31       * @param args should be empty
32       */
33      public static void main(String[] args)
34      {
35          for (String className : Unit.STANDARD_UNITS)
36          {
37              if (!className.contains("Money") && !className.contains("Dimensionless"))
38              {
39                  System.out.println();
40                  System.out.println(
41                          "    /****************************************************************************************************************/");
42                  String s =
43                          "    /******************************************************XX********************************************************/";
44                  String cs = className.toUpperCase().replace("UNIT", "");
45                  int i = cs.length() + 2;
46                  cs = (i % 2 == 1) ? cs + " " : cs;
47                  i = (i % 2 == 1) ? i + 2 : i;
48                  i = i / 2;
49                  s = s.replace("********************".substring(0, i - 1) + "X", " " + cs + " ");
50                  s = s.replace("X********************".substring(0, i), "");
51                  System.out.println(s);
52                  System.out.println(
53                          "    /****************************************************************************************************************/");
54                  System.out.println();
55  
56                  @SuppressWarnings("rawtypes")
57                  Class c;
58                  try
59                  {
60                      c = Class.forName("org.djunits.unit." + className);
61                  }
62                  catch (Exception exception)
63                  {
64                      System.exit(-1);
65                      return;
66                  }
67  
68                  int l = 0;
69                  for (Field f : c.getFields())
70                  {
71                      if (Modifier.isPublic(f.getModifiers()) && Modifier.isStatic(f.getModifiers())
72                              && Modifier.isFinal(f.getModifiers()) && !f.getName().equals("SI")
73                              && !f.getName().equals("STANDARD_UNITS"))
74                      {
75                          String n = f.getName();
76                          if (f.getName().contains("ELECTRONVOLT"))
77                          {
78                              n = cs.trim() + "_" + n;
79                          }
80                          l = Math.max(l, n.length());
81                      }
82                  }
83  
84                  for (Field f : c.getFields())
85                  {
86                      if (Modifier.isPublic(f.getModifiers()) && Modifier.isStatic(f.getModifiers())
87                              && Modifier.isFinal(f.getModifiers()) && !f.getName().equals("SI")
88                              && !f.getName().equals("STANDARD_UNITS"))
89                      {
90                          String n = f.getName();
91                          if (f.getName().contains("ELECTRONVOLT"))
92                          {
93                              n = cs.trim() + "_" + n;
94                          }
95  
96                          String un = (n + "                                    ").substring(0, l);
97                          System.out.println(
98                                  "    " + c.getSimpleName() + " " + un + " = " + c.getSimpleName() + "." + f.getName() + ";");
99                      }
100                 }
101             }
102         }
103     }
104 
105 }