LengthUnits.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.LengthUnit;
  5. import org.djunits.unit.LinearDensityUnit;
  6. import org.djunits.unit.UNITS;
  7. import org.djunits.value.vdouble.scalar.Length;
  8. import org.djunits.value.vdouble.scalar.LinearDensity;
  9. import org.djunits.value.vdouble.scalar.Position;
  10. import org.opentrafficsim.core.network.NetworkException;

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

  25.     /** The per-length units. */
  26.     public static final Map<String, LinearDensityUnit> PER_LENGTH_UNITS = new HashMap<>();

  27.     static
  28.     {
  29.         LENGTH_UNITS.put("mm", MILLIMETER);
  30.         LENGTH_UNITS.put("cm", CENTIMETER);
  31.         LENGTH_UNITS.put("dm", DECIMETER);
  32.         LENGTH_UNITS.put("dam", DEKAMETER);
  33.         LENGTH_UNITS.put("hm", HECTOMETER);
  34.         LENGTH_UNITS.put("m", METER);
  35.         LENGTH_UNITS.put("km", KILOMETER);
  36.         LENGTH_UNITS.put("mi", MILE);
  37.         LENGTH_UNITS.put("y", YARD);
  38.         LENGTH_UNITS.put("ft", FOOT);

  39.         PER_LENGTH_UNITS.put("/mm", PER_MILLIMETER);
  40.         PER_LENGTH_UNITS.put("/cm", PER_CENTIMETER);
  41.         PER_LENGTH_UNITS.put("/dm", PER_DECIMETER);
  42.         PER_LENGTH_UNITS.put("/dam", PER_DEKAMETER);
  43.         PER_LENGTH_UNITS.put("/hm", PER_HECTOMETER);
  44.         PER_LENGTH_UNITS.put("/m", PER_METER);
  45.         PER_LENGTH_UNITS.put("/km", PER_KILOMETER);
  46.         PER_LENGTH_UNITS.put("/mi", PER_MILE);
  47.         PER_LENGTH_UNITS.put("/y", PER_YARD);
  48.         PER_LENGTH_UNITS.put("/ft", PER_FOOT);
  49.     }

  50.     /** Utility class. */
  51.     private LengthUnits()
  52.     {
  53.         // do not instantiate
  54.     }

  55.     /**
  56.      * @param s the string to parse
  57.      * @return the unit as a String in the Map.
  58.      * @throws NetworkException when parsing fails
  59.      */
  60.     public static String parseLengthUnit(final String s) throws NetworkException
  61.     {
  62.         String u = null;
  63.         for (String us : LENGTH_UNITS.keySet())
  64.         {
  65.             if (s.toString().contains(us))
  66.             {
  67.                 if (u == null || us.length() > u.length())
  68.                 {
  69.                     u = us;
  70.                 }
  71.             }
  72.         }
  73.         if (u == null)
  74.         {
  75.             throw new NetworkException("Parsing network: cannot instantiate length unit in: " + s);
  76.         }
  77.         return u;
  78.     }

  79.     /**
  80.      * @param s the string to parse
  81.      * @return the next value.
  82.      * @throws NetworkException when parsing fails
  83.      */
  84.     public static Position parsePosition(final String s) throws NetworkException
  85.     {
  86.         String us = parseLengthUnit(s);
  87.         LengthUnit u = LENGTH_UNITS.get(us);
  88.         String sv = s.substring(0, s.indexOf(us));
  89.         try
  90.         {
  91.             double value = Double.parseDouble(sv);
  92.             return new Position(value, u);
  93.         }
  94.         catch (NumberFormatException nfe)
  95.         {
  96.             throw new NetworkException("Parsing network: cannot instantiate scalar: " + s, nfe);
  97.         }
  98.     }

  99.     /**
  100.      * @param s the string to parse
  101.      * @return the next value.
  102.      * @throws NetworkException when parsing fails
  103.      */
  104.     public static Length parseLength(final String s) throws NetworkException
  105.     {
  106.         String us = parseLengthUnit(s);
  107.         LengthUnit u = LENGTH_UNITS.get(us);
  108.         String sv = s.substring(0, s.indexOf(us));
  109.         try
  110.         {
  111.             double value = Double.parseDouble(sv);
  112.             return new Length(value, u);
  113.         }
  114.         catch (NumberFormatException nfe)
  115.         {
  116.             throw new NetworkException("Parsing network: cannot instantiate scalar: " + s, nfe);
  117.         }
  118.     }

  119.     /**
  120.      * @param s the string to parse
  121.      * @return the unit as a String in the Map.
  122.      * @throws NetworkException when parsing fails
  123.      */
  124.     public static String parsePerLengthUnit(final String s) throws NetworkException
  125.     {
  126.         String u = null;
  127.         for (String us : PER_LENGTH_UNITS.keySet())
  128.         {
  129.             if (s.toString().contains(us))
  130.             {
  131.                 if (u == null || us.length() > u.length())
  132.                 {
  133.                     u = us;
  134.                 }
  135.             }
  136.         }
  137.         if (u == null)
  138.         {
  139.             throw new NetworkException("Parsing network: cannot instantiate per-length unit in: " + s);
  140.         }
  141.         return u;
  142.     }

  143.     /**
  144.      * @param s the string to parse
  145.      * @return the next value.
  146.      * @throws NetworkException when parsing fails
  147.      */
  148.     public static LinearDensity parseLinearDensity(final String s) throws NetworkException
  149.     {
  150.         String us = parsePerLengthUnit(s);
  151.         LinearDensityUnit u = PER_LENGTH_UNITS.get(us);
  152.         String sv = s.substring(0, s.indexOf(us));
  153.         try
  154.         {
  155.             double value = Double.parseDouble(sv);
  156.             return new LinearDensity(value, u);
  157.         }
  158.         catch (NumberFormatException nfe)
  159.         {
  160.             throw new NetworkException("Parsing network: cannot instantiate scalar: " + s, nfe);
  161.         }
  162.     }

  163. }