001package org.matsim.contrib.travelsummary.events2traveldiaries.travelcomponents;
002
003import org.matsim.api.core.v01.Coord;
004import org.matsim.api.core.v01.Id;
005
006import java.util.LinkedList;
007import java.util.NoSuchElementException;
008
009public class Journey extends TravelComponent {
010    private String trip_idx;
011    private String mainmode = null;
012    private Activity fromAct;
013    private Activity toAct;
014    private boolean carJourney = false;
015    private boolean teleportJourney = false;
016    private LinkedList<Trip> trips = new LinkedList<Trip>();
017    private LinkedList<Transfer> transfers = new LinkedList<Transfer>();
018    private LinkedList<Wait> waits = new LinkedList<Wait>();
019    private LinkedList<Walk> walks = new LinkedList<Walk>();
020    LinkedList<TravelComponent> planElements = new LinkedList<TravelComponent>();
021
022    public Trip addTrip() {
023        Trip trip = new Trip();
024        trip.journey = this;
025        getTrips().add(trip);
026        planElements.add(trip);
027        return trip;
028    }
029
030    public Wait addWait() {
031        Wait wait = new Wait();
032        wait.journey = this;
033        getWaits().add(wait);
034        planElements.add(wait);
035        return wait;
036    }
037
038    public Walk addWalk() {
039        Walk walk = new Walk();
040        walk.journey = this;
041        getWalks().add(walk);
042        planElements.add(walk);
043        return walk;
044    }
045
046    public void addTransfer(Transfer xfer) {
047        xfer.journey = this;
048        getTransfers().add(xfer);
049        planElements.add(xfer);
050    }
051
052    private Coord orig;
053    private Coord dest;
054    private Transfer possibleTransfer;
055    private double carDistance;
056
057    public void incrementCarDistance(double increment) {
058        carDistance += increment;
059    }
060
061    public String toString() {
062        return String.format("JOURNEY: start: %6.0f end: %6.0f dur: %6.0f invehDist: %6.0f walkDist: %6.0f \n %s",
063                getStartTime(), getEndTime(), getDuration(), getInVehDistance(), getWalkDistance(),
064                planElements.toString());
065    }
066
067    public double getInVehDistance() {
068        if (getMainMode().equals("walk"))
069            return 0;
070        if (!isCarJourney()) {
071            double distance = 0;
072            for (Trip t : getTrips()) {
073                distance += t.getDistance();
074            }
075            return distance;
076        }
077        return carDistance;
078    }
079
080    double getWalkDistance() {
081        if (getMainMode().equals("walk"))
082            return walkSpeed * getDuration();
083        if (!isCarJourney()) {
084            double distance = 0;
085            for (Walk w : getWalks()) {
086                distance += w.getDistance();
087            }
088            return distance;
089        }
090        return 0;
091    }
092
093    public double getInVehTime() {
094        if (getMainMode().equals("walk"))
095            return 0;
096        if (!isCarJourney()) {
097            double time = 0;
098            for (Trip t : getTrips()) {
099                time += t.getDuration();
100            }
101            return time;
102        }
103        return getDuration();
104    }
105
106    double getWalkTime() {
107        if (!isCarJourney()) {
108            double time = 0;
109            for (Walk w : getWalks()) {
110                time += w.getDuration();
111            }
112            return time;
113        }
114        return 0;
115    }
116
117    double getWaitTime() {
118        if (!isCarJourney()) {
119            double time = 0;
120            for (Wait w : getWaits()) {
121                time += w.getDuration();
122            }
123            return time;
124        }
125        return 0;
126    }
127
128    public String getMainMode() {
129        if (!(mainmode == null)) {
130            return mainmode;
131        }
132        if (isCarJourney()) {
133            return "car";
134        }
135        try {
136            Trip longestTrip = getTrips().getFirst();
137            if (getTrips().size() > 1) {
138                for (int i = 1; i < getTrips().size(); i++) {
139                    if (getTrips().get(i).getDistance() > longestTrip.getDistance()) {
140                        longestTrip = getTrips().get(i);
141                    }
142                }
143            }
144            return longestTrip.getMode();
145
146        } catch (NoSuchElementException e) {
147            return "walk";
148
149        }
150    }
151
152    public double getDistance() {
153
154        return getInVehDistance() + getWalkDistance();
155    }
156
157    public double getAccessWalkDistance() {
158
159        try {
160            return getWalks().getFirst().getDistance();
161        } catch (NoSuchElementException e) {
162            return 0;
163        }
164    }
165
166    public double getAccessWalkTime() {
167
168        try {
169            return getWalks().getFirst().getDuration();
170        } catch (NoSuchElementException e) {
171            return 0;
172        }
173
174    }
175
176    public double getAccessWaitTime() {
177
178        try {
179            return getWaits().getFirst().getDuration();
180
181        } catch (NoSuchElementException e) {
182            return 0;
183        }
184
185    }
186
187    public double getEgressWalkDistance() {
188        try {
189            for (Walk w : getWalks()) {
190                if (w.isEgressWalk())
191                    return w.getDistance();
192            }
193        } catch (Exception e) {
194            return 0;
195        }
196        return 0;
197
198    }
199
200    public double getEgressWalkTime() {
201        try {
202            for (Walk w : getWalks()) {
203                if (w.isEgressWalk())
204                    return w.getDuration();
205            }
206        } catch (Exception e) {
207            return 0;
208        }
209        return 0;
210    }
211
212    public Activity getFromAct() {
213        return fromAct;
214    }
215
216    public void setFromAct(Activity fromAct) {
217        this.fromAct = fromAct;
218    }
219
220    public Activity getToAct() {
221        return toAct;
222    }
223
224    public void setToAct(Activity toAct) {
225        this.toAct = toAct;
226    }
227
228    public boolean isCarJourney() {
229        return carJourney;
230    }
231
232    public void setCarJourney(boolean carJourney) {
233        this.carJourney = carJourney;
234    }
235
236    public LinkedList<Trip> getTrips() {
237        return trips;
238    }
239
240    public void setTrips(LinkedList<Trip> trips) {
241        this.trips = trips;
242    }
243
244    public LinkedList<Transfer> getTransfers() {
245        return transfers;
246    }
247
248    public void setTransfers(LinkedList<Transfer> transfers) {
249        this.transfers = transfers;
250    }
251
252    public LinkedList<Walk> getWalks() {
253        return walks;
254    }
255
256    public void setWalks(LinkedList<Walk> walks) {
257        this.walks = walks;
258    }
259
260    public Coord getDest() {
261        return dest;
262    }
263
264    public void setDest(Coord dest) {
265        this.dest = dest;
266    }
267
268    public Coord getOrig() {
269        return orig;
270    }
271
272    public void setOrig(Coord orig) {
273        this.orig = orig;
274    }
275
276    public Transfer getPossibleTransfer() {
277        return possibleTransfer;
278    }
279
280    public void setPossibleTransfer(Transfer possibleTransfer) {
281        this.possibleTransfer = possibleTransfer;
282    }
283
284    public LinkedList<Wait> getWaits() {
285        return waits;
286    }
287
288    public void setWaits(LinkedList<Wait> waits) {
289        this.waits = waits;
290    }
291
292    public void setMainmode(String mainmode) {
293        this.mainmode = mainmode;
294    }
295
296    public String getTrip_idx() {
297        return trip_idx;
298    }
299
300    public void setTrip_idx(String trip_idx) {
301        this.trip_idx = trip_idx;
302    }
303
304    public double getCarDistance() {
305        return carDistance;
306    }
307
308    public void setCarDistance(double carDistance) {
309        this.carDistance = carDistance;
310    }
311
312    public double getTransferWalkDistance() {
313        if (!isCarJourney()) {
314            double walkDistance = 0;
315            for (Transfer t : this.getTransfers()) {
316                walkDistance += t.getWalkDistance();
317            }
318            return walkDistance;
319        }
320        return 0;
321    }
322
323    public double getTransferWalkTime() {
324        if (!isCarJourney()) {
325            double walkTime = 0;
326            for (Transfer t : this.getTransfers()) {
327                walkTime += t.getWalkTime();
328            }
329            return walkTime;
330        }
331        return 0;
332    }
333
334    public double getTransferWaitTime() {
335        if (!isCarJourney()) {
336            double waitTime = 0;
337            for (Transfer t : this.getTransfers()) {
338                waitTime += t.getWaitTime();
339            }
340            return waitTime;
341        }
342        return 0;
343    }
344
345    public Id getFirstBoardingStop() {
346        if (!isCarJourney() && this.getTrips().size() > 0) {
347            return this.getTrips().getFirst().getBoardingStop();
348        }
349        return null;
350    }
351
352    public Id getLastAlightingStop() {
353        if (!isCarJourney() && this.getTrips().size() > 0) {
354            return this.getTrips().getLast().getAlightingStop();
355        }
356        return null;
357    }
358
359    public boolean isTeleportJourney() {
360        return teleportJourney;
361    }
362
363    public void setTeleportJourney(boolean teleportJourney) {
364        this.teleportJourney = teleportJourney;
365    }
366
367    public static void setWalkSpeed(double walkSpeed) {
368        Journey.walkSpeed = walkSpeed;
369    }
370}