View Javadoc
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-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
14   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
15   * </p>
16   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
17   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
18   */
19  
20  public class SpeedLimitInfo implements Serializable
21  {
22  
23      /** */
24      private static final long serialVersionUID = 20160501L;
25  
26      /** Set of current speed info's mapped to speed limit types. */
27      private final Map<SpeedLimitType<?>, Object> speedInfoMap = new LinkedHashMap<>();
28  
29      /**
30       * Adds or overwrites the speed info of the given speed limit type.
31       * @param speedLimitType SpeedLimitType&lt;T&gt;; speed limit type to add info for
32       * @param speedInfo T; info regarding the speed limit type
33       * @param <T> class of speed info
34       * @throws NullPointerException if the speed limit type or speed info is null
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       * Removes the speed info of given speed limit type.
45       * @param speedLimitType SpeedLimitType&lt;?&gt;; speed limit type of speed info to remove
46       * @throws NullPointerException if the speed limit type is null
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       * Whether speed info is present for the given speed limit type.
56       * @param speedLimitType SpeedLimitType&lt;?&gt;; speed limit type
57       * @return whether speed info is present for the given speed limit type
58       */
59      public final boolean containsType(final SpeedLimitType<?> speedLimitType)
60      {
61          return this.speedInfoMap.containsKey(speedLimitType);
62      }
63  
64      /**
65       * Returns the info regarding a specific speed limit type.
66       * @param speedLimitType SpeedLimitType&lt;T&gt;; speed limit type to return info for
67       * @param <T> class of speed limit type info
68       * @return the speed limit type info
69       * @throws NullPointerException if the speed limit type is null
70       * @throws IllegalStateException if the speed limit type is not present
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      /** {@inheritDoc} */
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      /** {@inheritDoc} */
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     /** {@inheritDoc} */
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 }