View Javadoc
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   * Parser for durations and frequencies with unit.
15   * <p>
16   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
17   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
18   * <p>
19   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
20   * initial version Jul 23, 2015 <br>
21   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
22   */
23  public final class DurationUnits implements UNITS
24  {
25      /** The time units. */
26      public static final Map<String, DurationUnit> DURATION_UNITS = new HashMap<>();
27  
28      /** The per time units. */
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      /** Utility class. */
57      private DurationUnits()
58      {
59          // do not instantiate
60      }
61  
62      /**
63       * @param s String; the string to parse
64       * @return the unit as a String in the Map.
65       * @throws NetworkException when parsing fails
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       * @param s String; the string to parse
89       * @return the next value.
90       * @throws NetworkException when parsing fails
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      * @param s String; the string to parse
110      * @return the unit as a String in the Map.
111      * @throws NetworkException when parsing fails
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      * @param s String; the string to parse
135      * @return the next value.
136      * @throws NetworkException when parsing fails
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 }