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 * Class to contain speed info related to various speed limit types. Instances can reflect the current speed limit situation,
11 * some situation ahead, or some situation in the past.
12 * <p>
13 * Copyright (c) 2013-2022 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
14 * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
15 * <p>
16 * @version $Revision$, $LastChangedDate$, by $Author$, initial version Apr 21, 2016 <br>
17 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
18 * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
19 */
20
21 public class SpeedLimitInfo implements Serializable
22 {
23
24 /** */
25 private static final long serialVersionUID = 20160501L;
26
27 /** Set of current speed info's mapped to speed limit types. */
28 private final Map<SpeedLimitType<?>, Object> speedInfoMap = new LinkedHashMap<>();
29
30 /**
31 * Adds or overwrites the speed info of the given speed limit type.
32 * @param speedLimitType SpeedLimitType<T>; speed limit type to add info for
33 * @param speedInfo T; info regarding the speed limit type
34 * @param <T> class of speed info
35 * @throws NullPointerException if the speed limit type or speed info is null
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 * Removes the speed info of given speed limit type.
46 * @param speedLimitType SpeedLimitType<?>; speed limit type of speed info to remove
47 * @throws NullPointerException if the speed limit type is null
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 * Whether speed info is present for the given speed limit type.
57 * @param speedLimitType SpeedLimitType<?>; speed limit type
58 * @return whether speed info is present for the given speed limit type
59 */
60 public final boolean containsType(final SpeedLimitType<?> speedLimitType)
61 {
62 return this.speedInfoMap.containsKey(speedLimitType);
63 }
64
65 /**
66 * Returns the info regarding a specific speed limit type.
67 * @param speedLimitType SpeedLimitType<T>; speed limit type to return info for
68 * @param <T> class of speed limit type info
69 * @return the speed limit type info
70 * @throws NullPointerException if the speed limit type is null
71 * @throws IllegalStateException if the speed limit type is not present
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 /** {@inheritDoc} */
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 /** {@inheritDoc} */
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 /** {@inheritDoc} */
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 }