TimeUnits.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.FrequencyUnit;
  5. import org.djunits.unit.TimeUnit;
  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.djunits.value.vdouble.scalar.Time;
  10. import org.opentrafficsim.core.network.NetworkException;

  11. /**
  12.  * Parser for times and frequencies 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 TimeUnits implements UNITS
  22. {
  23.     /** The time units. */
  24.     public static final Map<String, TimeUnit> TIME_UNITS = new HashMap<>();

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

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

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

  50.     /** Utility class. */
  51.     private TimeUnits()
  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 parseTimeUnit(final String s) throws NetworkException
  61.     {
  62.         String u = null;
  63.         for (String us : TIME_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 time 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 Time parseTime(final String s) throws NetworkException
  85.     {
  86.         String us = parseTimeUnit(s);
  87.         TimeUnit u = TIME_UNITS.get(us);
  88.         String sv = s.substring(0, s.indexOf(us));
  89.         try
  90.         {
  91.             double value = Double.parseDouble(sv);
  92.             return new Time(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 Duration parseDuration(final String s) throws NetworkException
  105.     {
  106.         String us = parseTimeUnit(s);
  107.         TimeUnit u = TIME_UNITS.get(us);
  108.         String sv = s.substring(0, s.indexOf(us));
  109.         try
  110.         {
  111.             double value = Double.parseDouble(sv);
  112.             return new Duration(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 parseFrequencyUnit(final String s) throws NetworkException
  125.     {
  126.         String u = null;
  127.         for (String us : FREQUENCY_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 frequency 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 Frequency parseFrequency(final String s) throws NetworkException
  149.     {
  150.         String us = parseFrequencyUnit(s);
  151.         FrequencyUnit u = FREQUENCY_UNITS.get(us);
  152.         String sv = s.substring(0, s.indexOf(us));
  153.         try
  154.         {
  155.             double value = Double.parseDouble(sv);
  156.             return new Frequency(value, u);
  157.         }
  158.         catch (NumberFormatException nfe)
  159.         {
  160.             throw new NetworkException("Parsing network: cannot instantiate scalar: " + s, nfe);
  161.         }
  162.     }

  163. }