View Javadoc
1   package org.opentrafficsim.road.network.lane;
2   
3   import java.io.Serializable;
4   import java.util.Collection;
5   import java.util.HashSet;
6   import java.util.Set;
7   
8   import org.opentrafficsim.base.Type;
9   import org.opentrafficsim.core.gtu.GTUType;
10  
11  import nl.tudelft.simulation.immutablecollections.ImmutableHashSet;
12  import nl.tudelft.simulation.immutablecollections.ImmutableSet;
13  import nl.tudelft.simulation.language.Throw;
14  
15  /**
16   * Lane type to indicate compatibility with GTU types. The id of a LaneType should be unique. This is, however, not checked or
17   * enforced, as the LaneType is not a singleton as the result of the compatibilitySet. Different simulations running in the same
18   * GTU can have different compatibilitySets for LaneTypes with the same id. Therefore, uniqueness is not enforced.
19   * <p>
20   * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
21   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
22   * <p>
23   * $LastChangedDate: 2015-08-30 00:16:51 +0200 (Sun, 30 Aug 2015) $, @version $Revision: 1329 $, by $Author: averbraeck $,
24   * initial version Aug 21, 2014 <br>
25   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
26   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
27   * @author <a href="http://www.citg.tudelft.nl">Guus Tamminga</a>
28   */
29  public class LaneType extends Type<LaneType> implements Serializable
30  {
31      /** */
32      private static final long serialVersionUID = 20140821L;
33  
34      /** The id of the LaneType to make it identifiable. */
35      private final String id;
36  
37      /** The compatibility of GTUs with this lane type. */
38      private final ImmutableSet<GTUType> compatibilitySet;
39  
40      /** Lane type that does not allow any vehicles. */
41      public static final LaneType NONE;
42  
43      /** Lane type that allows all vehicles. */
44      public static final LaneType ALL;
45  
46      static
47      {
48          NONE = new LaneType("NONE", new HashSet<GTUType>());
49          Set<GTUType> allSet = new HashSet<>();
50          allSet.add(GTUType.ALL);
51          ALL = new LaneType("ALL", allSet);
52      }
53  
54      /**
55       * Create a new Lane type with an immutable compatibility set.
56       * @param id the id of the lane type.
57       * @param compatibility the collection of compatible GTUTypes for this LaneType
58       * @throws NullPointerException if either the id is null, or the compatibilitySet is null
59       */
60      public LaneType(final String id, final Collection<GTUType> compatibility) throws NullPointerException
61      {
62          Throw.whenNull(id, "id cannot be null for LaneType");
63          Throw.whenNull(compatibility, "compatibility collection cannot be null for LaneType with id = %s", id);
64  
65          this.id = id;
66          this.compatibilitySet = new ImmutableHashSet<>(compatibility);
67      }
68  
69      /**
70       * @param gtuType GTU type to look for compatibility.
71       * @return whether the LaneType is compatible with the GTU type, or compatible with all GTU types.
72       */
73      public final boolean isCompatible(final GTUType gtuType)
74      {
75          return this.compatibilitySet.contains(gtuType) || this.compatibilitySet.contains(GTUType.ALL);
76      }
77  
78      /**
79       * @return id.
80       */
81      public final String getId()
82      {
83          return this.id;
84      }
85  
86      /** {@inheritDoc} */
87      @Override
88      @SuppressWarnings("checkstyle:designforextension")
89      public String toString()
90      {
91          return "LaneType [id=" + this.id + ", compatibilitySet=" + this.compatibilitySet + "]";
92      }
93  
94      /** {@inheritDoc} */
95      @Override
96      @SuppressWarnings("checkstyle:designforextension")
97      public int hashCode()
98      {
99          final int prime = 31;
100         int result = 1;
101         result = prime * result + this.id.hashCode();
102         return result;
103     }
104 
105     /** {@inheritDoc} */
106     @Override
107     @SuppressWarnings({ "checkstyle:designforextension", "checkstyle:needbraces" })
108     public boolean equals(final Object obj)
109     {
110         if (this == obj)
111             return true;
112         if (obj == null)
113             return false;
114         if (getClass() != obj.getClass())
115             return false;
116         LaneType other = (LaneType) obj;
117         if (!this.id.equals(other.id))
118             return false;
119         return true;
120     }
121 
122 }