View Javadoc
1   package org.opentrafficsim.xml.bindings;
2   
3   import javax.xml.bind.annotation.adapters.XmlAdapter;
4   
5   import org.opentrafficsim.xml.bindings.types.ArcDirection;
6   
7   /**
8    * LeftRightAdapter to convert between XML representations of an arc direction, coded as L | LEFT | R | RIGHT | CLOCKWISE |
9    * COUNTERCLOCKWISE, and an enum type. <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 LeftRightAdapter extends XmlAdapter<String, ArcDirection>
17  {
18      /** {@inheritDoc} */
19      @Override
20      public ArcDirection unmarshal(final String field) throws IllegalArgumentException
21      {
22          try
23          {
24              String clean = field.replaceAll("\\s", "");
25              if (clean.equals("L") || clean.equals("LEFT") || clean.equals("COUNTERCLOCKWISE"))
26              {
27                  return ArcDirection.LEFT;
28              }
29              if (clean.equals("R") || clean.equals("RIGHT") || clean.equals("CLOCKWISE"))
30              {
31                  return ArcDirection.RIGHT;
32              }
33          }
34          catch (Exception exception)
35          {
36              throw new IllegalArgumentException("Error parsing ArcDirection " + field, exception);
37          }
38          throw new IllegalArgumentException("Error parsing ArcDirection " + field);
39      }
40  
41      /** {@inheritDoc} */
42      @Override
43      public String marshal(final ArcDirection arcDirection) throws IllegalArgumentException
44      {
45          if (arcDirection.equals(ArcDirection.LEFT))
46              return "LEFT";
47          return "RIGHT";
48      }
49  
50  }