1 package org.opentrafficsim.xml.bindings;
2
3 import org.djutils.exceptions.Throw;
4 import org.opentrafficsim.xml.bindings.types.DoubleType;
5
6
7
8
9
10
11
12
13
14 public class PositiveFactorAdapter extends ExpressionAdapter<Double, DoubleType>
15 {
16
17
18 @Override
19 public DoubleType unmarshal(final String value)
20 {
21 if (isExpression(value))
22 {
23 return new DoubleType(trimBrackets(value));
24 }
25 double factor = value.endsWith("%") ? Double.parseDouble(value.substring(0, value.length() - 1)) / 100.0
26 : Double.parseDouble(value);
27 Throw.when(factor < 0.0, RuntimeException.class, "Factor %d is not positive.", factor);
28 return new DoubleType(factor);
29 }
30
31 }