DoubleUnitIntervalAdapter.java

  1. package org.opentrafficsim.xml.bindings;

  2. import org.djutils.exceptions.Throw;
  3. import org.opentrafficsim.xml.bindings.types.DoubleType;

  4. /**
  5.  * Adapter for Double expression type (unit interval).
  6.  * <p>
  7.  * Copyright (c) 2023-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  8.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  9.  * </p>
  10.  * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
  11.  */
  12. public class DoubleUnitIntervalAdapter extends ExpressionAdapter<Double, DoubleType>
  13. {

  14.     /** {@inheritDoc} */
  15.     @Override
  16.     public DoubleType unmarshal(final String field)
  17.     {
  18.         if (isExpression(field))
  19.         {
  20.             return new DoubleType(trimBrackets(field));
  21.         }
  22.         double value = Double.valueOf(field);
  23.         Throw.when(value < 0.0 || value > 1.0, IllegalArgumentException.class,
  24.                 "DoubleUnitInterval value %s is not in unit interval.", value);
  25.         return new DoubleType(value);
  26.     }

  27.     /** {@inheritDoc} */
  28.     @Override
  29.     public String marshal(final DoubleType value)
  30.     {
  31.         Throw.when(!value.isExpression() && (value.getValue() < 0.0 || value.getValue() > 1.0), IllegalArgumentException.class,
  32.                 "DoubleUnitInterval value %s is not in unit interval.", value.getValue());
  33.         return super.marshal(value);
  34.     }

  35. }