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 * LengthAdapter converts between the XML String for a Length and the DJUnits Length. The length should be positive. <br>
10 * <br>
11 * Copyright (c) 2003-2018 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
12 * for project information <a href="https://www.simulation.tudelft.nl/" target="_blank">www.simulation.tudelft.nl</a>. The
13 * source code and binary code of this software is proprietary information of Delft University of Technology.
14 * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank">Alexander Verbraeck</a>
15 */
16 public class LengthAdapter extends XmlAdapter<String, Length>
17 {
18 /** {@inheritDoc} */
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 /** {@inheritDoc} */
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 }