View Javadoc
1   package org.opentrafficsim.road.network.speed;
2   
3   import java.util.LinkedHashMap;
4   import java.util.Map;
5   
6   import org.djutils.exceptions.Throw;
7   
8   /**
9    * Class to contain speed info related to various speed limit types. Instances can reflect the current speed limit situation,
10   * some situation ahead, or some situation in the past.
11   * <p>
12   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
13   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
14   * </p>
15   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
16   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
17   */
18  public class SpeedLimitInfo
19  {
20  
21      /** Set of current speed info's mapped to speed limit types. */
22      private final Map<SpeedLimitType<?>, Object> speedInfoMap = new LinkedHashMap<>();
23  
24      /**
25       * Constructor.
26       */
27      public SpeedLimitInfo()
28      {
29          //
30      }
31  
32      /**
33       * Adds or overwrites the speed info of the given speed limit type.
34       * @param speedLimitType speed limit type to add info for
35       * @param speedInfo info regarding the speed limit type
36       * @param <T> class of speed info
37       * @throws NullPointerException if the speed limit type or speed info is null
38       */
39      public final <T> void addSpeedInfo(final SpeedLimitType<T> speedLimitType, final T speedInfo)
40      {
41          Throw.whenNull(speedLimitType, "Speed limit type may not be null.");
42          Throw.whenNull(speedInfo, "Speed info may not be null.");
43          this.speedInfoMap.put(speedLimitType, speedInfo);
44      }
45  
46      /**
47       * Removes the speed info of given speed limit type.
48       * @param speedLimitType speed limit type of speed info to remove
49       * @throws NullPointerException if the speed limit type is null
50       */
51      public final void removeSpeedInfo(final SpeedLimitType<?> speedLimitType)
52      {
53          Throw.whenNull(speedLimitType, "Speed limit type may not be null.");
54          this.speedInfoMap.remove(speedLimitType);
55      }
56  
57      /**
58       * Whether speed info is present for the given speed limit type.
59       * @param speedLimitType speed limit type
60       * @return whether speed info is present for the given speed limit type
61       */
62      public final boolean containsType(final SpeedLimitType<?> speedLimitType)
63      {
64          return this.speedInfoMap.containsKey(speedLimitType);
65      }
66  
67      /**
68       * Returns the info regarding a specific speed limit type.
69       * @param speedLimitType speed limit type to return info for
70       * @param <T> class of speed limit type info
71       * @return the speed limit type info
72       * @throws NullPointerException if the speed limit type is null
73       * @throws IllegalStateException if the speed limit type is not present
74       */
75      @SuppressWarnings("unchecked")
76      public final <T> T getSpeedInfo(final SpeedLimitType<T> speedLimitType)
77      {
78          Throw.whenNull(speedLimitType, "Speed limit type may not be null.");
79          Throw.when(!containsType(speedLimitType), IllegalStateException.class,
80                  "The speed limit type '%s' "
81                          + "is not present in the speed limit info. Use SpeedLimitInfo.containsType() to check.",
82                  speedLimitType.getId());
83          return (T) this.speedInfoMap.get(speedLimitType);
84      }
85  
86      @Override
87      public final int hashCode()
88      {
89          final int prime = 31;
90          int result = 1;
91          result = prime * result + this.speedInfoMap.hashCode();
92          return result;
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 other = (SpeedLimitInfo) obj;
111         if (!this.speedInfoMap.equals(other.speedInfoMap))
112         {
113             return false;
114         }
115         return true;
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 }