View Javadoc
1   package org.opentrafficsim.core.network.route;
2   
3   import java.util.List;
4   
5   import org.opentrafficsim.core.gtu.GTUType;
6   import org.opentrafficsim.core.network.Link;
7   import org.opentrafficsim.core.network.Network;
8   import org.opentrafficsim.core.network.NetworkException;
9   import org.opentrafficsim.core.network.Node;
10  
11  /**
12   * A CompleteRoute is a Route with directly connected Nodes.
13   * <p>
14   * Copyright (c) 2013-2020 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   * $LastChangedDate: 2015-07-16 10:20:53 +0200 (Thu, 16 Jul 2015) $, @version $Revision: 1124 $, by $Author: pknoppers $,
18   * initial version Jul 22, 2015 <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 class CompleteRoute extends Route
23  {
24      /** */
25      private static final long serialVersionUID = 20150722L;
26  
27      /** The GTUType for which this is a route. */
28      private final GTUType gtuType;
29  
30      /**
31       * Create an empty route for the given GTUType.
32       * @param id String; the name of the route
33       * @param gtuType GTUType; the GTUType for which this is a route
34       */
35      public CompleteRoute(final String id, final GTUType gtuType)
36      {
37          super(id);
38          this.gtuType = gtuType;
39      }
40  
41      /**
42       * Create a route based on an initial list of nodes. <br>
43       * This constructor makes a defensive copy of the provided List.
44       * @param id String; the name of the route.
45       * @param gtuType GTUType; the GTUType for which this is a route
46       * @param nodes List&lt;Node&gt;; the initial list of nodes.
47       * @throws NetworkException if intermediate nodes are missing in the route.
48       */
49      public CompleteRoute(final String id, final GTUType gtuType, final List<Node> nodes) throws NetworkException
50      {
51          super(id, nodes);
52          this.gtuType = gtuType;
53          Node fromNode = null;
54          for (Node toNode : getNodes())
55          {
56              if (null != fromNode)
57              {
58                  if (!fromNode.isDirectionallyConnectedTo(this.gtuType, toNode))
59                  {
60                      throw new NetworkException("CompleteRoute: node " + fromNode
61                              + " not directly or not directionally connected to node " + toNode);
62                  }
63              }
64              fromNode = toNode;
65          }
66      }
67  
68      /** {@inheritDoc} */
69      @Override
70      public final CompleteRoute addNode(final Node node) throws NetworkException
71      {
72          if (getNodes().size() > 0)
73          {
74              Node lastNode = getNodes().get(getNodes().size() - 1);
75              if (!lastNode.isDirectionallyConnectedTo(this.gtuType, node))
76              {
77                  throw new NetworkException("CompleteRoute: last node " + lastNode
78                          + " not directly or not directionally connected to node " + node);
79              }
80          }
81          super.addNode(node);
82          return this;
83      }
84  
85      /**
86       * Determine if this Route contains the specified Link.
87       * @param link Link; the link to check in the route.
88       * @return whether the link is part of the route or not.
89       */
90      public final boolean containsLink(final Link link)
91      {
92          int index1 = getNodes().indexOf(link.getStartNode());
93          int index2 = getNodes().indexOf(link.getEndNode());
94          return index1 >= 0 && index2 >= 0 && Math.abs(index2 - index1) == 1;
95      }
96  
97      /** {@inheritDoc} */
98      @Override
99      public final String toString()
100     {
101         return "CompleteRoute [gtuType=" + this.gtuType + ", nodes=" + super.getNodes() + "]";
102     }
103 
104     /** {@inheritDoc} */
105     @Override
106     @SuppressWarnings("checkstyle:designforextension")
107     public CompleteRoute clone(final Network newNetwork)
108             throws NetworkException
109     {
110         CompleteRouteute/CompleteRoute.html#CompleteRoute">CompleteRoute newRoute = new CompleteRoute(getId(), this.gtuType);
111         for (Node node : getNodes())
112         {
113             newRoute.addNode(newNetwork.getNode(node.getId()));
114         }
115         return newRoute;
116     }
117 
118     /** {@inheritDoc} */
119     @Override
120     @SuppressWarnings({"checkstyle:designforextension", "checkstyle:needbraces"})
121     public int hashCode()
122     {
123         final int prime = 31;
124         int result = super.hashCode(); // BE WARE
125         result = prime * result + ((gtuType == null) ? 0 : gtuType.hashCode());
126         return result;
127     }
128 
129     /** {@inheritDoc} */
130     @Override
131     @SuppressWarnings({"checkstyle:designforextension", "checkstyle:needbraces"})
132     public boolean equals(final Object obj)
133     {
134         if (this == obj)
135             return true;
136         if (obj == null)
137             return false;
138         if (getClass() != obj.getClass())
139             return false;
140         CompleteRoute../../../org/opentrafficsim/core/network/route/CompleteRoute.html#CompleteRoute">CompleteRoute other = (CompleteRoute) obj;
141         if (gtuType == null)
142         {
143             if (other.gtuType != null)
144                 return false;
145         }
146         else if (!gtuType.equals(other.gtuType))
147             return false;
148         return super.equals(other); // BE WARE
149     }
150 
151 }