View Javadoc
1   package org.djunits.generator;
2   
3   import java.util.Arrays;
4   import java.util.Comparator;
5   import java.util.TreeMap;
6   
7   import org.djunits.unit.AreaUnit;
8   import org.djunits.unit.Unit;
9   import org.djunits.unit.quantity.Quantities;
10  import org.djunits.unit.quantity.Quantity;
11  import org.djunits.unit.unitsystem.UnitSystem;
12  import org.djunits.unit.util.UNITS;
13  
14  /**
15   * Generator for the locale properties file.
16   * <p>
17   * @author <a href="https://www.tudelft.nl/p.knoppers">Peter Knoppers</a>
18   */
19  public class GenerateUSLocale
20  {
21      /**
22       * Determine if a double is (very near to) a power of 10.
23       * @param value double; the value to test
24       * @return boolean; true if <code>value</code> is (very near to) a power of 10; false otherwise
25       */
26      static boolean isPowerOf10(final double value)
27      {
28          double log = Math.log10(value);
29          double rintLog = Math.rint(log);
30          boolean result = Math.abs(rintLog - log) < 0.000001;
31          // System.out.println("value " + value + " is power of 10? " + result);
32          return result;
33      }
34  
35      /**
36       * Compare two double values.
37       * @param left double; the left value
38       * @param right double; the right value
39       * @return int; 0 if the values are equal; -1 if left should be sorted before right; +1 if right should be sorted before
40       *         left
41       */
42      static int compareDoubles(final double left, final double right)
43      {
44          if (left > right)
45          {
46              return -1;
47          }
48          else if (left < right)
49          {
50              return 1;
51          }
52          return 0;
53      }
54  
55      /**
56       * Indicate if a unit is SI.
57       * @param unit Unit&lt;?&gt;; the unit
58       * @return boolean; true if the unit is SI; false otherwise
59       */
60      static boolean isSI(final Unit<?> unit)
61      {
62          return unit.getUnitSystem().equals(UnitSystem.SI_ACCEPTED) || unit.getUnitSystem().equals(UnitSystem.SI_BASE)
63                  || unit.getUnitSystem().equals(UnitSystem.SI_DERIVED);
64      }
65  
66      /**
67       * Generate the localeunit.properties file.
68       * @param args String[]; the command line arguments; not used (yet)
69       */
70      public static void main(final String[] args)
71      {
72          @SuppressWarnings("unused")
73          AreaUnit junk = UNITS.ACRE; // force loading of all units
74  
75          System.out.println("# Format:");
76          System.out.println("# UnitType.unitname = abbreviation | description");
77          System.out.println("# UnitType.unitname = abbreviation | description | text_abbreviation1 | text_abbreviation2 | ...");
78  
79          TreeMap<String, Quantity<?>> unitTypes = new TreeMap<>(Quantities.INSTANCE.getRegistry());
80          for (String quantityName : unitTypes.keySet())
81          {
82              Quantity<?> quantity = unitTypes.get(quantityName);
83              System.out.println("#");
84              System.out.println("# " + quantity.getName());
85              System.out.println("#");
86              System.out.println(quantity.getName() + " = " + quantity.getName());
87              TreeMap<String, Unit<?>> ids = new TreeMap<>(quantity.getUnitsById());
88              String[] idArray = ids.keySet().toArray(new String[0]);
89              Arrays.sort(idArray, new Comparator<String>()
90              {
91                  @Override
92                  public int compare(String o1, String o2)
93                  {
94                      // System.out.println("Comparing " + o1 + " to " + o2);
95                      Unit<?> left = quantity.getUnitById(o1);
96                      Unit<?> right = quantity.getUnitById(o2);
97                      // if (left.equals(FrequencyUnit.PER_DAY) || right.equals(FrequencyUnit.PER_DAY))
98                      // {
99                      // System.out.println("Comparing " + o1 + " to " + o2);
100                     // }
101                     if (left.equals(right))
102                     {
103                         return o1.compareTo(o2);
104                     }
105                     if (left.equals(left.getQuantity().getStandardUnit()))
106                     {
107                         return -1;
108                     }
109                     if (right.equals(right.getQuantity().getStandardUnit()))
110                     {
111                         return 1;
112                     }
113                     if (isSI(left) && (!isSI(right)))
114                     {
115                         return -1;
116                     }
117                     if ((!isSI(left) && isSI(right)))
118                     {
119                         return 1;
120                     }
121                     double leftFactor = left.getScale().toStandardUnit(1.0) - left.getScale().toStandardUnit(0.0);
122                     double rightFactor = right.getScale().toStandardUnit(1.0) - right.getScale().toStandardUnit(0.0);
123                     if (isPowerOf10(leftFactor) && (isPowerOf10(rightFactor)))
124                     {
125                         return compareDoubles(leftFactor, rightFactor);
126                     }
127                     if (isPowerOf10(leftFactor))
128                     {
129                         return -1;
130                     }
131                     if (isPowerOf10(rightFactor))
132                     {
133                         return 1;
134                     }
135                     if (left.getName().endsWith("watt-hour") && right.getName().endsWith("watt-hour"))
136                     {
137                         return compareDoubles(leftFactor, rightFactor);
138                     }
139                     if (left.getName().endsWith("watt-hour"))
140                     {
141                         return -1;
142                     }
143                     if (right.getName().endsWith("watt-hour"))
144                     {
145                         return 1;
146                     }
147                     if (left.getName().endsWith("electronvolt") && right.getName().endsWith("electronvolt"))
148                     {
149                         return compareDoubles(leftFactor, rightFactor);
150                     }
151                     if (left.getName().endsWith("electronvolt"))
152                     {
153                         return -1;
154                     }
155                     if (right.getName().endsWith("electronvolt"))
156                     {
157                         return 1;
158                     }
159                     return left.getName().compareTo(right.getName());
160                 }
161             });
162             for (String unitId : idArray)
163             {
164                 Unit<?> u = quantity.getUnitById(unitId);
165                 System.out.print(quantity.getName() + "." + u.getId() + " = "
166                         + u.getDefaultDisplayAbbreviation() + " | " + u.getName());
167                 if (u.getAbbreviations().size() > 1)
168                 {
169                     for (String abbreviation : u.getAbbreviations())
170                     {
171                         if (!abbreviation.equals(u.getDefaultDisplayAbbreviation()))
172                         {
173                             System.out.print(" | " + abbreviation);
174                         }
175                     }
176                 }
177                 System.out.println();
178             }
179         }
180     }
181 
182 }