PositiveTimeAdapter.java

  1. package org.opentrafficsim.xml.bindings;

  2. import org.djunits.value.vdouble.scalar.Time;
  3. import org.djutils.exceptions.Throw;
  4. import org.djutils.logger.CategoryLogger;
  5. import org.opentrafficsim.xml.bindings.types.TimeType;

  6. /**
  7.  * TimeAdapter converts between the XML String for a Time and the DJUnits Time (positive).
  8.  * <p>
  9.  * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  10.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  11.  * </p>
  12.  * @author <a href="https://github.com/averbraeck" target="_blank">Alexander Verbraeck</a>
  13.  * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
  14.  */
  15. public class PositiveTimeAdapter extends ScalarAdapter<Time, TimeType>
  16. {

  17.     /** {@inheritDoc} */
  18.     @Override
  19.     public TimeType unmarshal(final String field)
  20.     {
  21.         if (isExpression(field))
  22.         {
  23.             return new TimeType(trimBrackets(field));
  24.         }
  25.         try
  26.         {
  27.             Time value = Time.valueOf(field);
  28.             Throw.when(value.lt0(), IllegalArgumentException.class, "PositiveTime value %s is not a positive value.", value);
  29.             return new TimeType(value);
  30.         }
  31.         catch (Exception exception)
  32.         {
  33.             CategoryLogger.always().error(exception, "Problem parsing Time '" + field + "'");
  34.             throw exception;
  35.         }
  36.     }

  37.     /** {@inheritDoc} */
  38.     @Override
  39.     public String marshal(final TimeType value)
  40.     {
  41.         Throw.when(!value.isExpression() && value.getValue().lt0(), IllegalArgumentException.class,
  42.                 "PositiveTime value %s is not a positive value.", value.getValue());
  43.         return super.marshal(value);
  44.     }

  45. }