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 @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 @Override
93 public final boolean equals(final Object obj)
94 {
95 if (this == obj)
96 {
97 return true;
98 }
99 if (obj == null)
100 {
101 return false;
102 }
103 if (getClass() != obj.getClass())
104 {
105 return false;
106 }
107 SpeedLimitInfo other = (SpeedLimitInfo) obj;
108 if (!this.speedInfoMap.equals(other.speedInfoMap))
109 {
110 return false;
111 }
112 return true;
113 }
114
115 @Override
116 public final String toString()
117 {
118 StringBuilder stringBuilder = new StringBuilder("SpeedLimitInfo [");
119 String sep = "";
120 for (SpeedLimitType<?> slt : this.speedInfoMap.keySet())
121 {
122 stringBuilder.append(sep);
123 stringBuilder.append(slt.getId());
124 stringBuilder.append("=");
125 stringBuilder.append(this.speedInfoMap.get(slt));
126 sep = ", ";
127 }
128 stringBuilder.append("]");
129 return stringBuilder.toString();
130 }
131
132 }