001package org.matsim.contribs.discrete_mode_choice.replanning.time_interpreter;
002
003import org.matsim.api.core.v01.population.Activity;
004
005/**
006 * This TimeInterpreter first checks whehther the end time of an activity is
007 * set. If it exists, the new time along the plan is set to this time. If not,
008 * the maximum activity duration is added to the current time. It is not allowed
009 * than neither end time nor maximum duration are defined.
010 * 
011 * Note that this behaviour can make the time "jump back" if the duration of a
012 * leg exceeds the end time of the following activity. This behaviour can be
013 * avoided by setting onlyAdvance to true.
014 * 
015 * @author sebhoerl
016 */
017public class EndTimeThenDurationInterpreter extends AbstractTimeInterpreter {
018        public EndTimeThenDurationInterpreter(double startTime, boolean onlyAdvance) {
019                super(startTime, startTime, onlyAdvance);
020        }
021
022        private EndTimeThenDurationInterpreter(double currentTime, double previousTime, boolean onlyAdvance) {
023                super(currentTime, previousTime, onlyAdvance);
024        }
025
026        @Override
027        public void addActivity(Activity activity) {
028                if (activity.getEndTime().isDefined()) {
029                        advance(activity.getEndTime().seconds());
030                } else if (activity.getMaximumDuration().isDefined()) {
031                        advance(currentTime + activity.getMaximumDuration().seconds());
032                } else {
033                        throw new IllegalStateException("Found activity having neither an end time nor a maximum duration");
034                }
035        }
036
037        @Override
038        public TimeInterpreter fork() {
039                return new EndTimeThenDurationInterpreter(currentTime, previousTime, onlyAdvance);
040        }
041
042        static public class Factory implements TimeInterpreter.Factory {
043                private final double startTime;
044                private final boolean onlyAdvance;
045
046                public Factory(double startTime, boolean onlyAdvance) {
047                        this.startTime = startTime;
048                        this.onlyAdvance = onlyAdvance;
049                }
050
051                @Override
052                public TimeInterpreter createTimeInterpreter() {
053                        return new EndTimeThenDurationInterpreter(startTime, onlyAdvance);
054                }
055        }
056}