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.DurationUnit;
7 import org.djunits.unit.FrequencyUnit;
8 import org.djunits.unit.UNITS;
9 import org.djunits.value.vdouble.scalar.Duration;
10 import org.djunits.value.vdouble.scalar.Frequency;
11 import org.opentrafficsim.core.network.NetworkException;
12
13
14
15
16
17
18
19
20
21
22
23 public final class DurationUnits implements UNITS
24 {
25
26 public static final Map<String, DurationUnit> DURATION_UNITS = new HashMap<>();
27
28
29 public static final Map<String, FrequencyUnit> FREQUENCY_UNITS = new HashMap<>();
30
31 static
32 {
33 DURATION_UNITS.put("ms", MILLISECOND);
34 DURATION_UNITS.put("s", SECOND);
35 DURATION_UNITS.put("m", MINUTE);
36 DURATION_UNITS.put("min", MINUTE);
37 DURATION_UNITS.put("h", HOUR);
38 DURATION_UNITS.put("hr", HOUR);
39 DURATION_UNITS.put("d", DAY);
40 DURATION_UNITS.put("day", DAY);
41 DURATION_UNITS.put("wk", WEEK);
42 DURATION_UNITS.put("week", WEEK);
43
44 FREQUENCY_UNITS.put("/ms", PER_MILLISECOND);
45 FREQUENCY_UNITS.put("/s", PER_SECOND);
46 FREQUENCY_UNITS.put("/m", PER_MINUTE);
47 FREQUENCY_UNITS.put("/min", PER_MINUTE);
48 FREQUENCY_UNITS.put("/h", PER_HOUR);
49 FREQUENCY_UNITS.put("/hr", PER_HOUR);
50 FREQUENCY_UNITS.put("/d", PER_DAY);
51 FREQUENCY_UNITS.put("/day", PER_DAY);
52 FREQUENCY_UNITS.put("/wk", PER_WEEK);
53 FREQUENCY_UNITS.put("/week", PER_WEEK);
54 }
55
56
57 private DurationUnits()
58 {
59
60 }
61
62
63
64
65
66
67 public static String parseDurationUnit(final String s) throws NetworkException
68 {
69 String u = null;
70 for (String us : DURATION_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 time unit in: " + s);
83 }
84 return u;
85 }
86
87
88
89
90
91
92 public static Duration parseDuration(final String s) throws NetworkException
93 {
94 String us = parseDurationUnit(s);
95 DurationUnit u = DURATION_UNITS.get(us);
96 String sv = s.substring(0, s.indexOf(us));
97 try
98 {
99 double value = Double.parseDouble(sv);
100 return new Duration(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 parseFrequencyUnit(final String s) throws NetworkException
114 {
115 String u = null;
116 for (String us : FREQUENCY_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 frequency unit in: " + s);
129 }
130 return u;
131 }
132
133
134
135
136
137
138 public static Frequency parseFrequency(final String s) throws NetworkException
139 {
140 String us = parseFrequencyUnit(s);
141 FrequencyUnit u = FREQUENCY_UNITS.get(us);
142 String sv = s.substring(0, s.indexOf(us));
143 try
144 {
145 double value = Double.parseDouble(sv);
146 return new Frequency(value, u);
147 }
148 catch (NumberFormatException nfe)
149 {
150 throw new NetworkException("Parsing network: cannot instantiate scalar: " + s, nfe);
151 }
152 }
153
154 }