1 package org.opentrafficsim.road.network.speed;
2
3 import java.io.Serializable;
4 import java.util.LinkedHashMap;
5 import java.util.Map;
6
7 import org.djutils.exceptions.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 LinkedHashMap<>();
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,
78 "The speed limit type '%s' "
79 + "is not present in the speed limit info. Use SpeedLimitInfo.containsType() to check.",
80 speedLimitType.getId());
81 return (T) this.speedInfoMap.get(speedLimitType);
82 }
83
84
85 @Override
86 public final int hashCode()
87 {
88 final int prime = 31;
89 int result = 1;
90 result = prime * result + this.speedInfoMap.hashCode();
91 return result;
92 }
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./../../org/opentrafficsim/road/network/speed/SpeedLimitInfo.html#SpeedLimitInfo">SpeedLimitInfo other = (SpeedLimitInfo) obj;
111 if (!this.speedInfoMap.equals(other.speedInfoMap))
112 {
113 return false;
114 }
115 return true;
116 }
117
118
119 @Override
120 public final String toString()
121 {
122 StringBuilder stringBuilder = new StringBuilder("SpeedLimitInfo [");
123 String sep = "";
124 for (SpeedLimitType<?> slt : this.speedInfoMap.keySet())
125 {
126 stringBuilder.append(sep);
127 stringBuilder.append(slt.getId());
128 stringBuilder.append("=");
129 stringBuilder.append(this.speedInfoMap.get(slt));
130 sep = ", ";
131 }
132 stringBuilder.append("]");
133 return stringBuilder.toString();
134 }
135
136 }