1 package org.opentrafficsim.road.network.lane.object;
2
3 import java.util.LinkedHashSet;
4 import java.util.List;
5 import java.util.Set;
6
7 import org.djunits.value.vdouble.scalar.Length;
8 import org.djutils.immutablecollections.Immutable;
9 import org.djutils.immutablecollections.ImmutableHashSet;
10 import org.djutils.immutablecollections.ImmutableMap;
11 import org.djutils.immutablecollections.ImmutableSet;
12 import org.opentrafficsim.core.gtu.GTUDirectionality;
13 import org.opentrafficsim.core.gtu.GTUType;
14 import org.opentrafficsim.core.network.LongitudinalDirectionality;
15 import org.opentrafficsim.core.network.NetworkException;
16 import org.opentrafficsim.road.network.lane.CrossSectionElement;
17 import org.opentrafficsim.road.network.lane.Lane;
18 import org.opentrafficsim.road.network.lane.conflict.BusStopConflictRule;
19 import org.opentrafficsim.road.network.lane.conflict.Conflict;
20
21 import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 public class BusStop extends AbstractLaneBasedObject
39 {
40
41
42 private static final long serialVersionUID = 20170124L;
43
44
45 private final Set<String> lines = new LinkedHashSet<>();
46
47
48 private final String name;
49
50
51 private Set<Conflict> conflicts = null;
52
53
54
55
56
57
58
59
60
61 public BusStop(final String id, final Lane lane, final Length longitudinalPosition, final String name,
62 final SimulatorInterface.TimeDoubleUnit simulator) throws NetworkException
63 {
64 super(id, lane, LongitudinalDirectionality.DIR_PLUS, longitudinalPosition,
65 LaneBasedObject.makeGeometry(lane, longitudinalPosition), Length.ZERO);
66 this.name = name;
67 }
68
69
70
71
72
73 public final void setLines(final Set<String> lines)
74 {
75 this.lines.clear();
76 this.lines.addAll(lines);
77 }
78
79
80
81
82
83 public final ImmutableSet<String> getLines()
84 {
85 return new ImmutableHashSet<>(this.lines, Immutable.COPY);
86 }
87
88
89
90
91
92 public final Set<Conflict> getConflicts()
93 {
94 if (this.conflicts == null)
95 {
96 this.conflicts = new LinkedHashSet<>();
97 Lane lane = getLane();
98
99 GTUDirectionality dir = getDirection().isForward() ? GTUDirectionality.DIR_PLUS : GTUDirectionality.DIR_MINUS;
100 Length position = getLongitudinalPosition();
101 while (lane != null)
102 {
103 List<LaneBasedObject> objects = lane.getObjectAhead(position, dir);
104 while (objects != null)
105 {
106 for (LaneBasedObject object : objects)
107 {
108 if (object instanceof Conflict)
109 {
110 Conflict./../../../../org/opentrafficsim/road/network/lane/conflict/Conflict.html#Conflict">Conflict conflict = (Conflict) object;
111 if (conflict.getConflictRule() instanceof BusStopConflictRule)
112 {
113 this.conflicts.add(conflict);
114 }
115 }
116 }
117 objects = lane.getObjectAhead(objects.get(0).getLongitudinalPosition(), dir);
118 }
119 ImmutableMap<Lane, GTUDirectionality> downstreamLanes =
120 lane.downstreamLanes(dir, lane.getNetwork().getGtuType(GTUType.DEFAULTS.BUS));
121 int numLanes = 0;
122 for (Lane nextLane : downstreamLanes.keySet())
123 {
124 if (nextLane.getParentLink().getPriority().isBusStop())
125 {
126 numLanes++;
127 lane = nextLane;
128 dir = downstreamLanes.get(lane);
129 position = dir.isPlus() ? Length.ZERO : lane.getLength();
130 }
131 }
132 if (numLanes != 1)
133 {
134 lane = null;
135 }
136 }
137 }
138 return this.conflicts;
139 }
140
141
142 @Override
143 public final int hashCode()
144 {
145 final int prime = 31;
146 int result = 1;
147 result = prime * result + this.getId().hashCode();
148 return result;
149 }
150
151
152 @Override
153 public final boolean equals(final Object obj)
154 {
155 if (this == obj)
156 {
157 return true;
158 }
159 if (obj == null)
160 {
161 return false;
162 }
163 if (getClass() != obj.getClass())
164 {
165 return false;
166 }
167 BusStop../../../../../../org/opentrafficsim/road/network/lane/object/BusStop.html#BusStop">BusStop other = (BusStop) obj;
168 if (!this.getId().equals(other.getId()))
169 {
170 return false;
171 }
172 return true;
173 }
174
175
176 @Override
177 public final String toString()
178 {
179 String out = "BusStop [id=" + getId() + ", lines=";
180 String delim = "";
181 for (String line : this.lines)
182 {
183 out = out + delim + line;
184 delim = "/";
185 }
186 return out + "]";
187 }
188
189
190 @Override
191 public final AbstractLaneBasedObject clone(final CrossSectionElement newCSE,
192 final SimulatorInterface.TimeDoubleUnit newSimulator) throws NetworkException
193 {
194 BusStopnetwork/lane/object/BusStop.html#BusStop">BusStop busStop = new BusStop(getId(), (Lane) newCSE, getLongitudinalPosition(), this.name, newSimulator);
195 busStop.setLines(this.lines);
196 return busStop;
197 }
198
199 }