001package jmri.jmrix.internal;
002
003import java.util.Deque;
004import java.util.ArrayDeque;
005import jmri.implementation.AbstractReporter;
006import jmri.CollectingReporter;
007
008/**
009 * Extension of the AbstractReporter class that implements CollectingReporter
010 * and represents the contents of a track.  This is an internal construct that
011 * does not correspond to a physical reporter.
012 *
013 * @author Paul Bender Copyright (C) 2019
014 */
015public class TrackReporter extends AbstractReporter implements CollectingReporter {
016
017    private Deque collection = null;
018
019    public TrackReporter(String systemName) {
020        super(systemName);
021        collection = new ArrayDeque<Object>();
022    }
023
024    public TrackReporter(String systemName, String userName) {
025        super(systemName, userName);
026        collection = new ArrayDeque<Object>();
027    }
028
029    /**
030     * Provide a general method for updating the report.
031     */
032    @Override
033    public void setReport(Object r) {
034        if (r == _currentReport) {
035            return;
036        }
037        Object old = _currentReport;
038        Object oldLast = _lastReport;
039        _currentReport = r;
040        if (r != null) {
041            _lastReport = r;
042            // notify
043            firePropertyChange("lastReport", oldLast, _lastReport);
044        }
045        // notify
046        firePropertyChange("currentReport", old, _currentReport);
047    }
048            
049    @Override
050    public int getState() {
051       return state;
052    }
053
054    @Override
055    public void setState(int s) {
056       state = s;
057    }
058    int state = 0;
059
060    //CollectingReporter Interface Method(s)
061    /**
062     * @return the collection of elements associated with this reporter.
063     */
064    @Override
065    public java.util.Collection getCollection(){
066       return(collection);
067    }
068
069    // Special methods to set the report from the ends of the track
070    // these methods record the order of reports seen.
071
072    @SuppressWarnings("unchecked")
073    public void pushEast(Object o){
074         if(o != null) {
075            collection.addFirst(o);
076            setReport(o);
077         }
078    }
079
080    @SuppressWarnings("unchecked")
081    public void pushWest(Object o){
082         if(o != null) {
083            collection.addLast(o);
084            setReport(o);
085         }
086    }
087
088    public Object pullEast(){
089       Object retval = collection.removeFirst();
090       setReport(collection.peekFirst());
091       return retval;
092    }
093
094    public Object pullWest(){
095       Object retval = collection.removeLast();
096       setReport(collection.peekLast());
097       return retval;
098    }
099
100}