View Javadoc
1   package org.opentrafficsim.xml.bindings;
2   
3   import org.djutils.exceptions.Throw;
4   import org.opentrafficsim.xml.bindings.types.DoubleType;
5   
6   /**
7    * Adapter for Double expression type (unit interval).
8    * <p>
9    * Copyright (c) 2023-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/wjschakel">Wouter Schakel</a>
13   */
14  public class DoubleUnitIntervalAdapter extends ExpressionAdapter<Double, DoubleType>
15  {
16  
17      /** {@inheritDoc} */
18      @Override
19      public DoubleType unmarshal(final String field)
20      {
21          if (isExpression(field))
22          {
23              return new DoubleType(trimBrackets(field));
24          }
25          double value = Double.valueOf(field);
26          Throw.when(value < 0.0 || value > 1.0, IllegalArgumentException.class,
27                  "DoubleUnitInterval value %s is not in unit interval.", value);
28          return new DoubleType(value);
29      }
30  
31      /** {@inheritDoc} */
32      @Override
33      public String marshal(final DoubleType value)
34      {
35          Throw.when(!value.isExpression() && (value.getValue() < 0.0 || value.getValue() > 1.0), IllegalArgumentException.class,
36                  "DoubleUnitInterval value %s is not in unit interval.", value.getValue());
37          return super.marshal(value);
38      }
39  
40  }