PositionUnits.java

  1. package org.opentrafficsim.core.network.factory.xml.units;

  2. import java.util.HashMap;
  3. import java.util.Map;

  4. import org.djunits.unit.PositionUnit;
  5. import org.djunits.value.vdouble.scalar.Position;
  6. import org.opentrafficsim.core.network.NetworkException;

  7. /**
  8.  * Parser for position with unit.
  9.  * <p>
  10.  * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  11.  * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  12.  * <p>
  13.  * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
  14.  * initial version Jul 23, 2015 <br>
  15.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  16.  */
  17. public final class PositionUnits
  18. {
  19.     /** The position units. */
  20.     public static final Map<String, PositionUnit> POSITION_UNITS = new HashMap<>();

  21.     static
  22.     {
  23.         POSITION_UNITS.put("mm", PositionUnit.MILLIMETER);
  24.         POSITION_UNITS.put("cm", PositionUnit.CENTIMETER);
  25.         POSITION_UNITS.put("dm", PositionUnit.DECIMETER);
  26.         POSITION_UNITS.put("dam", PositionUnit.DEKAMETER);
  27.         POSITION_UNITS.put("hm", PositionUnit.HECTOMETER);
  28.         POSITION_UNITS.put("m", PositionUnit.METER);
  29.         POSITION_UNITS.put("km", PositionUnit.KILOMETER);
  30.         POSITION_UNITS.put("mi", PositionUnit.MILE);
  31.         POSITION_UNITS.put("y", PositionUnit.YARD);
  32.         POSITION_UNITS.put("ft", PositionUnit.FOOT);
  33.     }

  34.     /** Utility class. */
  35.     private PositionUnits()
  36.     {
  37.         // do not instantiate
  38.     }

  39.     /**
  40.      * @param s String; the string to parse
  41.      * @return the unit as a String in the Map.
  42.      * @throws NetworkException when parsing fails
  43.      */
  44.     public static String parsePositionUnit(final String s) throws NetworkException
  45.     {
  46.         String u = null;
  47.         for (String us : POSITION_UNITS.keySet())
  48.         {
  49.             if (s.toString().contains(us))
  50.             {
  51.                 if (u == null || us.length() > u.length())
  52.                 {
  53.                     u = us;
  54.                 }
  55.             }
  56.         }
  57.         if (u == null)
  58.         {
  59.             throw new NetworkException("Parsing network: cannot instantiate length unit in: " + s);
  60.         }
  61.         return u;
  62.     }

  63.     /**
  64.      * @param s String; the string to parse
  65.      * @return the next value.
  66.      * @throws NetworkException when parsing fails
  67.      */
  68.     public static Position parsePosition(final String s) throws NetworkException
  69.     {
  70.         String us = parsePositionUnit(s);
  71.         PositionUnit u = POSITION_UNITS.get(us);
  72.         String sv = s.substring(0, s.indexOf(us));
  73.         try
  74.         {
  75.             double value = Double.parseDouble(sv);
  76.             return new Position(value, u);
  77.         }
  78.         catch (NumberFormatException nfe)
  79.         {
  80.             throw new NetworkException("Parsing network: cannot instantiate scalar: " + s, nfe);
  81.         }
  82.     }

  83. }