LaneType.java

  1. package org.opentrafficsim.road.network.lane;

  2. import java.io.Serializable;

  3. import org.djutils.exceptions.Throw;
  4. import org.opentrafficsim.base.HierarchicalType;
  5. import org.opentrafficsim.core.compatibility.Compatibility;
  6. import org.opentrafficsim.core.compatibility.GTUCompatibility;
  7. import org.opentrafficsim.core.gtu.GTUDirectionality;
  8. import org.opentrafficsim.core.gtu.GTUType;
  9. import org.opentrafficsim.core.network.LongitudinalDirectionality;
  10. import org.opentrafficsim.road.network.RoadNetwork;

  11. /**
  12.  * Lane type to indicate compatibility with GTU types. The id of a LaneType should be unique. This is, however, not checked or
  13.  * enforced, as the LaneType is not a singleton as the result of the compatibilitySet. Different simulations running in the same
  14.  * GTU can have different compatibilitySets for LaneTypes with the same id. Therefore, uniqueness is not enforced.
  15.  * <p>
  16.  * Copyright (c) 2013-2020 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-08-30 00:16:51 +0200 (Sun, 30 Aug 2015) $, @version $Revision: 1329 $, by $Author: averbraeck $,
  20.  * initial version Aug 21, 2014 <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.citg.tudelft.nl">Guus Tamminga</a>
  24.  */
  25. public class LaneType extends HierarchicalType<LaneType> implements Serializable, Compatibility<GTUType, LaneType>
  26. {
  27.     /** */
  28.     private static final long serialVersionUID = 20140821L;

  29.     /** The compatibility of GTUs with this lane type. */
  30.     private final GTUCompatibility<LaneType> compatibility;

  31.     /** the network to which the LinkType belongs. */
  32.     private final RoadNetwork network;

  33.     /** Default types with their name. */
  34.     public enum DEFAULTS
  35.     {
  36.         /** The lane type used for lanes that are forbidden to all GTU types. */
  37.         NONE("NONE"),

  38.         /** Vehicular roads (Dutch: weg); allows all road vehicles and pedestrians. */
  39.         TWO_WAY_LANE("TWO_WAY_LANE"),

  40.         /** Vehicular lane that is two-way for PEDESTRIANS but only permitted in design direction for all other road users. */
  41.         ONE_WAY_LANE("ONE_WAY_LANE"),

  42.         /** Controlled access roads (Dutch: snelweg). */
  43.         FREEWAY("FREEWAY"),

  44.         /** High speed vehicular roads (Dutch: autoweg). */
  45.         HIGHWAY("HIGHWAY"),

  46.         /** Lane on rural vehicular roads (Dutch: weg buiten bebouwde kom). */
  47.         RURAL_ROAD_LANE("RURAL_ROAD_LANE"),

  48.         /** Lane on urban vehicular roads (Dutch: weg binnen bebouwde kom). */
  49.         URBAN_ROAD_LANE("URBAN_ROAD_LANE"),

  50.         /** Residential vehicular roads (Dutch: woonerf). */
  51.         RESIDENTIAL_ROAD_LANE("RESIDENTIAL_ROAD_LANE"),

  52.         /** Bidirectional bus lane (Dutch: busstrook). */
  53.         BUS_LANE("BUS_LANE"),

  54.         /** Bidirectional bicycle lane (Dutch: (brom)fietspad). */
  55.         MOPED_PATH("MOPED_PATH"),

  56.         /** Bicycle path (Dutch: fietspad). */
  57.         BICYCLE_PATH("BICYCLE_PATH"),

  58.         /** Bidirectional footpath (Dutch: voetpad). */
  59.         FOOTPATH("FOOTPATH");

  60.         /** The name. */
  61.         private final String id;

  62.         /**
  63.          * Construct the enum.
  64.          * @param id String; the id
  65.          */
  66.         DEFAULTS(final String id)
  67.         {
  68.             this.id = id;
  69.         }

  70.         /** @return the id */
  71.         public String getId()
  72.         {
  73.             return this.id;
  74.         }
  75.     }

  76.     /**
  77.      * Create a new Lane type with a compatibility set.
  78.      * @param id String; the id of the lane type.
  79.      * @param compatibility GTUCompatibility&lt;LaneType&gt;; the collection of compatible GTUTypes for this LaneType.
  80.      *            Compatibility is solely determined by a specific lane type, and independent of compatibility in super or sub
  81.      *            types.
  82.      * @param network RoadNetwork; The network to which the LaneType belongs
  83.      * @throws NullPointerException if either the id is null, or the compatibilitySet is null
  84.      */
  85.     public LaneType(final String id, final GTUCompatibility<LaneType> compatibility, final RoadNetwork network)
  86.             throws NullPointerException
  87.     {
  88.         super(id);
  89.         Throw.whenNull(compatibility, "compatibility collection cannot be null for LaneType with id = %s", id);
  90.         Throw.whenNull(network, "network cannot be null for LaneType with id = %s", id);
  91.         this.compatibility = new GTUCompatibility<>(compatibility);
  92.         this.network = network;
  93.         this.network.addLaneType(this);
  94.     }

  95.     /**
  96.      * Create a new Lane type with a compatibility set.
  97.      * @param id String; the id of the lane type.
  98.      * @param parent LaneType; parent type
  99.      * @param compatibility GTUCompatibility&lt;LaneType&gt;; the collection of compatible GTUTypes for this LaneType.
  100.      *            Compatibility is solely determined by a specific lane type, and independent of compatibility in super or sub
  101.      *            types.
  102.      * @param network RoadNetwork; The network to which the LaneType belongs
  103.      * @throws NullPointerException if either the id is null, or the compatibilitySet is null
  104.      */
  105.     public LaneType(final String id, final LaneType parent, final GTUCompatibility<LaneType> compatibility,
  106.             final RoadNetwork network) throws NullPointerException
  107.     {
  108.         super(id, parent);
  109.         Throw.whenNull(compatibility, "compatibility collection cannot be null for LaneType with id = %s", id);
  110.         Throw.whenNull(parent, "parent cannot be null for LaneType with id = %s", id);
  111.         this.compatibility = new GTUCompatibility<>(compatibility);
  112.         this.network = network;
  113.         this.network.addLaneType(this);
  114.     }

  115.     /**
  116.      * Private constructor for a LaneType.
  117.      * @param id String; id of the new LaneType
  118.      * @param inverted boolean; if true; the compatibility is longitudinally inverted
  119.      * @param network RoadNetwork; The network to which the LaneType belongs
  120.      */
  121.     private LaneType(final String id, final boolean inverted, final RoadNetwork network)
  122.     {
  123.         super(id);
  124.         this.compatibility = null;
  125.         this.network = network;
  126.         this.network.addLaneType(this);
  127.     }

  128.     /**
  129.      * Whether this, or any of the parent types, equals the given type.
  130.      * @param type DEFAULTS; type
  131.      * @return whether this, or any of the parent types, equals the given type
  132.      */
  133.     public boolean isOfType(final DEFAULTS type)
  134.     {
  135.         if (this.getId().equals(type.getId()))
  136.         {
  137.             return true;
  138.         }
  139.         if (getParent() != null)
  140.         {
  141.             return getParent().isOfType(type);
  142.         }
  143.         return false;
  144.     }

  145.     /**
  146.      * Construct a new Lane type based on another Lane type with longitudinally inverted compatibility.
  147.      * @return LaneType; the new lane type
  148.      */
  149.     public final LaneType inv()
  150.     {
  151.         return new LaneType(getId(), true, getNetwork());
  152.     }

  153.     /**
  154.      * @return the gtu compatibility for this LaneType
  155.      */
  156.     public GTUCompatibility<LaneType> getCompatibility()
  157.     {
  158.         return this.compatibility;
  159.     }

  160.     /**
  161.      * @return the network to which the LinkType belongs
  162.      */
  163.     public RoadNetwork getNetwork()
  164.     {
  165.         return this.network;
  166.     }

  167.     /**
  168.      * Compatibility is solely determined by a specific lane type, and independent of compatibility in super or sub types.
  169.      * @param gtuType GTUType; GTU type to look for compatibility.
  170.      * @param direction GTUDirectionality; the direction that the GTU is moving (with respect to the direction of the design
  171.      *            line of the Link)
  172.      * @return boolean; true if this LaneType permits GTU type in the given direction
  173.      */
  174.     @Override
  175.     public final Boolean isCompatible(final GTUType gtuType, final GTUDirectionality direction)
  176.     {
  177.         // OTS-338
  178.         // return this.compatibilitySet.contains(gtuType) || this.compatibilitySet.contains(GTUType.ALL);
  179.         return getDirectionality(gtuType).permits(direction);
  180.     }

  181.     /**
  182.      * Get the permitted driving directions for a given GTU type on this Lane.
  183.      * @param gtuType GTUType; the GTU type
  184.      * @return LongitudinalDirectionality; the permitted directions of the GTU type on this Lane
  185.      */
  186.     public final LongitudinalDirectionality getDirectionality(final GTUType gtuType)
  187.     {
  188.         LongitudinalDirectionality result = this.compatibility.getDirectionality(gtuType, true);
  189.         if (null == this.compatibility)
  190.         {
  191.             return result.invert();
  192.         }
  193.         return result;
  194.     }

  195.     /**
  196.      * Add GTU type to compatibility.
  197.      * @param gtuType GTUType; the GTU type to add
  198.      * @param direction LongitudinalDirectionality; permitted direction of movement
  199.      */
  200.     public final void addGtuCompatability(final GTUType gtuType, final LongitudinalDirectionality direction)
  201.     {
  202.         if (null == this.compatibility)
  203.         {
  204.             getParent().addGtuCompatability(gtuType, direction.invert());
  205.         }
  206.         else
  207.         {
  208.             this.compatibility.addAllowedGTUType(gtuType, direction);
  209.         }
  210.     }

  211.     /** {@inheritDoc} */
  212.     @Override
  213.     @SuppressWarnings("checkstyle:designforextension")
  214.     public String toString()
  215.     {
  216.         return "LaneType [id=" + this.getId() + ", compatibilitySet=" + this.compatibility + "]";
  217.     }

  218.     /** {@inheritDoc} */
  219.     @Override
  220.     @SuppressWarnings("checkstyle:designforextension")
  221.     public int hashCode()
  222.     {
  223.         final int prime = 31;
  224.         int result = super.hashCode();
  225.         result = prime * result + ((this.compatibility == null) ? 0 : this.compatibility.hashCode());
  226.         return result;
  227.     }

  228.     /** {@inheritDoc} */
  229.     @Override
  230.     @SuppressWarnings("checkstyle:designforextension")
  231.     public boolean equals(final Object obj)
  232.     {
  233.         if (this == obj)
  234.         {
  235.             return true;
  236.         }
  237.         if (!super.equals(obj))
  238.         {
  239.             return false;
  240.         }
  241.         if (getClass() != obj.getClass())
  242.         {
  243.             return false;
  244.         }
  245.         LaneType other = (LaneType) obj;
  246.         if (this.compatibility == null)
  247.         {
  248.             if (other.compatibility != null)
  249.             {
  250.                 return false;
  251.             }
  252.         }
  253.         else if (!this.compatibility.equals(other.compatibility))
  254.         {
  255.             return false;
  256.         }
  257.         return true;
  258.     }

  259.     /** {@inheritDoc} */
  260.     @Override
  261.     public final LongitudinalDirectionality getDirectionality(final GTUType gtuType, final boolean tryParentsOfGTUType)
  262.     {
  263.         LongitudinalDirectionality result = this.compatibility.getDirectionality(gtuType, tryParentsOfGTUType);
  264.         if (null == this.compatibility)
  265.         {
  266.             return result.invert();
  267.         }
  268.         return result;
  269.     }

  270. }