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