1 package org.opentrafficsim.core.network;
2
3 import java.io.Serializable;
4 import java.util.ArrayList;
5 import java.util.List;
6
7 import org.opentrafficsim.core.network.lane.CrossSectionLink;
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 public class Route implements Serializable
27 {
28
29 private static final long serialVersionUID = 20150101L;
30
31
32 private final List<Node<?, ?>> nodes;
33
34
35 private int lastNode = -1;
36
37
38
39
40 public Route()
41 {
42 this.nodes = new ArrayList<Node<?, ?>>();
43 }
44
45
46
47
48
49 public Route(final List<Node<?, ?>> nodes)
50 {
51
52 this.nodes = nodes;
53 }
54
55
56
57
58
59
60
61 public final boolean addNode(final Node<?, ?> node) throws NetworkException
62 {
63 try
64 {
65 return this.nodes.add(node);
66 }
67 catch (RuntimeException e)
68 {
69 throw new NetworkException("Route.addNode(Node) could not be executed", e);
70 }
71 }
72
73
74
75
76
77
78
79 public final void addNode(final int i, final Node<?, ?> node) throws NetworkException
80 {
81 try
82 {
83 this.nodes.add(i, node);
84 }
85 catch (IndexOutOfBoundsException e)
86 {
87 throw new NetworkException("Route.addNode(i, Node) was called where i<0 or i>=nodes.size()");
88 }
89 catch (RuntimeException e)
90 {
91 throw new NetworkException("Route.addNode(i, Node) could not be executed", e);
92 }
93
94
95 if (i <= this.lastNode)
96 {
97
98 this.lastNode++;
99 }
100 }
101
102
103
104
105
106
107
108
109 public final Node<?, ?> removeNode(final int i) throws NetworkException
110 {
111 Node<?, ?> result = null;
112 if (i == this.lastNode)
113 {
114 throw new NetworkException("Route.removeNode(i) was called where i was equal to the last visited node");
115 }
116 try
117 {
118 result = this.nodes.remove(i);
119 }
120 catch (RuntimeException e)
121 {
122 throw new NetworkException("Route.removeNode(i, Node) could not be executed", e);
123 }
124
125 if (i < this.lastNode)
126 {
127
128 this.lastNode--;
129 }
130 return result;
131 }
132
133
134
135
136
137
138
139 public final Node<?, ?> getNode(final int i) throws NetworkException
140 {
141 try
142 {
143 return this.nodes.get(i);
144 }
145 catch (IndexOutOfBoundsException e)
146 {
147 throw new NetworkException("Route.getNode(i) was called where i<0 or i>=nodes.size()");
148 }
149 }
150
151
152
153
154
155 public final Node<?, ?> originNode() throws NetworkException
156 {
157 if (this.nodes.size() == 0)
158 {
159 throw new NetworkException("Route.getOrigin() called, but node list has no nodes");
160 }
161 return getNode(0);
162 }
163
164
165
166
167
168 public final int size()
169 {
170 return this.nodes.size();
171 }
172
173
174
175
176
177 public final Node<?, ?> destinationNode() throws NetworkException
178 {
179 if (this.nodes.size() == 0)
180 {
181 throw new NetworkException("Route.getDestination() called, but node list has no nodes");
182 }
183 return getNode(size() - 1);
184 }
185
186
187
188
189
190 public final Node<?, ?> lastVisitedNode() throws NetworkException
191 {
192 if (this.lastNode == -1)
193 {
194 return null;
195 }
196 return getNode(this.lastNode);
197 }
198
199
200
201
202
203
204 public final Node<?, ?> nextNodeToVisit() throws NetworkException
205 {
206 if (this.lastNode >= size() - 1)
207 {
208 return null;
209 }
210 return getNode(this.lastNode + 1);
211 }
212
213
214
215
216
217
218 public final Node<?, ?> visitNextNode() throws NetworkException
219 {
220 if (this.lastNode >= size() - 1)
221 {
222 return null;
223 }
224 this.lastNode++;
225 return getNode(this.lastNode);
226 }
227
228
229
230
231
232
233 public final boolean containsLink(final CrossSectionLink<?, ?> link)
234 {
235 Node<?, ?> sn = link.getStartNode();
236 Node<?, ?> en = link.getEndNode();
237 if (this.nodes.contains(sn) && this.nodes.contains(en))
238 {
239 return this.nodes.indexOf(en) - this.nodes.indexOf(sn) == 1;
240 }
241 return false;
242 }
243
244
245
246
247
248
249
250 public final int indexOf(final Node<?, ?> node)
251 {
252 return this.nodes.indexOf(node);
253 }
254
255
256 @SuppressWarnings("checkstyle:designforextension")
257 public String toString()
258 {
259 StringBuilder result = new StringBuilder();
260 final String currentLocationMark = "<>";
261 result.append("Route: [");
262 String separator = "";
263 if (this.lastNode < 0)
264 {
265 result.append(currentLocationMark);
266 }
267 for (int index = 0; index < this.nodes.size(); index++)
268 {
269 Node<?, ?> node = this.nodes.get(index);
270 result.append(separator + node);
271 if (index == this.lastNode)
272 {
273 result.append(" " + currentLocationMark);
274 }
275 separator = ", ";
276 }
277 result.append("]");
278 return result.toString();
279 }
280
281 }