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
16
17
18
19 public class GenerateUSLocale
20 {
21
22
23
24
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
32 return result;
33 }
34
35
36
37
38
39
40
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
57
58
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
68
69
70 public static void main(final String[] args)
71 {
72 @SuppressWarnings("unused")
73 AreaUnit junk = UNITS.ACRE;
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
95 Unit<?> left = quantity.getUnitById(o1);
96 Unit<?> right = quantity.getUnitById(o2);
97
98
99
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 }