1 package org.opentrafficsim.road.network.lane;
2
3 import java.io.Serializable;
4 import java.util.ArrayList;
5 import java.util.HashMap;
6 import java.util.List;
7 import java.util.Map;
8
9 import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
10 import org.opentrafficsim.core.geometry.OTSLine3D;
11 import org.opentrafficsim.core.gtu.GTUType;
12 import org.opentrafficsim.core.network.LinkType;
13 import org.opentrafficsim.core.network.LongitudinalDirectionality;
14 import org.opentrafficsim.core.network.Network;
15 import org.opentrafficsim.core.network.NetworkException;
16 import org.opentrafficsim.core.network.Node;
17 import org.opentrafficsim.core.network.OTSLink;
18 import org.opentrafficsim.core.network.OTSNode;
19 import org.opentrafficsim.road.network.lane.changing.LaneKeepingPolicy;
20
21 import nl.tudelft.simulation.event.EventType;
22
23
24
25
26
27
28
29
30
31
32
33
34
35 public class CrossSectionLink extends OTSLink implements Serializable
36 {
37
38 private static final long serialVersionUID = 20141015L;
39
40
41 private final List<CrossSectionElement> crossSectionElementList = new ArrayList<>();
42
43
44 private final List<Lane> lanes = new ArrayList<>();
45
46
47 private final LaneKeepingPolicy laneKeepingPolicy;
48
49
50
51
52
53
54 public static final EventType LANE_ADD_EVENT = new EventType("LANE.ADD");
55
56
57
58
59
60
61 public static final EventType LANE_REMOVE_EVENT = new EventType("LANE.REMOVE");
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 @SuppressWarnings("checkstyle:parameternumber")
78 public CrossSectionLink(final Network network, final String id, final Node startNode, final Node endNode,
79 final LinkType linkType, final OTSLine3D designLine,
80 final Map<GTUType, LongitudinalDirectionality> directionalityMap, final LaneKeepingPolicy laneKeepingPolicy)
81 throws NetworkException
82 {
83 super(network, id, startNode, endNode, linkType, designLine, directionalityMap);
84 this.laneKeepingPolicy = laneKeepingPolicy;
85 }
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101 @SuppressWarnings("checkstyle:parameternumber")
102 public CrossSectionLink(final Network network, final String id, final Node startNode, final Node endNode,
103 final LinkType linkType, final OTSLine3D designLine, final LongitudinalDirectionality directionality,
104 final LaneKeepingPolicy laneKeepingPolicy) throws NetworkException
105 {
106 super(network, id, startNode, endNode, linkType, designLine, directionality);
107 this.laneKeepingPolicy = laneKeepingPolicy;
108 }
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123 public CrossSectionLink(final Network network, final String id, final OTSNode startNode, final OTSNode endNode,
124 final LinkType linkType, final OTSLine3D designLine, final LaneKeepingPolicy laneKeepingPolicy)
125 throws NetworkException
126 {
127 this(network, id, startNode, endNode, linkType, designLine, new HashMap<GTUType, LongitudinalDirectionality>(),
128 laneKeepingPolicy);
129 }
130
131
132
133
134
135
136
137
138
139
140 protected CrossSectionLink(final Network newNetwork, final OTSSimulatorInterface newSimulator, final boolean animation,
141 final CrossSectionLink link) throws NetworkException
142 {
143 super(newNetwork, newSimulator, animation, link);
144 this.laneKeepingPolicy = link.laneKeepingPolicy;
145 for (CrossSectionElement cse : link.crossSectionElementList)
146 {
147 addCrossSectionElement(cse.clone(this, newSimulator, animation));
148 }
149
150 }
151
152
153
154
155
156
157 protected final void addCrossSectionElement(final CrossSectionElement cse)
158 {
159 this.crossSectionElementList.add(cse);
160 if (cse instanceof Lane)
161 {
162 this.lanes.add((Lane) cse);
163 fireEvent(LANE_ADD_EVENT,
164 new Object[] { getNetwork().getId(), getId(), cse.getId(), (Lane) cse, this.lanes.indexOf(cse) });
165 }
166 }
167
168
169
170
171
172 public final List<CrossSectionElement> getCrossSectionElementList()
173 {
174 return this.crossSectionElementList == null ? new ArrayList<>() : new ArrayList<>(this.crossSectionElementList);
175 }
176
177
178
179
180
181 public final LaneKeepingPolicy getLaneKeepingPolicy()
182 {
183 return this.laneKeepingPolicy;
184 }
185
186
187
188
189
190
191 public final CrossSectionElement getCrossSectionElement(final String id)
192 {
193 for (CrossSectionElement cse : this.crossSectionElementList)
194 {
195 if (cse.getId().equals(id))
196 {
197 return cse;
198 }
199 }
200 return null;
201 }
202
203
204
205
206
207 public final List<Lane> getLanes()
208 {
209 return this.lanes == null ? new ArrayList<>() : new ArrayList<>(this.lanes);
210 }
211
212
213 @Override
214 public final String toString()
215 {
216 return "CrossSectionLink [crossSectionElementList=" + this.crossSectionElementList + ", lanes=" + this.lanes
217 + ", laneKeepingPolicy=" + this.laneKeepingPolicy + "]";
218 }
219
220
221 @Override
222 @SuppressWarnings("checkstyle:designforextension")
223 public CrossSectionLink clone(final Network newNetwork, final OTSSimulatorInterface newSimulator, final boolean animation)
224 throws NetworkException
225 {
226 return new CrossSectionLink(newNetwork, newSimulator, animation, this);
227 }
228
229 }