View Javadoc
1   package org.opentrafficsim.editor.extensions;
2   
3   import java.util.LinkedHashMap;
4   
5   import org.djunits.value.vdouble.scalar.Angle;
6   import org.djunits.value.vdouble.scalar.Direction;
7   import org.djunits.value.vdouble.scalar.Length;
8   import org.djunits.value.vdouble.scalar.LinearDensity;
9   import org.djutils.draw.point.Point2d;
10  import org.djutils.exceptions.Throw;
11  import org.opentrafficsim.road.network.lane.Stripe;
12  import org.opentrafficsim.xml.bindings.AngleAdapter;
13  import org.opentrafficsim.xml.bindings.ArcDirectionAdapter;
14  import org.opentrafficsim.xml.bindings.BooleanAdapter;
15  import org.opentrafficsim.xml.bindings.DirectionAdapter;
16  import org.opentrafficsim.xml.bindings.DoubleAdapter;
17  import org.opentrafficsim.xml.bindings.ExpressionAdapter;
18  import org.opentrafficsim.xml.bindings.IntegerAdapter;
19  import org.opentrafficsim.xml.bindings.LengthAdapter;
20  import org.opentrafficsim.xml.bindings.LengthBeginEndAdapter;
21  import org.opentrafficsim.xml.bindings.LinearDensityAdapter;
22  import org.opentrafficsim.xml.bindings.Point2dAdapter;
23  import org.opentrafficsim.xml.bindings.StringAdapter;
24  import org.opentrafficsim.xml.bindings.StripeTypeAdapter;
25  import org.opentrafficsim.xml.bindings.types.ArcDirectionType.ArcDirection;
26  import org.opentrafficsim.xml.bindings.types.ExpressionType;
27  import org.opentrafficsim.xml.bindings.types.LengthBeginEndType.LengthBeginEnd;
28  
29  /**
30   * Class that houses static instances of adapters, for common usage within the editor.
31   * <p>
32   * Copyright (c) 2023-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
33   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
34   * </p>
35   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
36   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
37   */
38  public final class Adapters
39  {
40  
41      /** Map of adapters per output type. */
42      private final static java.util.Map<Class<?>, ExpressionAdapter<?, ?>> ADAPTERS = new LinkedHashMap<>();
43  
44      static
45      {
46          ADAPTERS.put(Angle.class, new AngleAdapter());
47          ADAPTERS.put(ArcDirection.class, new ArcDirectionAdapter());
48          ADAPTERS.put(Boolean.class, new BooleanAdapter());
49          ADAPTERS.put(Direction.class, new DirectionAdapter());
50          ADAPTERS.put(Double.class, new DoubleAdapter());
51          ADAPTERS.put(Integer.class, new IntegerAdapter());
52          ADAPTERS.put(Length.class, new LengthAdapter());
53          ADAPTERS.put(LengthBeginEnd.class, new LengthBeginEndAdapter());
54          ADAPTERS.put(LinearDensity.class, new LinearDensityAdapter());
55          ADAPTERS.put(Point2d.class, new Point2dAdapter());
56          ADAPTERS.put(String.class, new StringAdapter());
57          ADAPTERS.put(Stripe.Type.class, new StripeTypeAdapter());
58      }
59  
60      /**
61       * Returns an adapter for the given class. Adapters are only provided for known classes. This is to limit the number of
62       * adapters in memory, as these are stateless. Adapters are available for: Angle, ArcDirection, Boolean, Direction, Double,
63       * Integer, Length, LinearDensity, Point2d, String, and Stripe.Type.
64       * @param <T> output type of the adapter.
65       * @param <E> expression type of the adapter.
66       * @param clazz Class&lt;T&gt;; class of the output type of the adapter.
67       * @return ExpressionAdapter&lt;T, ?&gt;; adapter.
68       */
69      @SuppressWarnings("unchecked")
70      public static <T, E extends ExpressionType<T>> ExpressionAdapter<T, E> get(final Class<T> clazz)
71      {
72          Throw.when(!ADAPTERS.containsKey(clazz), RuntimeException.class,
73                  "No adapter for class %s available. Add it in the static code block of MapData or create one directly.");
74          return (ExpressionAdapter<T, E>) ADAPTERS.get(clazz);
75      }
76  
77      /**
78       * Add an adapter for the given class.
79       * @param <T> output type of the adapter.
80       * @param <E> expression type of the adapter.
81       * @param clazz Class&lt;T&gt;; class of the output type of the adapter.
82       * @param adapter ExpressionAdapter&lt;T, ?&gt;; adapter.
83       */
84      public static <T, E extends ExpressionType<T>> void set(final Class<T> clazz, final ExpressionAdapter<T, E> adapter)
85      {
86          Throw.whenNull(clazz, "Class may not be null.");
87          Throw.whenNull(adapter, "Adapter may not be null.");
88          ADAPTERS.put(clazz, adapter);
89      }
90  
91  }