GTUTypeAssumptions.java

  1. package org.opentrafficsim.road.gtu.lane.perception;

  2. import java.io.Serializable;
  3. import java.util.LinkedHashMap;
  4. import java.util.Map;

  5. import org.djunits.value.vdouble.scalar.Speed;
  6. import org.djutils.exceptions.Throw;
  7. import org.opentrafficsim.base.parameters.Parameters;
  8. import org.opentrafficsim.core.gtu.GTUType;
  9. import org.opentrafficsim.road.gtu.lane.tactical.following.CarFollowingModel;
  10. import org.opentrafficsim.road.network.lane.LaneType;

  11. /**
  12.  * <p>
  13.  * Copyright (c) 2013-2020 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/license.html">OpenTrafficSim License</a>.
  15.  * </p>
  16.  * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
  17.  * initial version May 27, 2016 <br>
  18.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  19.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  20.  * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  21.  */
  22. public class GTUTypeAssumptions implements Serializable
  23. {
  24.     /** */
  25.     private static final long serialVersionUID = 20160527L;

  26.     /** stored car following model of the observed GTU. */
  27.     private final Map<GTUType, CarFollowingModel> carFollowingModelMap = new LinkedHashMap<>();

  28.     /** stored parameters of the observed GTU. */
  29.     private final Map<GTUType, Parameters> parametersMap = new LinkedHashMap<>();

  30.     /** stored speed limit info of the observed GTU. */
  31.     private final Map<GTUType, Map<LaneType, Speed>> laneTypeSpeedMap = new LinkedHashMap<>();

  32.     /**
  33.      * Set the car following model for a certain GTUType as an assumption for that GTUType.
  34.      * @param gtuType GTUType; the GTUType to set the model for
  35.      * @param carFollowingModel CarFollowingModel; the model to set for the GTUType
  36.      */
  37.     public final void setCarFollowingModel(final GTUType gtuType, final CarFollowingModel carFollowingModel)
  38.     {
  39.         Throw.whenNull(gtuType, "gtuType cannot be null");
  40.         Throw.whenNull(carFollowingModel, "carFollowingModel cannot be null");
  41.         this.carFollowingModelMap.put(gtuType, carFollowingModel);
  42.     }

  43.     /**
  44.      * Set the parameters for a certain GTUType as an assumption for that GTUType.
  45.      * @param gtuType GTUType; the GTUType to set the model for
  46.      * @param parameters Parameters; the model to set for the GTUType
  47.      */
  48.     public final void setParameters(final GTUType gtuType, final Parameters parameters)
  49.     {
  50.         Throw.whenNull(gtuType, "gtuType cannot be null");
  51.         Throw.whenNull(parameters, "parameters cannot be null");
  52.         this.parametersMap.put(gtuType, parameters);
  53.     }

  54.     /**
  55.      * Set the maximum speed for a certain GTUType on a certain LaneType as an assumption for that GTUType.
  56.      * @param gtuType GTUType; the GTUType to set the model for
  57.      * @param laneType LaneType; the laneType to set the speed for
  58.      * @param maxSpeed Speed; the maximum speed on the laneType for the given GTUType
  59.      */
  60.     public final void setLaneTypeMaxSpeed(final GTUType gtuType, final LaneType laneType, final Speed maxSpeed)
  61.     {
  62.         Throw.whenNull(gtuType, "gtuType cannot be null");
  63.         Throw.whenNull(laneType, "laneType cannot be null");
  64.         Throw.whenNull(maxSpeed, "maxSpeed cannot be null");
  65.         Map<LaneType, Speed> maxLaneTypeSpeed = this.laneTypeSpeedMap.get(gtuType);
  66.         if (maxLaneTypeSpeed == null)
  67.         {
  68.             maxLaneTypeSpeed = new LinkedHashMap<>();
  69.             this.laneTypeSpeedMap.put(gtuType, maxLaneTypeSpeed);
  70.         }
  71.         maxLaneTypeSpeed.put(laneType, maxSpeed);
  72.     }

  73.     /**
  74.      * Return the car following model for a certain GTUType as an assumption for that GTUType.
  75.      * @param gtuType GTUType; the GTUType to get the model for
  76.      * @return the car following model for the GTUType, or <b>null</b> when there is no information for the gtuType
  77.      */
  78.     public final CarFollowingModel getCarFollowingModel(final GTUType gtuType)
  79.     {
  80.         return this.carFollowingModelMap.get(gtuType);
  81.     }

  82.     /**
  83.      * Return the parameters model for a certain GTUType as an assumption for that GTUType.
  84.      * @param gtuType GTUType; the GTUType to get the model for
  85.      * @return the parameters for the GTUType, or <b>null</b> when there is no information for the gtuType
  86.      */
  87.     public final Parameters getParameters(final GTUType gtuType)
  88.     {
  89.         return this.parametersMap.get(gtuType);
  90.     }

  91.     /**
  92.      * Return the maximum speed on a LaneType for a certain GTUType as an assumption for that GTUType.
  93.      * @param gtuType GTUType; the GTUType to get the maximum speed for
  94.      * @param laneType LaneType; the LaneType to get the maximum speed for
  95.      * @return the maximum speed for the GTUType on the LaneType, or <b>null</b> when there is no information for the
  96.      *         combination of gtuType and laneType
  97.      */
  98.     public final Speed getLaneTypeMaxSpeed(final GTUType gtuType, final LaneType laneType)
  99.     {
  100.         if (!this.laneTypeSpeedMap.containsKey(gtuType))
  101.         {
  102.             return null;
  103.         }
  104.         return this.laneTypeSpeedMap.get(gtuType).get(laneType);
  105.     }

  106.     /**
  107.      * Return a safe copy of the maximum speed for all LaneTypes for a certain GTUType as an assumption for that GTUType.
  108.      * @param gtuType GTUType; the GTUType to get the maximum speed for
  109.      * @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
  110.      *         information for the gtuType
  111.      */
  112.     public final Map<LaneType, Speed> getMaxSpeeds(final GTUType gtuType)
  113.     {
  114.         if (!this.laneTypeSpeedMap.containsKey(gtuType))
  115.         {
  116.             return null;
  117.         }
  118.         Map<LaneType, Speed> maxSpeeds = new LinkedHashMap<>();
  119.         maxSpeeds.putAll(this.laneTypeSpeedMap.get(gtuType));
  120.         return maxSpeeds;
  121.     }

  122.     /** {@inheritDoc} */
  123.     @Override
  124.     public final int hashCode()
  125.     {
  126.         final int prime = 31;
  127.         int result = 1;
  128.         result = prime * result + ((this.parametersMap == null) ? 0 : this.parametersMap.hashCode());
  129.         result = prime * result + ((this.carFollowingModelMap == null) ? 0 : this.carFollowingModelMap.hashCode());
  130.         result = prime * result + ((this.laneTypeSpeedMap == null) ? 0 : this.laneTypeSpeedMap.hashCode());
  131.         return result;
  132.     }

  133.     /** {@inheritDoc} */
  134.     @Override
  135.     @SuppressWarnings("checkstyle:needbraces")
  136.     public final boolean equals(final Object obj)
  137.     {
  138.         if (this == obj)
  139.             return true;
  140.         if (obj == null)
  141.             return false;
  142.         if (getClass() != obj.getClass())
  143.             return false;
  144.         GTUTypeAssumptions other = (GTUTypeAssumptions) obj;
  145.         if (this.parametersMap == null)
  146.         {
  147.             if (other.parametersMap != null)
  148.                 return false;
  149.         }
  150.         else if (!this.parametersMap.equals(other.parametersMap))
  151.             return false;
  152.         if (this.carFollowingModelMap == null)
  153.         {
  154.             if (other.carFollowingModelMap != null)
  155.                 return false;
  156.         }
  157.         else if (!this.carFollowingModelMap.equals(other.carFollowingModelMap))
  158.             return false;
  159.         if (this.laneTypeSpeedMap == null)
  160.         {
  161.             if (other.laneTypeSpeedMap != null)
  162.                 return false;
  163.         }
  164.         else if (!this.laneTypeSpeedMap.equals(other.laneTypeSpeedMap))
  165.             return false;
  166.         return true;
  167.     }

  168.     /** {@inheritDoc} */
  169.     @Override
  170.     public final String toString()
  171.     {
  172.         return "GTUTypeAssumptions [carFollowingModelMap=" + this.carFollowingModelMap + ", parametersMap=" + this.parametersMap
  173.                 + ", laneTypeSpeedMap=" + this.laneTypeSpeedMap + "]";
  174.     }

  175. }