1 package org.opentrafficsim.xml.bindings;
2
3 import org.djunits.value.vdouble.scalar.Duration;
4 import org.djutils.exceptions.Throw;
5 import org.djutils.logger.CategoryLogger;
6 import org.opentrafficsim.xml.bindings.types.DurationType;
7
8
9
10
11
12
13
14
15
16
17 public class PositiveDurationAdapter extends ScalarAdapter<Duration, DurationType>
18 {
19
20
21 @Override
22 public DurationType unmarshal(final String field)
23 {
24 if (isExpression(field))
25 {
26 return new DurationType(trimBrackets(field));
27 }
28 try
29 {
30 Duration value = Duration.valueOf(field);
31 Throw.when(value.lt0(), IllegalArgumentException.class, "PositiveDuration value %s is not a positive value.", value);
32 return new DurationType(value);
33 }
34 catch (Exception exception)
35 {
36 CategoryLogger.always().error(exception, "Problem parsing Duration '" + field + "'");
37 throw exception;
38 }
39 }
40
41
42 @Override
43 public String marshal(final DurationType value)
44 {
45 Throw.when(!value.isExpression() && value.getValue().lt0(), IllegalArgumentException.class,
46 "PositiveDuration value %s is not a positive value.", value.getValue());
47 return super.marshal(value);
48 }
49
50 }