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.LengthUnit;
7 import org.djunits.unit.LinearDensityUnit;
8 import org.djunits.unit.UNITS;
9 import org.djunits.value.vdouble.scalar.Length;
10 import org.djunits.value.vdouble.scalar.LinearDensity;
11 import org.opentrafficsim.core.network.NetworkException;
12
13
14
15
16
17
18
19
20
21
22
23 public final class LengthUnits implements UNITS
24 {
25
26 public static final Map<String, LengthUnit> LENGTH_UNITS = new HashMap<>();
27
28
29 public static final Map<String, LinearDensityUnit> PER_LENGTH_UNITS = new HashMap<>();
30
31 static
32 {
33 LENGTH_UNITS.put("mm", MILLIMETER);
34 LENGTH_UNITS.put("cm", CENTIMETER);
35 LENGTH_UNITS.put("dm", DECIMETER);
36 LENGTH_UNITS.put("dam", DEKAMETER);
37 LENGTH_UNITS.put("hm", HECTOMETER);
38 LENGTH_UNITS.put("m", METER);
39 LENGTH_UNITS.put("km", KILOMETER);
40 LENGTH_UNITS.put("mi", MILE);
41 LENGTH_UNITS.put("y", YARD);
42 LENGTH_UNITS.put("ft", FOOT);
43
44 PER_LENGTH_UNITS.put("/mm", PER_MILLIMETER);
45 PER_LENGTH_UNITS.put("/cm", PER_CENTIMETER);
46 PER_LENGTH_UNITS.put("/dm", PER_DECIMETER);
47 PER_LENGTH_UNITS.put("/dam", PER_DEKAMETER);
48 PER_LENGTH_UNITS.put("/hm", PER_HECTOMETER);
49 PER_LENGTH_UNITS.put("/m", PER_METER);
50 PER_LENGTH_UNITS.put("/km", PER_KILOMETER);
51 PER_LENGTH_UNITS.put("/mi", PER_MILE);
52 PER_LENGTH_UNITS.put("/y", PER_YARD);
53 PER_LENGTH_UNITS.put("/ft", PER_FOOT);
54 }
55
56
57 private LengthUnits()
58 {
59
60 }
61
62
63
64
65
66
67 public static String parseLengthUnit(final String s) throws NetworkException
68 {
69 String u = null;
70 for (String us : LENGTH_UNITS.keySet())
71 {
72 if (s.toString().contains(us))
73 {
74 if (u == null || us.length() > u.length())
75 {
76 u = us;
77 }
78 }
79 }
80 if (u == null)
81 {
82 throw new NetworkException("Parsing network: cannot instantiate length unit in: " + s);
83 }
84 return u;
85 }
86
87
88
89
90
91
92 public static Length parseLength(final String s) throws NetworkException
93 {
94 String us = parseLengthUnit(s);
95 LengthUnit u = LENGTH_UNITS.get(us);
96 String sv = s.substring(0, s.indexOf(us));
97 try
98 {
99 double value = Double.parseDouble(sv);
100 return new Length(value, u);
101 }
102 catch (NumberFormatException nfe)
103 {
104 throw new NetworkException("Parsing network: cannot instantiate scalar: " + s, nfe);
105 }
106 }
107
108
109
110
111
112
113 public static String parsePerLengthUnit(final String s) throws NetworkException
114 {
115 String u = null;
116 for (String us : PER_LENGTH_UNITS.keySet())
117 {
118 if (s.toString().contains(us))
119 {
120 if (u == null || us.length() > u.length())
121 {
122 u = us;
123 }
124 }
125 }
126 if (u == null)
127 {
128 throw new NetworkException("Parsing network: cannot instantiate per-length unit in: " + s);
129 }
130 return u;
131 }
132
133
134
135
136
137
138 public static LinearDensity parseLinearDensity(final String s) throws NetworkException
139 {
140 String us = parsePerLengthUnit(s);
141 LinearDensityUnit u = PER_LENGTH_UNITS.get(us);
142 String sv = s.substring(0, s.indexOf(us));
143 try
144 {
145 double value = Double.parseDouble(sv);
146 return new LinearDensity(value, u);
147 }
148 catch (NumberFormatException nfe)
149 {
150 throw new NetworkException("Parsing network: cannot instantiate scalar: " + s, nfe);
151 }
152 }
153
154 }