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.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   * Parser for length 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 LengthUnits implements UNITS
24  {
25      /** The length units. */
26      public static final Map<String, LengthUnit> LENGTH_UNITS = new HashMap<>();
27  
28      /** The per-length units. */
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      /** Utility class. */
57      private LengthUnits()
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 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       * @param s String; the string to parse
89       * @return the next value.
90       * @throws NetworkException when parsing fails
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      * @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 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      * @param s String; the string to parse
135      * @return the next value.
136      * @throws NetworkException when parsing fails
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 }