1 package org.opentrafficsim.core.network.route;
2
3 import java.util.List;
4
5 import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
6 import org.opentrafficsim.core.gtu.GTUType;
7 import org.opentrafficsim.core.network.Link;
8 import org.opentrafficsim.core.network.Network;
9 import org.opentrafficsim.core.network.NetworkException;
10 import org.opentrafficsim.core.network.Node;
11
12
13
14
15
16
17
18
19
20
21
22
23 public class CompleteRoute extends Route
24 {
25
26 private static final long serialVersionUID = 20150722L;
27
28
29 private final GTUType gtuType;
30
31
32
33
34
35
36 public CompleteRoute(final String id, final GTUType gtuType)
37 {
38 super(id);
39 this.gtuType = gtuType;
40 }
41
42
43
44
45
46
47
48
49
50 public CompleteRoute(final String id, final GTUType gtuType, final List<Node> nodes) throws NetworkException
51 {
52 super(id, nodes);
53 this.gtuType = gtuType;
54 Node fromNode = null;
55 for (Node toNode : getNodes())
56 {
57 if (null != fromNode)
58 {
59 if (!fromNode.isDirectionallyConnectedTo(this.gtuType, toNode))
60 {
61 throw new NetworkException("CompleteRoute: node " + fromNode
62 + " not directly or not directionally connected to node " + toNode);
63 }
64 }
65 fromNode = toNode;
66 }
67 }
68
69
70 @Override
71 public final void addNode(final Node node) throws NetworkException
72 {
73 if (getNodes().size() > 0)
74 {
75 Node lastNode = getNodes().get(getNodes().size() - 1);
76 if (!lastNode.isDirectionallyConnectedTo(this.gtuType, node))
77 {
78 throw new NetworkException("CompleteRoute: last node " + lastNode
79 + " not directly or not directionally connected to node " + node);
80 }
81 }
82 super.addNode(node);
83 }
84
85
86
87
88
89
90 public final boolean containsLink(final Link link)
91 {
92 Node sn = link.getStartNode();
93 Node en = link.getEndNode();
94 for (int index = 1; index < size(); index++)
95 {
96 if (getNodes().get(index) == en && getNodes().get(index - 1) == sn)
97 {
98 return true;
99 }
100 }
101 return false;
102 }
103
104
105 @Override
106 public final String toString()
107 {
108 return "CompleteRoute [gtuType=" + this.gtuType + ", nodes=" + super.getNodes() + "]";
109 }
110
111
112 @Override
113 @SuppressWarnings("checkstyle:designforextension")
114 public CompleteRoute clone(final Network newNetwork, final OTSSimulatorInterface newSimulator, final boolean animation)
115 throws NetworkException
116 {
117 CompleteRoute newRoute = new CompleteRoute(getId(), this.gtuType);
118 for (Node node : getNodes())
119 {
120 newRoute.addNode(newNetwork.getNode(node.getId()));
121 }
122 return newRoute;
123 }
124
125 }