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