View Javadoc
1   package org.opentrafficsim.road.gtu.lane.perception;
2   
3   import java.io.Serializable;
4   import java.util.HashMap;
5   import java.util.Map;
6   
7   import org.djunits.value.vdouble.scalar.Speed;
8   import org.djutils.exceptions.Throw;
9   import org.opentrafficsim.base.parameters.Parameters;
10  import org.opentrafficsim.core.gtu.GTUType;
11  import org.opentrafficsim.road.gtu.lane.tactical.following.CarFollowingModel;
12  import org.opentrafficsim.road.network.lane.LaneType;
13  
14  /**
15   * <p>
16   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
17   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
18   * </p>
19   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
20   * initial version May 27, 2016 <br>
21   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
22   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
23   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
24   */
25  public class GTUTypeAssumptions implements Serializable
26  {
27      /** */
28      private static final long serialVersionUID = 20160527L;
29  
30      /** stored car following model of the observed GTU. */
31      private final Map<GTUType, CarFollowingModel> carFollowingModelMap = new HashMap<>();
32  
33      /** stored parameters of the observed GTU. */
34      private final Map<GTUType, Parameters> parametersMap = new HashMap<>();
35  
36      /** stored speed limit info of the observed GTU. */
37      private final Map<GTUType, Map<LaneType, Speed>> laneTypeSpeedMap = new HashMap<>();
38  
39      /**
40       * Set the car following model for a certain GTUType as an assumption for that GTUType.
41       * @param gtuType GTUType; the GTUType to set the model for
42       * @param carFollowingModel CarFollowingModel; the model to set for the GTUType
43       */
44      public final void setCarFollowingModel(final GTUType gtuType, final CarFollowingModel carFollowingModel)
45      {
46          Throw.whenNull(gtuType, "gtuType cannot be null");
47          Throw.whenNull(carFollowingModel, "carFollowingModel cannot be null");
48          this.carFollowingModelMap.put(gtuType, carFollowingModel);
49      }
50  
51      /**
52       * Set the parameters for a certain GTUType as an assumption for that GTUType.
53       * @param gtuType GTUType; the GTUType to set the model for
54       * @param parameters Parameters; the model to set for the GTUType
55       */
56      public final void setParameters(final GTUType gtuType, final Parameters parameters)
57      {
58          Throw.whenNull(gtuType, "gtuType cannot be null");
59          Throw.whenNull(parameters, "parameters cannot be null");
60          this.parametersMap.put(gtuType, parameters);
61      }
62  
63      /**
64       * Set the maximum speed for a certain GTUType on a certain LaneType as an assumption for that GTUType.
65       * @param gtuType GTUType; the GTUType to set the model for
66       * @param laneType LaneType; the laneType to set the speed for
67       * @param maxSpeed Speed; the maximum speed on the laneType for the given GTUType
68       */
69      public final void setLaneTypeMaxSpeed(final GTUType gtuType, final LaneType laneType, final Speed maxSpeed)
70      {
71          Throw.whenNull(gtuType, "gtuType cannot be null");
72          Throw.whenNull(laneType, "laneType cannot be null");
73          Throw.whenNull(maxSpeed, "maxSpeed cannot be null");
74          Map<LaneType, Speed> maxLaneTypeSpeed = this.laneTypeSpeedMap.get(gtuType);
75          if (maxLaneTypeSpeed == null)
76          {
77              maxLaneTypeSpeed = new HashMap<>();
78              this.laneTypeSpeedMap.put(gtuType, maxLaneTypeSpeed);
79          }
80          maxLaneTypeSpeed.put(laneType, maxSpeed);
81      }
82  
83      /**
84       * Return the car following model for a certain GTUType as an assumption for that GTUType.
85       * @param gtuType GTUType; the GTUType to get the model for
86       * @return the car following model for the GTUType, or <b>null</b> when there is no information for the gtuType
87       */
88      public final CarFollowingModel getCarFollowingModel(final GTUType gtuType)
89      {
90          return this.carFollowingModelMap.get(gtuType);
91      }
92  
93      /**
94       * Return the parameters model for a certain GTUType as an assumption for that GTUType.
95       * @param gtuType GTUType; the GTUType to get the model for
96       * @return the parameters for the GTUType, or <b>null</b> when there is no information for the gtuType
97       */
98      public final Parameters getParameters(final GTUType gtuType)
99      {
100         return this.parametersMap.get(gtuType);
101     }
102 
103     /**
104      * Return the maximum speed on a LaneType for a certain GTUType as an assumption for that GTUType.
105      * @param gtuType GTUType; the GTUType to get the maximum speed for
106      * @param laneType LaneType; the LaneType to get the maximum speed for
107      * @return the maximum speed for the GTUType on the LaneType, or <b>null</b> when there is no information for the
108      *         combination of gtuType and laneType
109      */
110     public final Speed getLaneTypeMaxSpeed(final GTUType gtuType, final LaneType laneType)
111     {
112         if (!this.laneTypeSpeedMap.containsKey(gtuType))
113         {
114             return null;
115         }
116         return this.laneTypeSpeedMap.get(gtuType).get(laneType);
117     }
118 
119     /**
120      * Return a safe copy of the maximum speed for all LaneTypes for a certain GTUType as an assumption for that GTUType.
121      * @param gtuType GTUType; the GTUType to get the maximum speed for
122      * @return a map with a safe copy of the maximum speed for the GTUType on all LaneTypes, or <b>null</b> when there is no
123      *         information for the gtuType
124      */
125     public final Map<LaneType, Speed> getMaxSpeeds(final GTUType gtuType)
126     {
127         if (!this.laneTypeSpeedMap.containsKey(gtuType))
128         {
129             return null;
130         }
131         Map<LaneType, Speed> maxSpeeds = new HashMap<>();
132         maxSpeeds.putAll(this.laneTypeSpeedMap.get(gtuType));
133         return maxSpeeds;
134     }
135 
136     /** {@inheritDoc} */
137     @Override
138     public final int hashCode()
139     {
140         final int prime = 31;
141         int result = 1;
142         result = prime * result + ((this.parametersMap == null) ? 0 : this.parametersMap.hashCode());
143         result = prime * result + ((this.carFollowingModelMap == null) ? 0 : this.carFollowingModelMap.hashCode());
144         result = prime * result + ((this.laneTypeSpeedMap == null) ? 0 : this.laneTypeSpeedMap.hashCode());
145         return result;
146     }
147 
148     /** {@inheritDoc} */
149     @Override
150     @SuppressWarnings("checkstyle:needbraces")
151     public final boolean equals(final Object obj)
152     {
153         if (this == obj)
154             return true;
155         if (obj == null)
156             return false;
157         if (getClass() != obj.getClass())
158             return false;
159         GTUTypeAssumptions other = (GTUTypeAssumptions) obj;
160         if (this.parametersMap == null)
161         {
162             if (other.parametersMap != null)
163                 return false;
164         }
165         else if (!this.parametersMap.equals(other.parametersMap))
166             return false;
167         if (this.carFollowingModelMap == null)
168         {
169             if (other.carFollowingModelMap != null)
170                 return false;
171         }
172         else if (!this.carFollowingModelMap.equals(other.carFollowingModelMap))
173             return false;
174         if (this.laneTypeSpeedMap == null)
175         {
176             if (other.laneTypeSpeedMap != null)
177                 return false;
178         }
179         else if (!this.laneTypeSpeedMap.equals(other.laneTypeSpeedMap))
180             return false;
181         return true;
182     }
183 
184     /** {@inheritDoc} */
185     @Override
186     public final String toString()
187     {
188         return "GTUTypeAssumptions [carFollowingModelMap=" + this.carFollowingModelMap + ", parametersMap=" + this.parametersMap
189                 + ", laneTypeSpeedMap=" + this.laneTypeSpeedMap + "]";
190     }
191 
192 }