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
56
57
58
59
60
61
62 public class SerializationUnits implements Serializable
63 {
64
65 private static final long serialVersionUID = 20170304L;
66
67
68 private static Map<Byte, SerializationUnits> byteTypeMap = new HashMap<>();
69
70
71 private static Map<Class<? extends Unit<?>>, SerializationUnits> unitTypeMap = new HashMap<>();
72
73
74 public static final SerializationUnits DIMENSIONLESS =
75 new SerializationUnits(0, DimensionlessUnit.class, "Dimensionless", "Unit without a dimension", "[]");
76
77
78 public static final SerializationUnits ACCELERATION =
79 new SerializationUnits(1, AccelerationUnit.class, "Acceleration", "Acceleration", "[m/s^2]");
80
81
82 public static final SerializationUnits SOLIDANGLE =
83 new SerializationUnits(2, SolidAngleUnit.class, "SolidAngle", "Solid angle (steradian)", "[sr]");
84
85
86 public static final SerializationUnits ANGLE =
87 new SerializationUnits(3, AngleUnit.class, "Angle", "Angle (relative)", "[rad]");
88
89
90 public static final SerializationUnits DIRECTION =
91 new SerializationUnits(4, DirectionUnit.class, "Direction", "Angle (absolute)", "[rad]");
92
93
94 public static final SerializationUnits AREA = new SerializationUnits(5, AreaUnit.class, "Area", "Area (m2)", "[m^2]");
95
96
97 public static final SerializationUnits DENSITY =
98 new SerializationUnits(6, DensityUnit.class, "Density", "Density based on mass and length", "[kg/m^3]");
99
100
101 public static final SerializationUnits ELECTRICALCHARGE =
102 new SerializationUnits(7, ElectricalChargeUnit.class, "ElectricalCharge", "Electrical charge (Coulomb)", "[s.A]");
103
104
105 public static final SerializationUnits ELECTRICALCURRENT =
106 new SerializationUnits(8, ElectricalCurrentUnit.class, "ElectricalCurrent", "Electrical current (Ampere)", "[A]");
107
108
109 public static final SerializationUnits ELECTRICALPOTENTIAL = new SerializationUnits(9, ElectricalPotentialUnit.class,
110 "ElectricalPotential", "Electrical potential (Volt)", "[kg.m^2/s^3.A]");
111
112
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
117 public static final SerializationUnits ENERGY =
118 new SerializationUnits(11, EnergyUnit.class, "Energy", "Energy (Joule)", "[kg.m^2/s^2]");
119
120
121 public static final SerializationUnits FLOWMASS =
122 new SerializationUnits(12, FlowMassUnit.class, "FlowMass", "Mass flow rate ", "[kg/s]");
123
124
125 public static final SerializationUnits FLOWVOLUME =
126 new SerializationUnits(13, FlowVolumeUnit.class, "FlowVolume", "Volume flow rate", "[m^3/s]");
127
128
129 public static final SerializationUnits FORCE =
130 new SerializationUnits(14, ForceUnit.class, "Force", "Force (Newton)", "[kg.m/s^2]");
131
132
133 public static final SerializationUnits FREQUENCY =
134 new SerializationUnits(15, FrequencyUnit.class, "Frequency", "Frequency (Hz)", "[1/s]");
135
136
137 public static final SerializationUnits LENGTH =
138 new SerializationUnits(16, LengthUnit.class, "Length", "Length (relative)", "[m]");
139
140
141 public static final SerializationUnits POSITION =
142 new SerializationUnits(17, PositionUnit.class, "Position", "Length (absolute)", "[m]");
143
144
145 public static final SerializationUnits LINEARDENSITY =
146 new SerializationUnits(18, LinearDensityUnit.class, "LinearDensity", "Linear density ", "[1/m]");
147
148
149 public static final SerializationUnits MASS = new SerializationUnits(19, MassUnit.class, "Mass", "Mass", "[kg]");
150
151
152 public static final SerializationUnits POWER =
153 new SerializationUnits(20, PowerUnit.class, "Power", "Power (Watt)", "[kg.m^2/s^3]");
154
155
156 public static final SerializationUnits PRESSURE =
157 new SerializationUnits(21, PressureUnit.class, "Pressure", "Pressure (Pascal)", "[kg/m.s^2]");
158
159
160 public static final SerializationUnits SPEED = new SerializationUnits(22, SpeedUnit.class, "Speed", "Speed", "[m/s]");
161
162
163 public static final SerializationUnits TEMPERATURE =
164 new SerializationUnits(23, TemperatureUnit.class, "Temperature", "Temperature (relative)", "[K]");
165
166
167 public static final SerializationUnits ABSOLUTETEMPERATURE =
168 new SerializationUnits(24, AbsoluteTemperatureUnit.class, "AbsoluteTemperature", "Temperature (absolute)", "[K]");
169
170
171 public static final SerializationUnits DURATION =
172 new SerializationUnits(25, DurationUnit.class, "Duration", "Time (relative)", "[s]");
173
174
175 public static final SerializationUnits TIME = new SerializationUnits(26, TimeUnit.class, "Time", "Time (absolute)", "[s]");
176
177
178 public static final SerializationUnits TORQUE =
179 new SerializationUnits(27, TorqueUnit.class, "Torque", "Torque (Newton-meter)", "[kg.m^2/s^2]");
180
181
182 public static final SerializationUnits VOLUME = new SerializationUnits(28, VolumeUnit.class, "Volume", "Volume", "[m^3]");
183
184
185 public static final SerializationUnits ABSORBEDDOSE =
186 new SerializationUnits(28, AbsorbedDoseUnit.class, "Absorbed dose", "Absorbed Dose (Gray)", "[m^2/s^2]");
187
188
189 public static final SerializationUnits AMOUNTOFSUBSTANCE = new SerializationUnits(30, AmountOfSubstanceUnit.class,
190 "Amount of substance", "Amount of substance (mole)", "[mol]");
191
192
193 public static final SerializationUnits CATALYTICACTIVITY = new SerializationUnits(31, CatalyticActivityUnit.class,
194 "Catalytic activity", "Catalytic activity (katal)", "[mol/s]");
195
196
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
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
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
209 public static final SerializationUnits EQUIVALENTDOSE =
210 new SerializationUnits(35, EquivalentDoseUnit.class, "Equivalent dose", "Equivalent dose (Sievert)", "[m^2/s^2]");
211
212
213 public static final SerializationUnits ILLUMINANCE =
214 new SerializationUnits(36, IlluminanceUnit.class, "Illuminance", "Illuminance (lux)", "[sr.cd/m^2]");
215
216
217 public static final SerializationUnits LUMINOUSFLUX =
218 new SerializationUnits(37, LuminousFluxUnit.class, "Luminous flux", "Luminous flux (lumen)", "[sr.cd]");
219
220
221 public static final SerializationUnits LUMINOUSINTENSITY = new SerializationUnits(38, LuminousIntensityUnit.class,
222 "Luminous intensity", "Luminous intensity (candela)", "[cd]");
223
224
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
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
233 public static final SerializationUnits RADIOACTIVITY =
234 new SerializationUnits(41, RadioActivityUnit.class, "Radioactivity", "Radioactivity (Becquerel)", "[1/s]");
235
236
237 public static final SerializationUnits ANGULARACCELERATION = new SerializationUnits(42, AngularAccelerationUnit.class,
238 "AngularAcceleration", "AngularAcceleration", "[rad/s^2]");
239
240
241 public static final SerializationUnits ANGULARVELOCITY =
242 new SerializationUnits(43, AngularVelocityUnit.class, "AngularVelocity", "AngularVelocity", "[rad/s]");
243
244
245 public static final SerializationUnits MOMENTUM =
246 new SerializationUnits(44, MomentumUnit.class, "Momentum", "Momentum", "[rad/s^2]");
247
248
249 private final byte code;
250
251
252 private final Class<? extends Unit<?>> djunitsType;
253
254
255 private final String name;
256
257
258 private final String description;
259
260
261 private final String siUnit;
262
263
264
265
266
267
268
269
270
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
287
288
289
290 public static SerializationUnits getUnitType(final byte code)
291 {
292 return byteTypeMap.get(code);
293 }
294
295
296
297
298
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
308
309
310
311
312 public static <U extends Unit<U>> SerializationUnits getUnitType(final U unit)
313 {
314 return unitTypeMap.get(unit.getClass());
315 }
316
317
318
319
320
321
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
331
332
333 public final byte getCode()
334 {
335 return this.code;
336 }
337
338
339
340
341
342 public final Class<? extends Unit<?>> getDjunitsType()
343 {
344 return this.djunitsType;
345 }
346
347
348
349
350
351 public final String getName()
352 {
353 return this.name;
354 }
355
356
357
358
359
360 public final String getDescription()
361 {
362 return this.description;
363 }
364
365
366
367
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 }