DurationUnits.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.DurationUnit;
  5. import org.djunits.unit.FrequencyUnit;
  6. import org.djunits.unit.UNITS;
  7. import org.djunits.value.vdouble.scalar.Duration;
  8. import org.djunits.value.vdouble.scalar.Frequency;
  9. import org.opentrafficsim.core.network.NetworkException;

  10. /**
  11.  * Parser for durations and frequencies with unit.
  12.  * <p>
  13.  * Copyright (c) 2013-2018 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 DurationUnits implements UNITS
  21. {
  22.     /** The time units. */
  23.     public static final Map<String, DurationUnit> DURATION_UNITS = new HashMap<>();

  24.     /** The per time units. */
  25.     public static final Map<String, FrequencyUnit> FREQUENCY_UNITS = new HashMap<>();

  26.     static
  27.     {
  28.         DURATION_UNITS.put("ms", MILLISECOND);
  29.         DURATION_UNITS.put("s", SECOND);
  30.         DURATION_UNITS.put("m", MINUTE);
  31.         DURATION_UNITS.put("min", MINUTE);
  32.         DURATION_UNITS.put("h", HOUR);
  33.         DURATION_UNITS.put("hr", HOUR);
  34.         DURATION_UNITS.put("d", DAY);
  35.         DURATION_UNITS.put("day", DAY);
  36.         DURATION_UNITS.put("wk", WEEK);
  37.         DURATION_UNITS.put("week", WEEK);

  38.         FREQUENCY_UNITS.put("/ms", PER_MILLISECOND);
  39.         FREQUENCY_UNITS.put("/s", PER_SECOND);
  40.         FREQUENCY_UNITS.put("/m", PER_MINUTE);
  41.         FREQUENCY_UNITS.put("/min", PER_MINUTE);
  42.         FREQUENCY_UNITS.put("/h", PER_HOUR);
  43.         FREQUENCY_UNITS.put("/hr", PER_HOUR);
  44.         FREQUENCY_UNITS.put("/d", PER_DAY);
  45.         FREQUENCY_UNITS.put("/day", PER_DAY);
  46.         FREQUENCY_UNITS.put("/wk", PER_WEEK);
  47.         FREQUENCY_UNITS.put("/week", PER_WEEK);
  48.     }

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

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

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

  98.     /**
  99.      * @param s the string to parse
  100.      * @return the unit as a String in the Map.
  101.      * @throws NetworkException when parsing fails
  102.      */
  103.     public static String parseFrequencyUnit(final String s) throws NetworkException
  104.     {
  105.         String u = null;
  106.         for (String us : FREQUENCY_UNITS.keySet())
  107.         {
  108.             if (s.toString().contains(us))
  109.             {
  110.                 if (u == null || us.length() > u.length())
  111.                 {
  112.                     u = us;
  113.                 }
  114.             }
  115.         }
  116.         if (u == null)
  117.         {
  118.             throw new NetworkException("Parsing network: cannot instantiate frequency unit in: " + s);
  119.         }
  120.         return u;
  121.     }

  122.     /**
  123.      * @param s the string to parse
  124.      * @return the next value.
  125.      * @throws NetworkException when parsing fails
  126.      */
  127.     public static Frequency parseFrequency(final String s) throws NetworkException
  128.     {
  129.         String us = parseFrequencyUnit(s);
  130.         FrequencyUnit u = FREQUENCY_UNITS.get(us);
  131.         String sv = s.substring(0, s.indexOf(us));
  132.         try
  133.         {
  134.             double value = Double.parseDouble(sv);
  135.             return new Frequency(value, u);
  136.         }
  137.         catch (NumberFormatException nfe)
  138.         {
  139.             throw new NetworkException("Parsing network: cannot instantiate scalar: " + s, nfe);
  140.         }
  141.     }

  142. }