1 package org.opentrafficsim.road.network.speed;
2
3 import java.util.LinkedHashMap;
4 import java.util.Map;
5
6 import org.djutils.exceptions.Throw;
7
8
9
10
11
12
13
14
15
16
17
18 public class SpeedLimitInfo
19 {
20
21
22 private final Map<SpeedLimitType<?>, Object> speedInfoMap = new LinkedHashMap<>();
23
24
25
26
27 public SpeedLimitInfo()
28 {
29
30 }
31
32
33
34
35
36
37
38
39 public final <T> void addSpeedInfo(final SpeedLimitType<T> speedLimitType, final T speedInfo)
40 {
41 Throw.whenNull(speedLimitType, "Speed limit type may not be null.");
42 Throw.whenNull(speedInfo, "Speed info may not be null.");
43 this.speedInfoMap.put(speedLimitType, speedInfo);
44 }
45
46
47
48
49
50
51 public final void removeSpeedInfo(final SpeedLimitType<?> speedLimitType)
52 {
53 Throw.whenNull(speedLimitType, "Speed limit type may not be null.");
54 this.speedInfoMap.remove(speedLimitType);
55 }
56
57
58
59
60
61
62 public final boolean containsType(final SpeedLimitType<?> speedLimitType)
63 {
64 return this.speedInfoMap.containsKey(speedLimitType);
65 }
66
67
68
69
70
71
72
73
74
75 @SuppressWarnings("unchecked")
76 public final <T> T getSpeedInfo(final SpeedLimitType<T> speedLimitType)
77 {
78 Throw.whenNull(speedLimitType, "Speed limit type may not be null.");
79 Throw.when(!containsType(speedLimitType), IllegalStateException.class,
80 "The speed limit type '%s' "
81 + "is not present in the speed limit info. Use SpeedLimitInfo.containsType() to check.",
82 speedLimitType.getId());
83 return (T) this.speedInfoMap.get(speedLimitType);
84 }
85
86 @Override
87 public final int hashCode()
88 {
89 final int prime = 31;
90 int result = 1;
91 result = prime * result + this.speedInfoMap.hashCode();
92 return result;
93 }
94
95 @Override
96 public final boolean equals(final Object obj)
97 {
98 if (this == obj)
99 {
100 return true;
101 }
102 if (obj == null)
103 {
104 return false;
105 }
106 if (getClass() != obj.getClass())
107 {
108 return false;
109 }
110 SpeedLimitInfo other = (SpeedLimitInfo) obj;
111 if (!this.speedInfoMap.equals(other.speedInfoMap))
112 {
113 return false;
114 }
115 return true;
116 }
117
118 @Override
119 public final String toString()
120 {
121 StringBuilder stringBuilder = new StringBuilder("SpeedLimitInfo [");
122 String sep = "";
123 for (SpeedLimitType<?> slt : this.speedInfoMap.keySet())
124 {
125 stringBuilder.append(sep);
126 stringBuilder.append(slt.getId());
127 stringBuilder.append("=");
128 stringBuilder.append(this.speedInfoMap.get(slt));
129 sep = ", ";
130 }
131 stringBuilder.append("]");
132 return stringBuilder.toString();
133 }
134
135 }