1 package org.opentrafficsim.road.gtu.lane.tactical.pt;
2
3 import java.util.HashMap;
4 import java.util.List;
5 import java.util.Map;
6 import java.util.Set;
7
8 import org.djunits.value.vdouble.scalar.Duration;
9 import org.djunits.value.vdouble.scalar.Time;
10 import org.opentrafficsim.core.network.Node;
11 import org.opentrafficsim.core.network.route.Route;
12
13 import nl.tudelft.simulation.language.Throw;
14
15
16
17
18
19
20
21
22
23
24
25 public class BusSchedule extends Route
26 {
27
28
29 private static final long serialVersionUID = 20170124L;
30
31
32 private final String line;
33
34
35 private final Map<String, BusStopInfo> schedule = new HashMap<>();
36
37
38 private final Map<String, Time> actualDeparturesBusStop = new HashMap<>();
39
40
41 private final Map<String, Time> actualDeparturesConflict = new HashMap<>();
42
43
44
45
46
47
48 public BusSchedule(final String id, final List<Node> nodes, final String line)
49 {
50 super(id, nodes);
51 this.line = line;
52 }
53
54
55
56
57
58 public BusSchedule(final String id, final String line)
59 {
60 super(id);
61 this.line = line;
62 }
63
64
65
66
67
68
69
70
71 public final void addBusStop(final String busStopId, final Time departureTime, final Duration dwellTime,
72 final boolean forceSchedule)
73 {
74 Throw.whenNull(busStopId, "Bus stop id may not be null.");
75 Throw.whenNull(departureTime, "Departure time may not be null.");
76 Throw.whenNull(dwellTime, "Dwell time may not be null.");
77 this.schedule.put(busStopId, new BusStopInfo(departureTime, dwellTime, forceSchedule));
78 }
79
80
81
82
83
84
85
86 public final boolean isLineStop(final String busStopId, final Time time)
87 {
88 return this.schedule.containsKey(busStopId) && (!this.actualDeparturesConflict.containsKey(busStopId)
89 || time.lt(this.actualDeparturesConflict.get(busStopId)));
90 }
91
92
93
94
95
96
97 public final Time getDepartureTime(final String busStopId)
98 {
99 checkStop(busStopId);
100 return this.schedule.get(busStopId).getDepartureTime();
101 }
102
103
104
105
106
107
108 public final Duration getDwellTime(final String busStopId)
109 {
110 checkStop(busStopId);
111 return this.schedule.get(busStopId).getDwellTime();
112 }
113
114
115
116
117
118
119 public final boolean isForceSchedule(final String busStopId)
120 {
121 checkStop(busStopId);
122 return this.schedule.get(busStopId).isForceSchedule();
123 }
124
125
126
127
128
129
130 private void checkStop(final String busStopId)
131 {
132 Throw.when(!this.schedule.containsKey(busStopId), IllegalArgumentException.class, "Bus stop %s is not for schedule %s.",
133 busStopId, this);
134 }
135
136
137
138
139
140
141
142 public final void setActualDeparture(final String busStopId, final Set<String> conflictIds, final Time time)
143 {
144 this.actualDeparturesBusStop.put(busStopId, time);
145 for (String conflictId : conflictIds)
146 {
147 this.actualDeparturesConflict.put(conflictId, time);
148 }
149 }
150
151
152
153
154
155
156 public final Time getActualDepartureBusStop(final String busStopId)
157 {
158 return this.actualDeparturesBusStop.get(busStopId);
159 }
160
161
162
163
164
165
166 public final Time getActualDepartureConflict(final String conflictId)
167 {
168 return this.actualDeparturesConflict.get(conflictId);
169 }
170
171
172
173
174 public final String getLine()
175 {
176 return this.line;
177 }
178
179
180 @Override
181 public final String toString()
182 {
183 return "BusSchedule [id=" + getId() + ", line=" + this.line + "]";
184 }
185
186
187
188
189
190
191
192
193
194
195
196
197
198 private class BusStopInfo
199 {
200
201
202 private final Time departureTime;
203
204
205 private final Duration dwellTime;
206
207
208 private final boolean forceSchedule;
209
210
211
212
213
214
215 BusStopInfo(final Time departureTime, final Duration dwellTime, final boolean forceSchedule)
216 {
217 this.departureTime = departureTime;
218 this.dwellTime = dwellTime;
219 this.forceSchedule = forceSchedule;
220 }
221
222
223
224
225 public final Time getDepartureTime()
226 {
227 return this.departureTime;
228 }
229
230
231
232
233 public final Duration getDwellTime()
234 {
235 return this.dwellTime;
236 }
237
238
239
240
241 public final boolean isForceSchedule()
242 {
243 return this.forceSchedule;
244 }
245
246
247 @Override
248 public String toString()
249 {
250 return "BusStopInfo [departureTime=" + this.departureTime + ", dwellTime=" + this.dwellTime + ", forceSchedule="
251 + this.forceSchedule + "]";
252 }
253
254 }
255
256 }