View Javadoc
1   package org.opentrafficsim.road.network.speed;
2   
3   import java.io.Serializable;
4   import java.util.HashMap;
5   import java.util.Map;
6   
7   import nl.tudelft.simulation.language.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-2016 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 HashMap<>();
29  
30      /**
31       * Adds or overwrites the speed info of the given speed limit type.
32       * @param speedLimitType speed limit type to add info for
33       * @param speedInfo 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 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 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 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, "The speed limit type '%s' "
78              + "is not present in the speed limit info. Use SpeedLimitInfo.containsType() to check.", speedLimitType.getId());
79          return (T) this.speedInfoMap.get(speedLimitType);
80      }
81  
82      /** {@inheritDoc} */
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      /** {@inheritDoc} */
93      @Override
94      public final boolean equals(final Object obj)
95      {
96          if (this == obj)
97          {
98              return true;
99          }
100         if (obj == null)
101         {
102             return false;
103         }
104         if (getClass() != obj.getClass())
105         {
106             return false;
107         }
108         SpeedLimitInfo other = (SpeedLimitInfo) obj;
109         if (!this.speedInfoMap.equals(other.speedInfoMap))
110         {
111             return false;
112         }
113         return true;
114     }
115 
116     /** {@inheritDoc} */
117     @Override
118     public final String toString()
119     {
120         StringBuilder stringBuilder = new StringBuilder("SpeedLimitInfo [");
121         String sep = "";
122         for (SpeedLimitType<?> slt : this.speedInfoMap.keySet())
123         {
124             stringBuilder.append(sep);
125             stringBuilder.append(slt.getId());
126             stringBuilder.append("=");
127             stringBuilder.append(this.speedInfoMap.get(slt));
128             sep = ", ";
129         }
130         stringBuilder.append("]");
131         return stringBuilder.toString();
132     }
133 
134 }