SpeedUnits.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.SpeedUnit;
  5. import org.djunits.unit.UNITS;
  6. import org.djunits.value.vdouble.scalar.Speed;
  7. import org.opentrafficsim.core.network.NetworkException;

  8. /**
  9.  * Parser for speed with unit.
  10.  * <p>
  11.  * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  12.  * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  13.  * <p>
  14.  * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
  15.  * initial version Jul 23, 2015 <br>
  16.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  17.  */
  18. public final class SpeedUnits implements UNITS
  19. {
  20.     /** The speed units. */
  21.     public static final Map<String, SpeedUnit> SPEED_UNITS = new HashMap<>();
  22.     static
  23.     {
  24.         SPEED_UNITS.put("km/h", KM_PER_HOUR);
  25.         SPEED_UNITS.put("mi/h", MILE_PER_HOUR);
  26.         SPEED_UNITS.put("m/s", METER_PER_SECOND);
  27.         SPEED_UNITS.put("ft/s", FOOT_PER_SECOND);
  28.     }

  29.     /** Utility class. */
  30.     private SpeedUnits()
  31.     {
  32.         // do not instantiate
  33.     }

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

  58.     /**
  59.      * @param s the string to parse
  60.      * @return the next value.
  61.      * @throws NetworkException when parsing fails
  62.      */
  63.     public static Speed parseSpeed(final String s) throws NetworkException
  64.     {
  65.         String us = parseSpeedUnit(s);
  66.         SpeedUnit u = SPEED_UNITS.get(us);
  67.         String sv = s.substring(0, s.indexOf(us));
  68.         try
  69.         {
  70.             double value = Double.parseDouble(sv);
  71.             return new Speed(value, u);
  72.         }
  73.         catch (NumberFormatException nfe)
  74.         {
  75.             throw new NetworkException("Parsing network: cannot instantiate scalar: " + s, nfe);
  76.         }
  77.     }
  78. }