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
13
14
15
16
17
18
19
20
21
22 public class CompleteRoute extends Route
23 {
24
25 private static final long serialVersionUID = 20150722L;
26
27
28 private final GTUType gtuType;
29
30
31
32
33
34
35 public CompleteRoute(final String id, final GTUType gtuType)
36 {
37 super(id);
38 this.gtuType = gtuType;
39 }
40
41
42
43
44
45
46
47
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
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
87
88
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
98 @Override
99 public final String toString()
100 {
101 return "CompleteRoute [gtuType=" + this.gtuType + ", nodes=" + super.getNodes() + "]";
102 }
103
104
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
119 @Override
120 @SuppressWarnings({"checkstyle:designforextension", "checkstyle:needbraces"})
121 public int hashCode()
122 {
123 final int prime = 31;
124 int result = super.hashCode();
125 result = prime * result + ((gtuType == null) ? 0 : gtuType.hashCode());
126 return result;
127 }
128
129
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);
149 }
150
151 }