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 public class SpeedLimitInfo implements Serializable
21 {
22
23
24 private static final long serialVersionUID = 20160501L;
25
26
27 private final Map<SpeedLimitType<?>, Object> speedInfoMap = new LinkedHashMap<>();
28
29
30
31
32
33
34
35
36 public final <T> void addSpeedInfo(final SpeedLimitType<T> speedLimitType, final T speedInfo)
37 {
38 Throw.whenNull(speedLimitType, "Speed limit type may not be null.");
39 Throw.whenNull(speedInfo, "Speed info may not be null.");
40 this.speedInfoMap.put(speedLimitType, speedInfo);
41 }
42
43
44
45
46
47
48 public final void removeSpeedInfo(final SpeedLimitType<?> speedLimitType)
49 {
50 Throw.whenNull(speedLimitType, "Speed limit type may not be null.");
51 this.speedInfoMap.remove(speedLimitType);
52 }
53
54
55
56
57
58
59 public final boolean containsType(final SpeedLimitType<?> speedLimitType)
60 {
61 return this.speedInfoMap.containsKey(speedLimitType);
62 }
63
64
65
66
67
68
69
70
71
72 @SuppressWarnings("unchecked")
73 public final <T> T getSpeedInfo(final SpeedLimitType<T> speedLimitType)
74 {
75 Throw.whenNull(speedLimitType, "Speed limit type may not be null.");
76 Throw.when(!containsType(speedLimitType), IllegalStateException.class,
77 "The speed limit type '%s' "
78 + "is not present in the speed limit info. Use SpeedLimitInfo.containsType() to check.",
79 speedLimitType.getId());
80 return (T) this.speedInfoMap.get(speedLimitType);
81 }
82
83
84 @Override
85 public final int hashCode()
86 {
87 final int prime = 31;
88 int result = 1;
89 result = prime * result + this.speedInfoMap.hashCode();
90 return result;
91 }
92
93
94 @Override
95 public final boolean equals(final Object obj)
96 {
97 if (this == obj)
98 {
99 return true;
100 }
101 if (obj == null)
102 {
103 return false;
104 }
105 if (getClass() != obj.getClass())
106 {
107 return false;
108 }
109 SpeedLimitInfo other = (SpeedLimitInfo) obj;
110 if (!this.speedInfoMap.equals(other.speedInfoMap))
111 {
112 return false;
113 }
114 return true;
115 }
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 }