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
18
19
20
21
22
23
24
25
26
27
28
29 public class Route implements Serializable, Identifiable
30 {
31
32 private static final long serialVersionUID = 20150101L;
33
34
35 private final List<Node> nodes;
36
37
38 private final Set<Node> nodeSet = new HashSet<>();
39
40
41 private final String id;
42
43
44
45
46
47 public Route(final String id)
48 {
49 this.nodes = new ArrayList<>();
50 this.id = id;
51 }
52
53
54
55
56
57
58 public Route(final String id, final List<Node> nodes)
59 {
60 this.id = id;
61 this.nodes = new ArrayList<>(nodes);
62 this.nodeSet.addAll(nodes);
63 }
64
65
66
67
68
69
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
81
82 public final List<Node> getNodes()
83 {
84 return this.nodes;
85 }
86
87
88
89
90
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
103
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
116
117
118 public final int size()
119 {
120 return this.nodes.size();
121 }
122
123
124
125
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
138
139
140
141
142 public final int indexOf(final Node node)
143 {
144 return this.nodes.indexOf(node);
145 }
146
147
148
149
150
151 public final boolean contains(final Node node)
152 {
153 return this.nodeSet.contains(node);
154 }
155
156
157
158
159 @Override
160 public final String getId()
161 {
162 return this.id;
163 }
164
165
166 @Override
167 @SuppressWarnings("checkstyle:designforextension")
168 public String toString()
169 {
170 return "Route [id=" + this.id + ", nodes=" + this.nodes + "]";
171 }
172
173
174
175
176
177
178
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 }