001package org.unix4j.unix.tail;
002
003import java.util.LinkedList;
004
005import org.unix4j.context.ExecutionContext;
006import org.unix4j.line.Line;
007import org.unix4j.processor.LineProcessor;
008
009class TailLinesFromEndProcessor extends AbstractTailProcessor {
010        
011        private final LinkedList<Line> tailLines = new LinkedList<Line>();
012
013    @Override
014    public void resetCountersAndFlush() {
015        final LineProcessor output = getOutput();
016        boolean more = true;
017        while (!tailLines.isEmpty() && more) {
018            more = output.processLine(tailLines.removeFirst());//remove to free memory
019        }
020        tailLines.clear();
021    }
022
023    public TailLinesFromEndProcessor(TailCommand command, ExecutionContext context, LineProcessor output) {
024                super(command, context, output);
025        }
026
027        @Override
028        public boolean processLine(Line line) {
029                tailLines.add(line);
030                if (tailLines.size() > count) {
031                        tailLines.removeFirst();
032                }
033                return true;//we want all lines
034        }
035
036        @Override
037        public void finish() {
038                final LineProcessor output = getOutput();
039                resetCountersAndFlush();
040                output.finish();
041        }
042}