1 package org.opentrafficsim.core.network.factory.xml.units;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import org.djunits.unit.AccelerationUnit;
7 import org.djunits.unit.UNITS;
8 import org.djunits.value.vdouble.scalar.Acceleration;
9 import org.opentrafficsim.core.network.NetworkException;
10
11
12
13
14
15
16
17
18
19
20
21 public final class AccelerationUnits implements UNITS
22 {
23
24 public static final Map<String, AccelerationUnit> ACCELERATION_UNITS = new HashMap<>();
25 static
26 {
27 ACCELERATION_UNITS.put("km/h^2", KM_PER_HOUR_2);
28 ACCELERATION_UNITS.put("mi/h^2", MILE_PER_HOUR_2);
29 ACCELERATION_UNITS.put("m/s^2", METER_PER_SECOND_2);
30 ACCELERATION_UNITS.put("ft/s^2", FOOT_PER_SECOND_2);
31 }
32
33
34 private AccelerationUnits()
35 {
36
37 }
38
39
40
41
42
43
44 public static String parseAccelerationUnit(final String s) throws NetworkException
45 {
46 String u = null;
47 for (String us : ACCELERATION_UNITS.keySet())
48 {
49 if (s.toString().contains(us))
50 {
51 if (u == null || us.length() > u.length())
52 {
53 u = us;
54 }
55 }
56 }
57 if (u == null)
58 {
59 throw new NetworkException("Parsing network: cannot instantiate acceleration unit in: " + s);
60 }
61 return u;
62 }
63
64
65
66
67
68
69 public static Acceleration parseAcceleration(final String s) throws NetworkException
70 {
71 String us = parseAccelerationUnit(s);
72 AccelerationUnit u = ACCELERATION_UNITS.get(us);
73 String sv = s.substring(0, s.indexOf(us));
74 try
75 {
76 double value = Double.parseDouble(sv);
77 return new Acceleration(value, u);
78 }
79 catch (NumberFormatException nfe)
80 {
81 throw new NetworkException("Parsing network: cannot instantiate scalar: " + s, nfe);
82 }
83 }
84 }