View Javadoc
1   package org.opentrafficsim.core.network.route;
2   
3   import java.io.Serializable;
4   import java.util.ArrayList;
5   import java.util.HashSet;
6   import java.util.List;
7   import java.util.Set;
8   
9   import org.opentrafficsim.base.Identifiable;
10  import org.opentrafficsim.core.network.Network;
11  import org.opentrafficsim.core.network.NetworkException;
12  import org.opentrafficsim.core.network.Node;
13  
14  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
15  
16  /**
17   * A Route consists of a list of Nodes. A route does not have to be complete. As long as all 'splitting' nodes are part of the
18   * route and have a valid successor node (connected by a Link), the strategical planner is able to make a plan. An extension of
19   * the Route class exists that contains a complete route, where all nodes on the route have to be present and connected.
20   * <p>
21   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
22   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
23   * <p>
24   * $LastChangedDate: 2019-01-06 01:35:05 +0100 (Sun, 06 Jan 2019) $, @version $Revision: 4831 $, by $Author: averbraeck $,
25   * initial version Jan 1, 2015 <br>
26   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
27   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
28   */
29  public class Route implements Serializable, Identifiable
30  {
31      /** */
32      private static final long serialVersionUID = 20150101L;
33  
34      /** The nodes of the route. */
35      private final List<Node> nodes;
36  
37      /** The nodes of the route as a Set for quick containsNode() method. */
38      private final Set<Node> nodeSet = new HashSet<>();
39  
40      /** Name of the route. */
41      private final String id;
42  
43      /**
44       * Create an empty route.
45       * @param id String; the name of the route.
46       */
47      public Route(final String id)
48      {
49          this.nodes = new ArrayList<>();
50          this.id = id;
51      }
52  
53      /**
54       * Create a route based on an initial list of nodes. <br>
55       * @param nodes List&lt;Node&gt;; the initial list of nodes.
56       * @param id String; the name of the route.
57       */
58      public Route(final String id, final List<Node> nodes)
59      {
60          this.id = id;
61          this.nodes = new ArrayList<>(nodes); // defensive copy
62          this.nodeSet.addAll(nodes);
63      }
64  
65      /**
66       * Add a node to the end of the node list.
67       * @param node Node; the node to add.
68       * @return Route; this route for method chaining
69       * @throws NetworkException in case node could not be added to the route.
70       */
71      @SuppressWarnings("checkstyle:designforextension")
72      public Route addNode(final Node node) throws NetworkException
73      {
74          this.nodes.add(node);
75          this.nodeSet.add(node);
76          return this;
77      }
78  
79      /**
80       * @return nodes.
81       */
82      public final List<Node> getNodes()
83      {
84          return this.nodes;
85      }
86  
87      /**
88       * @param i int; the index of the node to obtain
89       * @return node i.
90       * @throws NetworkException if i &lt; 0 or i &gt; size
91       */
92      public final Node getNode(final int i) throws NetworkException
93      {
94          if (i < 0 || i >= this.nodes.size())
95          {
96              throw new NetworkException("Route.getNode(i=" + i + "); i<0 or i>size=" + size());
97          }
98          return this.nodes.get(i);
99      }
100 
101     /**
102      * @return the first node of the route.
103      * @throws NetworkException when the list has no nodes.
104      */
105     public final Node originNode() throws NetworkException
106     {
107         if (this.nodes.size() == 0)
108         {
109             throw new NetworkException("Route.getOrigin() called, but node list has no nodes");
110         }
111         return this.nodes.get(0);
112     }
113 
114     /**
115      * @return the number of nodes in the list. If the list contains more than Integer.MAX_VALUE elements, returns
116      *         Integer.MAX_VALUE.
117      */
118     public final int size()
119     {
120         return this.nodes.size();
121     }
122 
123     /**
124      * @return the last node of the route.
125      * @throws NetworkException when the list has no nodes.
126      */
127     public final Node destinationNode() throws NetworkException
128     {
129         if (this.nodes.size() == 0)
130         {
131             throw new NetworkException("Route.getDestination() called, but node list has no nodes");
132         }
133         return this.nodes.get(size() - 1);
134     }
135 
136     /**
137      * Return the index of a Node in this Route, or -1 if this Route does not contain the specified Node. <br>
138      * If this route contains the Node more than once, the index of the first is returned.
139      * @param node Node; the Node to find
140      * @return int;
141      */
142     public final int indexOf(final Node node)
143     {
144         return this.nodes.indexOf(node);
145     }
146 
147     /**
148      * @param node Node; the Node to find
149      * @return whether the route contains this node (quick using HashSet);
150      */
151     public final boolean contains(final Node node)
152     {
153         return this.nodeSet.contains(node);
154     }
155 
156     /**
157      * @return name.
158      */
159     @Override
160     public final String getId()
161     {
162         return this.id;
163     }
164 
165     /** {@inheritDoc} */
166     @Override
167     @SuppressWarnings("checkstyle:designforextension")
168     public String toString()
169     {
170         return "Route [id=" + this.id + ", nodes=" + this.nodes + "]";
171     }
172 
173     /**
174      * Clone the Route.
175      * @param newNetwork Network; the new network
176      * @param newSimulator SimulatorInterface.TimeDoubleUnit; the new simulator for this network
177      * @return a clone of this route
178      * @throws NetworkException in case the cloning fails
179      */
180     @SuppressWarnings("checkstyle:designforextension")
181     public Route clone(final Network newNetwork, final SimulatorInterface.TimeDoubleUnit newSimulator) throws NetworkException
182     {
183         Route newRoute = new Route(this.id);
184         for (Node node : this.nodes)
185         {
186             newRoute.addNode(newNetwork.getNode(node.getId()));
187         }
188         return newRoute;
189     }
190 }