001package jmri.jmrit.timetable;
002
003/**
004 * Define the content of a Stop record.
005 *
006 * @author Dave Sand Copyright (C) 2018
007 */
008public class Stop {
009
010    /**
011     * Create a new stop with default values.
012     * @param trainId The parent train id.
013     * @param seq The next stop sequence number.
014     * @throws IllegalArgumentException STOP_ADD_FAIL
015     */
016    public Stop(int trainId, int seq) {
017        if (_dm.getTrain(trainId) == null) {
018            throw new IllegalArgumentException(TimeTableDataManager.STOP_ADD_FAIL);
019        }
020        _stopId = _dm.getNextId("Stop");  // NOI18N
021        _trainId = trainId;
022        _seq = seq;
023        _dm.addStop(_stopId, this);
024    }
025
026    public Stop(int stopId, int trainId, int stationId, int seq, int duration,
027            int nextSpeed, int arriveTime, int departTime, int stagingTrack, String stopNotes) {
028        _stopId = stopId;
029        _trainId = trainId;
030        _stationId = stationId;
031        _seq = seq;
032        setDuration(duration);
033        setNextSpeed(nextSpeed);
034        setArriveTime(arriveTime);
035        setDepartTime(departTime);
036        setStagingTrack(stagingTrack);
037        setStopNotes(stopNotes);
038    }
039
040    TimeTableDataManager _dm = TimeTableDataManager.getDataManager();
041
042    private final int _stopId;
043    private final int _trainId;
044    private int _stationId = 0;
045    private int _seq = 0;
046    private int _duration = 0;
047    private int _nextSpeed = 0;
048    private int _arriveTime = 0;
049    private int _departTime = 0;
050    private int _stagingTrack = 0;
051    private String _stopNotes = "";
052
053    /**
054     * Make a copy of the stop.
055     * @param trainId The new train id, if zero use the current train id.
056     * @param stationId The new station id.  If zero use the current station id.
057     * @param seq The sequence for the new stop.
058     * @return a new Stop instance.
059     */
060    public Stop getCopy(int trainId, int stationId, int seq) {
061        if (trainId == 0) trainId = getTrainId();
062        if (stationId == 0) stationId = getStationId();
063
064        Stop copy = new Stop(trainId, seq);
065        copy.setStationId(stationId);
066        copy.setDuration(_duration);
067        copy.setNextSpeed(_nextSpeed);
068        copy.setArriveTime(_arriveTime);
069        copy.setDepartTime(_departTime);
070        copy.setStagingTrack(_stagingTrack);
071        copy.setStopNotes(_stopNotes);
072        return copy;
073    }
074
075    public int getStopId() {
076        return _stopId;
077    }
078
079    public int getTrainId() {
080        return _trainId;
081    }
082
083    public int getStationId() {
084        return _stationId;
085    }
086
087    public void setStationId(int newStationId) {
088        int oldDStationId = _stationId;
089        _stationId = newStationId;
090
091        try {
092            _dm.calculateTrain(_trainId, false);
093            _dm.calculateTrain(_trainId, true);
094        } catch (IllegalArgumentException ex) {
095            _stationId = oldDStationId;  // Roll back station change
096            throw ex;
097        }
098    }
099
100    public int getSeq() {
101        return _seq;
102    }
103
104    public void setSeq(int newSeq) {
105        _seq = newSeq;
106    }
107
108    public int getDuration() {
109        return _duration;
110    }
111
112    public void setDuration(int newDuration) {
113        if (newDuration < 0) {
114            throw new IllegalArgumentException(TimeTableDataManager.STOP_DURATION_LT_0);
115        }
116        int oldDuration = _duration;
117        _duration = newDuration;
118
119        try {
120            _dm.calculateTrain(_trainId, false);
121            _dm.calculateTrain(_trainId, true);
122        } catch (IllegalArgumentException ex) {
123            _duration = oldDuration;  // Roll back duration change
124            throw ex;
125        }
126    }
127
128    public int getNextSpeed() {
129        return _nextSpeed;
130    }
131
132    public void setNextSpeed(int newNextSpeed) {
133        if (newNextSpeed < 0) {
134            throw new IllegalArgumentException(TimeTableDataManager.NEXT_SPEED_LT_0);
135        }
136        int oldNextSpeed = _nextSpeed;
137        _nextSpeed = newNextSpeed;
138
139        try {
140            _dm.calculateTrain(_trainId, false);
141            _dm.calculateTrain(_trainId, true);
142        } catch (IllegalArgumentException ex) {
143            _nextSpeed = oldNextSpeed;  // Roll back next speed change
144            throw ex;
145        }
146    }
147
148    public int getArriveTime() {
149        return _arriveTime;
150    }
151
152    public void setArriveTime(int newArriveTime) {
153        _arriveTime = newArriveTime;
154    }
155
156    public int getDepartTime() {
157        return _departTime;
158    }
159
160    public void setDepartTime(int newDepartTime) {
161        _departTime = newDepartTime;
162    }
163
164    public int getStagingTrack() {
165        return _stagingTrack;
166    }
167
168    public void setStagingTrack(int newStagingTrack) {
169        Station station = _dm.getStation(_stationId);
170        if (newStagingTrack < 0 || newStagingTrack > station.getStaging()) {
171            throw new IllegalArgumentException(TimeTableDataManager.STAGING_RANGE);
172        }
173
174        _stagingTrack = newStagingTrack;
175    }
176
177    public String getStopNotes() {
178        return _stopNotes;
179    }
180
181    public void setStopNotes(String newNotes) {
182        _stopNotes = newNotes;
183    }
184
185    @Override
186    public String toString() {
187        TimeTableDataManager dataMgr = TimeTableDataManager.getDataManager();
188        Station station = dataMgr.getStation(_stationId);
189        return _seq + " :: " + station.getStationName();
190    }
191
192//     private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(Stop.class);
193}