View Javadoc
1   package org.opentrafficsim.core.gtu;
2   
3   import java.io.Serializable;
4   
5   import org.opentrafficsim.base.Type;
6   
7   import nl.tudelft.simulation.language.Throw;
8   
9   /**
10   * A GTU type identifies the type of a GTU. <br>
11   * GTU types are used to check whether a particular GTU can travel over a particular part of infrastructure. E.g. a
12   * (LaneBased)GTU with GTUType CAR can travel over lanes that have a LaneType that has the GTUType CAR in the compatibility set.
13   * <p>
14   * Copyright (c) 2013-2017 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
15   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
16   * <p>
17   * @version $Revision: 3570 $, $LastChangedDate: 2017-04-29 12:51:08 +0200 (Sat, 29 Apr 2017) $, by $Author: averbraeck $,
18   *          initial version Dec 31, 2014 <br>
19   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
20   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
21   */
22  public final class GTUType extends Type<GTUType> implements Serializable
23  {
24      /** */
25      private static final long serialVersionUID = 20141231L;
26  
27      /** The id of the GTUType to make it identifiable. */
28      private final String id;
29  
30      /** Parent GTUType. */
31      private final GTUType parent;
32  
33      /** ALL GTUType to be used only for permeability and accessibility. */
34      public static final GTUType ALL;
35  
36      /** NONE GTUType to be used only for permeability and accessibility. */
37      public static final GTUType NONE;
38  
39      /** Super type for pedestrians. */
40      public static final GTUType PEDESTRIAN;
41  
42      /** Super type for bikes. */
43      public static final GTUType BIKE;
44  
45      /** Super type for vehicles. */
46      public static final GTUType VEHICLE;
47  
48      /** Super type for boats. */
49      public static final GTUType BOAT;
50  
51      /** Super type for trains. */
52      public static final GTUType TRAIN;
53  
54      /* static block to guarantee that ALL is always on the first place, and NONE on the second, for code reproducibility. */
55      static
56      {
57          ALL = new GTUType("ALL");
58          NONE = new GTUType("NONE");
59          PEDESTRIAN = new GTUType("PEDESTRIAN", ALL);
60          BIKE = new GTUType("BIKE", ALL);
61          VEHICLE = new GTUType("VEHICLE", ALL);
62          BOAT = new GTUType("BOAT", ALL);
63          TRAIN = new GTUType("TRAIN", ALL);
64      }
65  
66      /**
67       * @param id The id of the GTUType to make it identifiable.
68       * @throws NullPointerException if the id is null
69       */
70      private GTUType(final String id) throws NullPointerException
71      {
72          Throw.whenNull(id, "id cannot be null for GTUType");
73          this.id = id;
74          this.parent = null;
75      }
76  
77      /**
78       * @param id The id of the GTUType to make it identifiable.
79       * @param parent GTUType; parent GTU type
80       * @throws NullPointerException if the id is null
81       */
82      public GTUType(final String id, final GTUType parent) throws NullPointerException
83      {
84          Throw.whenNull(id, "id cannot be null for GTUType");
85          this.id = id;
86          this.parent = parent;
87      }
88  
89      /**
90       * @return id.
91       */
92      public String getId()
93      {
94          return this.id;
95      }
96  
97      /**
98       * @return parent.
99       */
100     public GTUType getParent()
101     {
102         return this.parent;
103     }
104 
105     /**
106      * Whether this, or any of the parent GTU types, equals the given GTU type.
107      * @param gtuType GTUType; gtu type
108      * @return whether this, or any of the parent GTU types, equals the given GTU type
109      */
110     public boolean isOfType(final GTUType gtuType)
111     {
112         if (this.equals(gtuType))
113         {
114             return true;
115         }
116         if (this.parent != null)
117         {
118             return this.parent.isOfType(gtuType);
119         }
120         return false;
121     }
122 
123     /** {@inheritDoc} */
124     public String toString()
125     {
126         return "GTUType: " + this.id;
127     }
128 
129     /** {@inheritDoc} */
130     @Override
131     public int hashCode()
132     {
133         final int prime = 31;
134         int result = 1;
135         result = prime * result + ((this.id == null) ? 0 : this.id.hashCode());
136         result = prime * result + ((this.parent == null) ? 0 : this.parent.hashCode());
137         return result;
138     }
139 
140     /** {@inheritDoc} */
141     @SuppressWarnings("checkstyle:needbraces")
142     @Override
143     public boolean equals(final Object obj)
144     {
145         if (this == obj)
146             return true;
147         if (obj == null)
148             return false;
149         if (getClass() != obj.getClass())
150             return false;
151         GTUType other = (GTUType) obj;
152         if (!this.id.equals(other.id))
153             return false;
154         if (this.parent == null)
155             if (other.parent != null)
156                 return false;
157             else if (!this.parent.equals(other.parent))
158                 return false;
159         return true;
160     }
161 
162 }