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