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