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<Object> collection = null; 018 019 public TrackReporter(String systemName) { 020 super(systemName); 021 collection = new ArrayDeque<>(); 022 } 023 024 public TrackReporter(String systemName, String userName) { 025 super(systemName, userName); 026 collection = new ArrayDeque<>(); 027 } 028 029 @Override 030 public int getState() { 031 return state; 032 } 033 034 @Override 035 public void setState(int s) { 036 state = s; 037 } 038 int state = 0; 039 040 //CollectingReporter Interface Method(s) 041 /** 042 * @return the collection of elements associated with this reporter. 043 */ 044 @Override 045 public java.util.Collection<Object> getCollection(){ 046 return(collection); 047 } 048 049 // Special methods to set the report from the ends of the track 050 // these methods record the order of reports seen. 051 052 public void pushEast(Object o){ 053 if(o != null) { 054 collection.addFirst(o); 055 setReport(o); 056 } 057 } 058 059 public void pushWest(Object o){ 060 if(o != null) { 061 collection.addLast(o); 062 setReport(o); 063 } 064 } 065 066 public Object pullEast(){ 067 Object retval = collection.removeFirst(); 068 setReport(collection.peekFirst()); 069 return retval; 070 } 071 072 public Object pullWest(){ 073 Object retval = collection.removeLast(); 074 setReport(collection.peekLast()); 075 return retval; 076 } 077 078}