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.PositionUnit;
7   import org.djunits.value.vdouble.scalar.Position;
8   import org.opentrafficsim.core.network.NetworkException;
9   
10  /**
11   * Parser for position with unit.
12   * <p>
13   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
14   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
15   * <p>
16   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
17   * initial version Jul 23, 2015 <br>
18   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
19   */
20  public final class PositionUnits
21  {
22      /** The position units. */
23      public static final Map<String, PositionUnit> POSITION_UNITS = new HashMap<>();
24  
25      static
26      {
27          POSITION_UNITS.put("mm", PositionUnit.MILLIMETER);
28          POSITION_UNITS.put("cm", PositionUnit.CENTIMETER);
29          POSITION_UNITS.put("dm", PositionUnit.DECIMETER);
30          POSITION_UNITS.put("dam", PositionUnit.DEKAMETER);
31          POSITION_UNITS.put("hm", PositionUnit.HECTOMETER);
32          POSITION_UNITS.put("m", PositionUnit.METER);
33          POSITION_UNITS.put("km", PositionUnit.KILOMETER);
34          POSITION_UNITS.put("mi", PositionUnit.MILE);
35          POSITION_UNITS.put("y", PositionUnit.YARD);
36          POSITION_UNITS.put("ft", PositionUnit.FOOT);
37      }
38  
39      /** Utility class. */
40      private PositionUnits()
41      {
42          // do not instantiate
43      }
44  
45      /**
46       * @param s String; the string to parse
47       * @return the unit as a String in the Map.
48       * @throws NetworkException when parsing fails
49       */
50      public static String parsePositionUnit(final String s) throws NetworkException
51      {
52          String u = null;
53          for (String us : POSITION_UNITS.keySet())
54          {
55              if (s.toString().contains(us))
56              {
57                  if (u == null || us.length() > u.length())
58                  {
59                      u = us;
60                  }
61              }
62          }
63          if (u == null)
64          {
65              throw new NetworkException("Parsing network: cannot instantiate length unit in: " + s);
66          }
67          return u;
68      }
69  
70      /**
71       * @param s String; the string to parse
72       * @return the next value.
73       * @throws NetworkException when parsing fails
74       */
75      public static Position parsePosition(final String s) throws NetworkException
76      {
77          String us = parsePositionUnit(s);
78          PositionUnit u = POSITION_UNITS.get(us);
79          String sv = s.substring(0, s.indexOf(us));
80          try
81          {
82              double value = Double.parseDouble(sv);
83              return new Position(value, u);
84          }
85          catch (NumberFormatException nfe)
86          {
87              throw new NetworkException("Parsing network: cannot instantiate scalar: " + s, nfe);
88          }
89      }
90  
91  }