View Javadoc
1   package org.djutils.serialization;
2   
3   import java.util.HashMap;
4   import java.util.Map;
5   import java.util.Objects;
6   
7   import org.djunits.unit.AbsoluteTemperatureUnit;
8   import org.djunits.unit.AbsorbedDoseUnit;
9   import org.djunits.unit.AccelerationUnit;
10  import org.djunits.unit.AmountOfSubstanceUnit;
11  import org.djunits.unit.AngleUnit;
12  import org.djunits.unit.AngularAccelerationUnit;
13  import org.djunits.unit.AngularVelocityUnit;
14  import org.djunits.unit.AreaUnit;
15  import org.djunits.unit.CatalyticActivityUnit;
16  import org.djunits.unit.DensityUnit;
17  import org.djunits.unit.DimensionlessUnit;
18  import org.djunits.unit.DirectionUnit;
19  import org.djunits.unit.DurationUnit;
20  import org.djunits.unit.ElectricalCapacitanceUnit;
21  import org.djunits.unit.ElectricalChargeUnit;
22  import org.djunits.unit.ElectricalConductanceUnit;
23  import org.djunits.unit.ElectricalCurrentUnit;
24  import org.djunits.unit.ElectricalInductanceUnit;
25  import org.djunits.unit.ElectricalPotentialUnit;
26  import org.djunits.unit.ElectricalResistanceUnit;
27  import org.djunits.unit.EnergyUnit;
28  import org.djunits.unit.EquivalentDoseUnit;
29  import org.djunits.unit.FlowMassUnit;
30  import org.djunits.unit.FlowVolumeUnit;
31  import org.djunits.unit.ForceUnit;
32  import org.djunits.unit.FrequencyUnit;
33  import org.djunits.unit.IlluminanceUnit;
34  import org.djunits.unit.LengthUnit;
35  import org.djunits.unit.LinearDensityUnit;
36  import org.djunits.unit.LuminousFluxUnit;
37  import org.djunits.unit.LuminousIntensityUnit;
38  import org.djunits.unit.MagneticFluxDensityUnit;
39  import org.djunits.unit.MagneticFluxUnit;
40  import org.djunits.unit.MassUnit;
41  import org.djunits.unit.MomentumUnit;
42  import org.djunits.unit.PositionUnit;
43  import org.djunits.unit.PowerUnit;
44  import org.djunits.unit.PressureUnit;
45  import org.djunits.unit.RadioActivityUnit;
46  import org.djunits.unit.SolidAngleUnit;
47  import org.djunits.unit.SpeedUnit;
48  import org.djunits.unit.TemperatureUnit;
49  import org.djunits.unit.TimeUnit;
50  import org.djunits.unit.TorqueUnit;
51  import org.djunits.unit.Unit;
52  import org.djunits.unit.VolumeUnit;
53  
54  /**
55   * The unit types with their code, including static methods to quickly find a unit type.
56   * <p>
57   * Copyright (c) 2016-2025 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
58   * BSD-style license. See <a href="https://sim0mq.org/docs/current/license.html">Sim0MQ License</a>.
59   * </p>
60   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
61   */
62  public class QuantityType
63  {
64      /** the unit types from number to type. */
65      private static Map<Byte, QuantityType> byteTypeMap = new HashMap<>();
66  
67      /** the unit types from class to type. */
68      private static Map<Class<? extends Unit<?>>, QuantityType> unitTypeMap = new HashMap<>();
69  
70      /** Dimensionless unit type with code 0. */
71      public static final QuantityType DIMENSIONLESS =
72              new QuantityType(0, DimensionlessUnit.class, "Dimensionless", "Unit without a dimension", "[]");
73  
74      /** Acceleration unit type with code 1. */
75      public static final QuantityType ACCELERATION =
76              new QuantityType(1, AccelerationUnit.class, "Acceleration", "Acceleration", "[m/s^2]");
77  
78      /** SolidAngle unit type with code 2. */
79      public static final QuantityType SOLIDANGLE =
80              new QuantityType(2, SolidAngleUnit.class, "SolidAngle", "Solid angle (steradian)", "[sr]");
81  
82      /** Angle unit type with code 3. */
83      public static final QuantityType ANGLE = new QuantityType(3, AngleUnit.class, "Angle", "Angle (relative)", "[rad]");
84  
85      /** Direction unit type with code 4. */
86      public static final QuantityType DIRECTION =
87              new QuantityType(4, DirectionUnit.class, "Direction", "Angle (absolute)", "[rad]");
88  
89      /** Area unit type with code 5. */
90      public static final QuantityType AREA = new QuantityType(5, AreaUnit.class, "Area", "Area (m2)", "[m^2]");
91  
92      /** Density unit type with code 6. */
93      public static final QuantityType DENSITY =
94              new QuantityType(6, DensityUnit.class, "Density", "Density based on mass and length", "[kg/m^3]");
95  
96      /** ElectricalCharge unit type with code 7. */
97      public static final QuantityType ELECTRICALCHARGE =
98              new QuantityType(7, ElectricalChargeUnit.class, "ElectricalCharge", "Electrical charge (Coulomb)", "[s.A]");
99  
100     /** ElectricalCurrent unit type with code 8. */
101     public static final QuantityType ELECTRICALCURRENT =
102             new QuantityType(8, ElectricalCurrentUnit.class, "ElectricalCurrent", "Electrical current (Ampere)", "[A]");
103 
104     /** ElectricalPotential unit type with code 9. */
105     public static final QuantityType ELECTRICALPOTENTIAL = new QuantityType(9, ElectricalPotentialUnit.class,
106             "ElectricalPotential", "Electrical potential (Volt)", "[kg.m^2/s^3.A]");
107 
108     /** ElectricalResistance unit type with code 10. */
109     public static final QuantityType ELECTRICALRESISTANCE = new QuantityType(10, ElectricalResistanceUnit.class,
110             "ElectricalResistance", "Electrical resistance (Ohm)", "[kg.m^2/s^3.A^2]");
111 
112     /** Energy unit type with code 11. */
113     public static final QuantityType ENERGY =
114             new QuantityType(11, EnergyUnit.class, "Energy", "Energy (Joule)", "[kg.m^2/s^2]");
115 
116     /** FlowMass unit type with code 12. */
117     public static final QuantityType FLOWMASS =
118             new QuantityType(12, FlowMassUnit.class, "FlowMass", "Mass flow rate ", "[kg/s]");
119 
120     /** FlowVolume unit type with code 13. */
121     public static final QuantityType FLOWVOLUME =
122             new QuantityType(13, FlowVolumeUnit.class, "FlowVolume", "Volume flow rate", "[m^3/s]");
123 
124     /** Force unit type with code 14. */
125     public static final QuantityType FORCE = new QuantityType(14, ForceUnit.class, "Force", "Force (Newton)", "[kg.m/s^2]");
126 
127     /** Frequency unit type with code 15. */
128     public static final QuantityType FREQUENCY =
129             new QuantityType(15, FrequencyUnit.class, "Frequency", "Frequency (Hz)", "[1/s]");
130 
131     /** Length unit type with code 16. */
132     public static final QuantityType LENGTH = new QuantityType(16, LengthUnit.class, "Length", "Length (relative)", "[m]");
133 
134     /** Position unit type with code 17. */
135     public static final QuantityType POSITION =
136             new QuantityType(17, PositionUnit.class, "Position", "Length (absolute)", "[m]");
137 
138     /** LinearDensity unit type with code 18. */
139     public static final QuantityType LINEARDENSITY =
140             new QuantityType(18, LinearDensityUnit.class, "LinearDensity", "Linear density ", "[1/m]");
141 
142     /** Mass unit type with code 19. */
143     public static final QuantityType MASS = new QuantityType(19, MassUnit.class, "Mass", "Mass", "[kg]");
144 
145     /** Power unit type with code 20. */
146     public static final QuantityType POWER = new QuantityType(20, PowerUnit.class, "Power", "Power (Watt)", "[kg.m^2/s^3]");
147 
148     /** Pressure unit type with code 21. */
149     public static final QuantityType PRESSURE =
150             new QuantityType(21, PressureUnit.class, "Pressure", "Pressure (Pascal)", "[kg/m.s^2]");
151 
152     /** Speed unit type with code 22. */
153     public static final QuantityType SPEED = new QuantityType(22, SpeedUnit.class, "Speed", "Speed", "[m/s]");
154 
155     /** Temperature unit type with code 23. */
156     public static final QuantityType TEMPERATURE =
157             new QuantityType(23, TemperatureUnit.class, "Temperature", "Temperature (relative)", "[K]");
158 
159     /** AbsoluteTemperature unit type with code 24. */
160     public static final QuantityType ABSOLUTETEMPERATURE =
161             new QuantityType(24, AbsoluteTemperatureUnit.class, "AbsoluteTemperature", "Temperature (absolute)", "[K]");
162 
163     /** Duration unit type with code 25. */
164     public static final QuantityType DURATION = new QuantityType(25, DurationUnit.class, "Duration", "Time (relative)", "[s]");
165 
166     /** Time unit type with code 26. */
167     public static final QuantityType TIME = new QuantityType(26, TimeUnit.class, "Time", "Time (absolute)", "[s]");
168 
169     /** Torque unit type with code 27. */
170     public static final QuantityType TORQUE =
171             new QuantityType(27, TorqueUnit.class, "Torque", "Torque (Newton-meter)", "[kg.m^2/s^2]");
172 
173     /** Volume unit type with code 28. */
174     public static final QuantityType VOLUME = new QuantityType(28, VolumeUnit.class, "Volume", "Volume", "[m^3]");
175 
176     /** AbsorbedDose unit type with code 29. */
177     public static final QuantityType ABSORBEDDOSE =
178             new QuantityType(28, AbsorbedDoseUnit.class, "Absorbed dose", "Absorbed Dose (Gray)", "[m^2/s^2]");
179 
180     /** AmountOfSubstance unit type with code 30. */
181     public static final QuantityType AMOUNTOFSUBSTANCE =
182             new QuantityType(30, AmountOfSubstanceUnit.class, "Amount of substance", "Amount of substance (mole)", "[mol]");
183 
184     /** CatalyticActivity unit type with code 31. */
185     public static final QuantityType CATALYTICACTIVITY =
186             new QuantityType(31, CatalyticActivityUnit.class, "Catalytic activity", "Catalytic activity (katal)", "[mol/s]");
187 
188     /** ElectricalCapacitance unit type with code 32. */
189     public static final QuantityType ELECTRICALCAPACITANCE = new QuantityType(32, ElectricalCapacitanceUnit.class,
190             "Electrical capacitance", "Electrical capacitance (Farad)", "[s^4.A^2/kg.m^2]");
191 
192     /** ElectricalConductance unit type with code 33. */
193     public static final QuantityType ELECTRICALCONDUCTANCE = new QuantityType(33, ElectricalConductanceUnit.class,
194             "Electrical conductance", "Electrical conductance (Siemens)", "[s^3.A^2/kg.m^2]");
195 
196     /** ElectricalInductance unit type with code 34. */
197     public static final QuantityType ELECTRICALINDUCTANCE = new QuantityType(34, ElectricalInductanceUnit.class,
198             "Electrical inductance", "Electrical inductance (Henry)", "[kg.m^2/s^2.A^2]");
199 
200     /** EquivalentDose unit type with code 35. */
201     public static final QuantityType EQUIVALENTDOSE =
202             new QuantityType(35, EquivalentDoseUnit.class, "Equivalent dose", "Equivalent dose (Sievert)", "[m^2/s^2]");
203 
204     /** Illuminance unit type with code 36. */
205     public static final QuantityType ILLUMINANCE =
206             new QuantityType(36, IlluminanceUnit.class, "Illuminance", "Illuminance (lux)", "[sr.cd/m^2]");
207 
208     /** LuminousFlux unit type with code 37. */
209     public static final QuantityType LUMINOUSFLUX =
210             new QuantityType(37, LuminousFluxUnit.class, "Luminous flux", "Luminous flux (lumen)", "[sr.cd]");
211 
212     /** LuminousIntensity unit type with code 38. */
213     public static final QuantityType LUMINOUSINTENSITY =
214             new QuantityType(38, LuminousIntensityUnit.class, "Luminous intensity", "Luminous intensity (candela)", "[cd]");
215 
216     /** MagneticFluxDensity unit type with code 39. */
217     public static final QuantityType MAGNETICFLUXDENSITY = new QuantityType(39, MagneticFluxDensityUnit.class,
218             "Magnetic flux density", "Magnetic flux density (Tesla)", "[kg/s^2.A]");
219 
220     /** MagneticFlux unit type with code 40. */
221     public static final QuantityType MAGNETICFLUX =
222             new QuantityType(40, MagneticFluxUnit.class, "Magnetic flux", "Magnetic flux (Weber)", "[kg.m^2/s^2.A]");
223 
224     /** RadioActivity unit type with code 41. */
225     public static final QuantityType RADIOACTIVITY =
226             new QuantityType(41, RadioActivityUnit.class, "Radioactivity", "Radioactivity (Becquerel)", "[1/s]");
227 
228     /** AngularAcceleration unit type with code 42. */
229     public static final QuantityType ANGULARACCELERATION =
230             new QuantityType(42, AngularAccelerationUnit.class, "AngularAcceleration", "AngularAcceleration", "[rad/s^2]");
231 
232     /** AngularVelocity unit type with code 43. */
233     public static final QuantityType ANGULARVELOCITY =
234             new QuantityType(43, AngularVelocityUnit.class, "AngularVelocity", "AngularVelocity", "[rad/s]");
235 
236     /** AngularAcceleration unit type with code 44. */
237     public static final QuantityType MOMENTUM = new QuantityType(44, MomentumUnit.class, "Momentum", "Momentum", "[rad/s^2]");
238 
239     /** The code of the unit as a byte. */
240     private final byte code;
241 
242     /** The djunits data type. */
243     private final Class<? extends Unit<?>> djunitsType;
244 
245     /** The unit name. */
246     private final String name;
247 
248     /** The unit description. */
249     private final String description;
250 
251     /** The SI or default unit in SI-elements. */
252     private final String siUnit;
253 
254     /**
255      * Construct a new UnitType and put it in the maps.
256      * @param code the byte code of the unit provided as an int
257      * @param djunitsType the djunits data type
258      * @param name the unit name
259      * @param description the unit description
260      * @param siUnit the SI or default unit in SI-elements
261      * @param <U> the Unit
262      */
263     public <U extends Unit<U>> QuantityType(final int code, final Class<U> djunitsType, final String name,
264             final String description, final String siUnit)
265     {
266         this.code = (byte) code;
267         this.djunitsType = djunitsType;
268         this.name = name;
269         this.description = description;
270         this.siUnit = siUnit;
271 
272         byteTypeMap.put(this.code, this);
273         unitTypeMap.put(this.djunitsType, this);
274     }
275 
276     /**
277      * Return the unit type belonging to the byte code.
278      * @param code the code to search for.
279      * @return the unit type, or null if not found.
280      */
281     public static QuantityType getUnitType(final byte code)
282     {
283         return byteTypeMap.get(code);
284     }
285 
286     /**
287      * Return the unit class belonging to the byte code.
288      * @param code the code to search for.
289      * @return the unit class, or null if not found.
290      */
291     public static Class<? extends Unit<?>> getUnitClass(final byte code)
292     {
293         QuantityType type = byteTypeMap.get(code);
294         return type == null ? null : type.getDjunitsType();
295     }
296 
297     /**
298      * Return the unit type belonging to the unit class.
299      * @param unit the unit to search for.
300      * @return the unit type, or null if not found.
301      * @param <U> the Unit
302      */
303     public static <U extends Unit<U>> QuantityType getUnitType(final U unit)
304     {
305         return unitTypeMap.get(unit.getClass());
306     }
307 
308     /**
309      * Return the byte code belonging to the unit class.
310      * @param unit the unit to search for.
311      * @return the unit type code, or null if not found.
312      * @param <U> the Unit
313      * @throws IllegalArgumentException when unit type could not be found
314      */
315     public static <U extends Unit<U>> byte getUnitCode(final U unit)
316     {
317         QuantityType type = unitTypeMap.get(unit.getClass());
318         if (type == null)
319         {
320             throw new IllegalArgumentException("Could not find unit type for unit " + unit + " in unitTypeMap");
321         }
322         return type.getCode();
323     }
324 
325     /**
326      * Retrieve the byte code of this UnitType.
327      * @return the byte code of this UnitType
328      */
329     public final byte getCode()
330     {
331         return this.code;
332     }
333 
334     /**
335      * Retrieve the DJUNITS type of this UnitType.
336      * @return the DJUNITS type of this UnitType
337      */
338     public final Class<? extends Unit<?>> getDjunitsType()
339     {
340         return this.djunitsType;
341     }
342 
343     /**
344      * Retrieve the name of the UnitType.
345      * @return the name of this UnitType
346      */
347     public final String getName()
348     {
349         return this.name;
350     }
351 
352     /**
353      * Retrieve the description of this UnitType.
354      * @return the description of this UnitType
355      */
356     public final String getDescription()
357     {
358         return this.description;
359     }
360 
361     /**
362      * Retrieve the SI unit of this UnitType.
363      * @return String the SI unit of this UnitType
364      */
365     public final String getSiUnit()
366     {
367         return this.siUnit;
368     }
369 
370     @SuppressWarnings("checkstyle:designforextension")
371     @Override
372     public int hashCode()
373     {
374         return Objects.hash(this.code, this.description, this.djunitsType, this.name, this.siUnit);
375     }
376 
377     @SuppressWarnings({"checkstyle:designforextension", "needbraces"})
378     @Override
379     public boolean equals(final Object obj)
380     {
381         if (this == obj)
382             return true;
383         if (obj == null)
384             return false;
385         if (getClass() != obj.getClass())
386             return false;
387         QuantityType other = (QuantityType) obj;
388         return this.code == other.code && Objects.equals(this.description, other.description)
389                 && Objects.equals(this.djunitsType, other.djunitsType) && Objects.equals(this.name, other.name)
390                 && Objects.equals(this.siUnit, other.siUnit);
391     }
392 
393     @Override
394     @SuppressWarnings("checkstyle:designforextension")
395     public String toString()
396     {
397         return "UnitType [code=" + this.code + ", name=" + this.name + ", description=" + this.description + ", siUnit="
398                 + this.siUnit + "]";
399     }
400 
401 }