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
31
32
33
34
35
36
37
38 public final class Adapters
39 {
40
41
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
62
63
64
65
66
67
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
79
80
81
82
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 }