001package org.unix4j.unix.sort; 002 003import java.util.Iterator; 004import java.util.NavigableSet; 005import java.util.TreeSet; 006 007import org.unix4j.context.ExecutionContext; 008import org.unix4j.line.Line; 009import org.unix4j.processor.LineProcessor; 010 011class UniqueSortProcessor extends AbstractSortProcessor { 012 013 private final NavigableSet<Line> uniqueLines; 014 015 public UniqueSortProcessor(SortCommand command, ExecutionContext context, LineProcessor output) { 016 super(command, context, output); 017 this.uniqueLines = new TreeSet<Line>(getComparator()); 018 } 019 020 @Override 021 public boolean processLine(Line line) { 022 uniqueLines.add(line);//duplicate lines are not even added 023 return true;//we want all lines 024 } 025 026 @Override 027 public void finish() { 028 final LineProcessor output = getOutput(); 029 final Iterator<Line> it = uniqueLines.iterator(); 030 while (it.hasNext()) { 031 final Line line = it.next(); 032 if (!output.processLine(line)) { 033 break;//they want no more lines 034 } 035 it.remove();//remove to free some memory 036 } 037 uniqueLines.clear(); 038 output.finish(); 039 } 040 041}