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