1 package org.opentrafficsim.xml.bindings;
2
3 import javax.xml.bind.annotation.adapters.XmlAdapter;
4
5 import org.djunits.value.Scalar;
6 import org.djunits.value.vdouble.scalar.Length;
7
8
9
10
11
12
13
14
15
16 public class LengthAdapter extends XmlAdapter<String, Length>
17 {
18
19 @Override
20 public Length unmarshal(final String field) throws IllegalArgumentException
21 {
22 if (field.trim().startsWith("-"))
23 {
24 throw new IllegalArgumentException("Length cannot be negative: " + field);
25 }
26 return Length.valueOf(field);
27 }
28
29
30 @Override
31 public String marshal(final Length length) throws IllegalArgumentException
32 {
33 if (length.lt(Length.ZERO))
34 {
35 throw new IllegalArgumentException("Length cannot be negative: " + length);
36 }
37 return Scalar.textualStringOfDefaultLocale(length);
38 }
39
40 }