001package org.unix4j.unix.grep;
002
003import org.unix4j.context.ExecutionContext;
004import org.unix4j.line.Line;
005import org.unix4j.line.SimpleLine;
006import org.unix4j.processor.LineProcessor;
007import org.unix4j.util.Counter;
008
009/**
010 * Writes all matching lines to the output. The matching operation is delegated
011 * to the {@link LineMatcher} passed to the constructor. 
012 */
013final class WriteMatchingLinesWithLineNumberProcessor extends AbstractGrepProcessor {
014
015        private final Counter lineCounter = new Counter();
016
017        public WriteMatchingLinesWithLineNumberProcessor(GrepCommand command, ExecutionContext context, LineProcessor output, LineMatcher matcher) {
018                super(command, context, output, matcher);
019        }
020
021        @Override
022        protected boolean processLine(Line line, boolean isMatch) {
023                lineCounter.increment();
024                if (isMatch) {
025                        return getOutput().processLine(new SimpleLine(
026                                        lineCounter.getCount() + ":" + line.getContent(), line.getLineEnding()
027                        ));
028                }
029                return true;//this line is not a match, but we still want the next line
030        }
031}