View Javadoc
1   package org.sim0mq.message.types;
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 Sim0MQUnitType 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, Sim0MQUnitType> byteTypeMap = new HashMap<>();
62  
63      /** the unit types from class to type. */
64      private static Map<Class<? extends Unit<?>>, Sim0MQUnitType> unitTypeMap = new HashMap<>();
65  
66      /** Dimensionless unit type with code 0. */
67      public static final Sim0MQUnitType DIMENSIONLESS =
68              new Sim0MQUnitType(0, DimensionlessUnit.class, "Dimensionless", "Unit without a dimension", "[]");
69  
70      /** Acceleration unit type with code 1. */
71      public static final Sim0MQUnitType ACCELERATION =
72              new Sim0MQUnitType(1, AccelerationUnit.class, "Acceleration", "Acceleration", "[m/s^2]");
73  
74      /** AngleSolid unit type with code 2. */
75      public static final Sim0MQUnitType ANGLESOLID =
76              new Sim0MQUnitType(2, AngleSolidUnit.class, "AngleSolid", "Solid angle ", "[steradian]");
77  
78      /** Angle unit type with code 3. */
79      public static final Sim0MQUnitType ANGLE = new Sim0MQUnitType(3, AngleUnit.class, "Angle", "Angle (relative)", "[rad]");
80  
81      /** Direction unit type with code 4. */
82      public static final Sim0MQUnitType DIRECTION =
83              new Sim0MQUnitType(4, DirectionUnit.class, "Direction", "Angle  (absolute)", "[rad]");
84  
85      /** Area unit type with code 5. */
86      public static final Sim0MQUnitType AREA = new Sim0MQUnitType(5, AreaUnit.class, "Area", "Area", "[m^2]");
87  
88      /** Density unit type with code 6. */
89      public static final Sim0MQUnitType DENSITY =
90              new Sim0MQUnitType(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 Sim0MQUnitType ELECTRICALCHARGE =
94              new Sim0MQUnitType(7, ElectricalChargeUnit.class, "ElectricalCharge", "Electrical charge (Coulomb)", "[sA]");
95  
96      /** ElectricalCurrent unit type with code 8. */
97      public static final Sim0MQUnitType ELECTRICALCURRENT =
98              new Sim0MQUnitType(8, ElectricalCurrentUnit.class, "ElectricalCurrent", "Electrical current (Ampere)", "[A]");
99  
100     /** ElectricalPotential unit type with code 9. */
101     public static final Sim0MQUnitType ELECTRICALPOTENTIAL = new Sim0MQUnitType(9, ElectricalPotentialUnit.class,
102             "ElectricalPotential", "Electrical potential (Volt)", "[kgm^2/s^3A]");
103 
104     /** ElectricalResistance unit type with code 10. */
105     public static final Sim0MQUnitType ELECTRICALRESISTANCE = new Sim0MQUnitType(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 Sim0MQUnitType ENERGY =
110             new Sim0MQUnitType(11, EnergyUnit.class, "Energy", "Energy (Joule)", "[kgm^2/s^2]");
111 
112     /** FlowMass unit type with code 12. */
113     public static final Sim0MQUnitType FLOWMASS =
114             new Sim0MQUnitType(12, FlowMassUnit.class, "FlowMass", "Mass flow rate ", "[kg/s]");
115 
116     /** FlowVolume unit type with code 13. */
117     public static final Sim0MQUnitType FLOWVOLUME =
118             new Sim0MQUnitType(13, FlowVolumeUnit.class, "FlowVolume", "Volume flow rate", "[m^3/s]");
119 
120     /** Force unit type with code 14. */
121     public static final Sim0MQUnitType FORCE = new Sim0MQUnitType(14, ForceUnit.class, "Force", "Force (Newton)", "[kgm/s^2]");
122 
123     /** Frequency unit type with code 15. */
124     public static final Sim0MQUnitType FREQUENCY =
125             new Sim0MQUnitType(15, FrequencyUnit.class, "Frequency", "Frequency (Hz)", "[1/s]");
126 
127     /** Length unit type with code 16. */
128     public static final Sim0MQUnitType LENGTH = new Sim0MQUnitType(16, LengthUnit.class, "Length", "Length (relative)", "[m]");
129 
130     /** Position unit type with code 17. */
131     public static final Sim0MQUnitType POSITION =
132             new Sim0MQUnitType(17, PositionUnit.class, "Position", "Length (absolute)", "[m]");
133 
134     /** LinearDensity unit type with code 18. */
135     public static final Sim0MQUnitType LINEARDENSITY =
136             new Sim0MQUnitType(18, LinearDensityUnit.class, "LinearDensity", "Linear density ", "[1/m]");
137 
138     /** Mass unit type with code 19. */
139     public static final Sim0MQUnitType MASS = new Sim0MQUnitType(19, MassUnit.class, "Mass", "Mass", "[kg]");
140 
141     /** Power unit type with code 20. */
142     public static final Sim0MQUnitType POWER = new Sim0MQUnitType(20, PowerUnit.class, "Power", "Power (Watt)", "[kgm^2/s^3]");
143 
144     /** Pressure unit type with code 21. */
145     public static final Sim0MQUnitType PRESSURE =
146             new Sim0MQUnitType(21, PressureUnit.class, "Pressure", "Pressure (Pascal)", "[kg/ms^2]");
147 
148     /** Speed unit type with code 22. */
149     public static final Sim0MQUnitType SPEED = new Sim0MQUnitType(22, SpeedUnit.class, "Speed", "Speed", "[m/s]");
150 
151     /** Temperature unit type with code 23. */
152     public static final Sim0MQUnitType TEMPERATURE =
153             new Sim0MQUnitType(23, TemperatureUnit.class, "Temperature", "Temperature (relative)", "[K]");
154 
155     /** AbsoluteTemperature unit type with code 24. */
156     public static final Sim0MQUnitType ABSOLUTETEMPERATURE =
157             new Sim0MQUnitType(24, AbsoluteTemperatureUnit.class, "AbsoluteTemperature", "Temperature (absolute)", "[K]");
158 
159     /** Duration unit type with code 25. */
160     public static final Sim0MQUnitType DURATION = new Sim0MQUnitType(25, DurationUnit.class, "Duration", "Time (relative)", "[s]");
161 
162     /** Time unit type with code 26. */
163     public static final Sim0MQUnitType TIME = new Sim0MQUnitType(26, TimeUnit.class, "Time", "Time (absolute)", "[s]");
164 
165     /** Torque unit type with code 27. */
166     public static final Sim0MQUnitType TORQUE =
167             new Sim0MQUnitType(27, TorqueUnit.class, "Torque", "Torque (Newton-meter)", "[kgm^2/s^2]");
168 
169     /** Volume unit type with code 28. */
170     public static final Sim0MQUnitType VOLUME = new Sim0MQUnitType(28, VolumeUnit.class, "Volume", "Volume", "[m^3]");
171 
172     /** Money unit type with code 100. */
173     public static final Sim0MQUnitType MONEY =
174             new Sim0MQUnitType(100, MoneyUnit.class, "Money", "Money (cost in e.g., $, €, ...)", "[$]");
175 
176     /** MoneyPerArea unit type with code 101. */
177     public static final Sim0MQUnitType MONEYPERAREA =
178             new Sim0MQUnitType(101, MoneyPerAreaUnit.class, "MoneyPerArea", "Money/Area (cost/m^2)", "[$/m^2]");
179 
180     /** MoneyPerEnergy unit type with code 102. */
181     public static final Sim0MQUnitType MONEYPERENERGY =
182             new Sim0MQUnitType(102, MoneyPerEnergyUnit.class, "MoneyPerEnergy", "Money/Energy (cost/W)", "[$s^3/kgm^2]");
183 
184     /** MoneyPerLength unit type with code 103. */
185     public static final Sim0MQUnitType MONEYPERLENGTH =
186             new Sim0MQUnitType(103, MoneyPerLengthUnit.class, "MoneyPerLength", "Money/Length (cost/m)", "[$/m]");
187 
188     /** MoneyPerMass unit type with code 104. */
189     public static final Sim0MQUnitType MONEYPERMASS =
190             new Sim0MQUnitType(104, MoneyPerMassUnit.class, "MoneyPerMass", "Money/Mass (cost/kg)", "[$/kg]");
191 
192     /** MoneyPerDuration unit type with code 105. */
193     public static final Sim0MQUnitType MONEYPERDURATION =
194             new Sim0MQUnitType(105, MoneyPerDurationUnit.class, "MoneyPerDuration", "Money/Duration (cost/s)", "[$/s]");
195 
196     /** MoneyPerVolume unit type with code 106. */
197     public static final Sim0MQUnitType MONEYPERVOLUME =
198             new Sim0MQUnitType(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      * @param code the byte code of the unit provided as an int
217      * @param djunitsType the djunits data type
218      * @param name the unit name
219      * @param description the unit description
220      * @param siUnit the SI or default unit in SI-elements
221      * @param <U> the Unit
222      */
223     public <U extends Unit<U>> Sim0MQUnitType(final int code, final Class<U> djunitsType, final String name,
224             final String description, final String siUnit)
225     {
226         super();
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 the code to search for.
240      * @return the unit type, or null if not found.
241      */
242     public static Sim0MQUnitType 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 the code to search for.
250      * @return the unit class, or null if not found.
251      */
252     public static Class<? extends Unit<?>> getUnitClass(final byte code)
253     {
254         Sim0MQUnitType 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 the unit to search for.
261      * @return the unit type, or null if not found.
262      * @param <U> the Unit
263      */
264     public static <U extends Unit<U>> Sim0MQUnitType 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 the unit to search for.
272      * @return the unit type, 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         Sim0MQUnitType type = unitTypeMap.get(unit.getClass());
278         return type == null ? null : type.getCode();
279     }
280 
281     /**
282      * @return code
283      */
284     public final byte getCode()
285     {
286         return this.code;
287     }
288 
289     /**
290      * @return djunitsType
291      */
292     public final Class<? extends Unit<?>> getDjunitsType()
293     {
294         return this.djunitsType;
295     }
296 
297     /**
298      * @return name
299      */
300     public final String getName()
301     {
302         return this.name;
303     }
304 
305     /**
306      * @return description
307      */
308     public final String getDescription()
309     {
310         return this.description;
311     }
312 
313     /**
314      * @return siUnit
315      */
316     public final String getSiUnit()
317     {
318         return this.siUnit;
319     }
320 
321     /** {@inheritDoc} */
322     @Override
323     @SuppressWarnings("checkstyle:designforextension")
324     public int hashCode()
325     {
326         final int prime = 31;
327         int result = 1;
328         result = prime * result + this.code;
329         result = prime * result + ((this.description == null) ? 0 : this.description.hashCode());
330         result = prime * result + ((this.djunitsType == null) ? 0 : this.djunitsType.hashCode());
331         result = prime * result + ((this.name == null) ? 0 : this.name.hashCode());
332         result = prime * result + ((this.siUnit == null) ? 0 : this.siUnit.hashCode());
333         return result;
334     }
335 
336     /** {@inheritDoc} */
337     @Override
338     @SuppressWarnings({ "checkstyle:designforextension", "needbraces" })
339     public boolean equals(final Object obj)
340     {
341         if (this == obj)
342             return true;
343         if (obj == null)
344             return false;
345         if (getClass() != obj.getClass())
346             return false;
347         Sim0MQUnitType other = (Sim0MQUnitType) obj;
348         if (this.code != other.code)
349             return false;
350         if (this.description == null)
351         {
352             if (other.description != null)
353                 return false;
354         }
355         else if (!this.description.equals(other.description))
356             return false;
357         if (this.djunitsType == null)
358         {
359             if (other.djunitsType != null)
360                 return false;
361         }
362         else if (!this.djunitsType.equals(other.djunitsType))
363             return false;
364         if (this.name == null)
365         {
366             if (other.name != null)
367                 return false;
368         }
369         else if (!this.name.equals(other.name))
370             return false;
371         if (this.siUnit == null)
372         {
373             if (other.siUnit != null)
374                 return false;
375         }
376         else if (!this.siUnit.equals(other.siUnit))
377             return false;
378         return true;
379     }
380 
381     /** {@inheritDoc} */
382     @Override
383     @SuppressWarnings("checkstyle:designforextension")
384     public String toString()
385     {
386         return "Sim0MQUnitType [code=" + this.code + ", name=" + this.name + ", description=" + this.description + ", siUnit="
387                 + this.siUnit + "]";
388     }
389 
390 }