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