Author: bodewig
Date: Thu Aug 27 07:17:24 2009
New Revision: 808304

URL: http://svn.apache.org/viewvc?rev=808304&view=rev
Log:
UniqFilter is not only simpler if implemented as a TokenFilter, it is even more 
useful as well

Added:
    ant/core/trunk/src/tests/antunit/filters/expected/unique-columns.txt   
(with props)
    ant/core/trunk/src/tests/antunit/filters/input/unique-columns.txt   (with 
props)
Modified:
    ant/core/trunk/WHATSNEW
    ant/core/trunk/docs/manual/CoreTypes/filterchain.html
    ant/core/trunk/src/main/org/apache/tools/ant/filters/UniqFilter.java
    ant/core/trunk/src/tests/antunit/filters/uniq-test.xml

Modified: ant/core/trunk/WHATSNEW
URL: 
http://svn.apache.org/viewvc/ant/core/trunk/WHATSNEW?rev=808304&r1=808303&r2=808304&view=diff
==============================================================================
--- ant/core/trunk/WHATSNEW (original)
+++ ant/core/trunk/WHATSNEW Thu Aug 27 07:17:24 2009
@@ -927,8 +927,8 @@
    added.
    Bugzilla Report 40504.
 
- * A new filterreader <uniqfilter> that suppresses lines that match
-   their ancestor line has been added.
+ * A new token filter <uniqfilter> that suppresses tokens that match
+   their ancestor token has been added.
 
 Changes from Ant 1.7.0 TO Ant 1.7.1
 =============================================

Modified: ant/core/trunk/docs/manual/CoreTypes/filterchain.html
URL: 
http://svn.apache.org/viewvc/ant/core/trunk/docs/manual/CoreTypes/filterchain.html?rev=808304&r1=808303&r2=808304&view=diff
==============================================================================
--- ant/core/trunk/docs/manual/CoreTypes/filterchain.html (original)
+++ ant/core/trunk/docs/manual/CoreTypes/filterchain.html Thu Aug 27 07:17:24 
2009
@@ -125,7 +125,6 @@
 <a href="#tokenfilter">TokenFilter</a><br>
 <a href="../CoreTasks/fixcrlf.html">FixCRLF</a><br>
 <a href="#sortfilter">SortFilter</a><br>
-<a href="#uniqfilter">UniqFilter</a><br>
 
 <h3><a name="filterreader">FilterReader</a></h3>
 
@@ -1032,6 +1031,7 @@
         <a href="#trim">Trim</a><br>
         <a href="#ignoreblank">IgnoreBlank</a><br>
         <a href="#filterdeletecharacters">DeleteCharacters</a><br>
+        <a href="#uniqfilter">UniqFilter</a><br>
     </p>
 
     The following string filters are provided by the optional distribution.
@@ -1360,6 +1360,20 @@
 
 </pre></blockquote>
 
+<p><b><em><a name="uniqfilter">UniqFilter</a></em></b></p>
+
+<p>Suppresses all tokens that match their ancestor token.  It is most
+  useful if combined with a sort filter.</p>
+
+<h4>Example:</h4>
+
+This suppresses duplicate lines.
+<blockquote><pre>
+&lt;tokenfilter&gt;
+  &lt;uniqfilter/&gt;
+&lt;/tokenfilter&gt;
+</pre></blockquote>
+
 <p><b><em><a name="scriptfilter">ScriptFilter</a></em></b></p>
 This is an optional filter that executes a script in a
 <a href="http://jakarta.apache.org/bsf"; target="_top">Apache BSF</a>
@@ -1495,21 +1509,4 @@
   &lt;/copy&gt;
 </pre></blockquote>
 
-<h3><a name="uniqfilter">UniqFilter</a></h3>
-
-<p>Suppresses all lines that match their ancestor line.  It is most
-  useful if combined with a sort filter.</p>
-
-<h4>Example:</h4>
-
-This suppresses duplicate lines.
-<blockquote><pre>
-&lt;filterreader 
classname=&quot;org.apache.tools.ant.filters.UniqFilter&quot;/&gt;
-</pre></blockquote>
-
-Convenience method:
-<blockquote><pre>
-&lt;uniqfilter/&gt;
-</pre></blockquote>
-
 </body></html>

Modified: ant/core/trunk/src/main/org/apache/tools/ant/filters/UniqFilter.java
URL: 
http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/filters/UniqFilter.java?rev=808304&r1=808303&r2=808304&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/filters/UniqFilter.java 
(original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/filters/UniqFilter.java Thu 
Aug 27 07:17:24 2009
@@ -17,53 +17,23 @@
  */
 package org.apache.tools.ant.filters;
 
-import java.io.IOException;
-import java.io.Reader;
-
 /**
- * Like the Unix uniq(1) command, only returns lines that are
- * different from their ancestor line.
+ * Like the Unix uniq(1) command, only returns tokens that are
+ * different from their ancestor token.
  *
- * <p>This filter is probably most useful if used together with a 
sortfilter.</p>
+ * <p>This filter is probably most useful if used together with a
+ * sortfilter.</p>
  *
  * @since Ant 1.8.0
  */
-public class UniqFilter extends BaseFilterReader implements ChainableReader {
+public class UniqFilter implements TokenFilter.Filter {
 
     private String lastLine = null;
-    private String currentLine = null;
 
     public UniqFilter() { }
 
-    public UniqFilter(Reader rdr) {
-        super(rdr);
-    }
-
-    public int read() throws IOException {
-        int ch = -1;
-        if (currentLine != null) {
-            ch = currentLine.charAt(0);
-            if (currentLine.length() == 1) {
-                currentLine = null;
-            } else {
-                currentLine = currentLine.substring(1);
-            }
-        } else {
-            do {
-                currentLine = readLine();
-            } while (lastLine != null && currentLine != null
-                     && lastLine.equals(currentLine));
-            lastLine = currentLine;
-            if (currentLine != null) {
-                return read();
-            }
-        }
-        return ch;
-    }
-
-    public Reader chain(final Reader rdr) {
-        UniqFilter newFilter = new UniqFilter(rdr);
-        newFilter.setInitialized(true);
-        return newFilter;
+    public String filter(String string) {
+        return lastLine == null || !lastLine.equals(string)
+            ? (lastLine = string) : null;
     }
 }

Added: ant/core/trunk/src/tests/antunit/filters/expected/unique-columns.txt
URL: 
http://svn.apache.org/viewvc/ant/core/trunk/src/tests/antunit/filters/expected/unique-columns.txt?rev=808304&view=auto
==============================================================================
--- ant/core/trunk/src/tests/antunit/filters/expected/unique-columns.txt (added)
+++ ant/core/trunk/src/tests/antunit/filters/expected/unique-columns.txt Thu 
Aug 27 07:17:24 2009
@@ -0,0 +1 @@
+A B C B

Propchange: ant/core/trunk/src/tests/antunit/filters/expected/unique-columns.txt
------------------------------------------------------------------------------
    svn:eol-style = native

Added: ant/core/trunk/src/tests/antunit/filters/input/unique-columns.txt
URL: 
http://svn.apache.org/viewvc/ant/core/trunk/src/tests/antunit/filters/input/unique-columns.txt?rev=808304&view=auto
==============================================================================
--- ant/core/trunk/src/tests/antunit/filters/input/unique-columns.txt (added)
+++ ant/core/trunk/src/tests/antunit/filters/input/unique-columns.txt Thu Aug 
27 07:17:24 2009
@@ -0,0 +1 @@
+A A B C B

Propchange: ant/core/trunk/src/tests/antunit/filters/input/unique-columns.txt
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: ant/core/trunk/src/tests/antunit/filters/uniq-test.xml
URL: 
http://svn.apache.org/viewvc/ant/core/trunk/src/tests/antunit/filters/uniq-test.xml?rev=808304&r1=808303&r2=808304&view=diff
==============================================================================
--- ant/core/trunk/src/tests/antunit/filters/uniq-test.xml (original)
+++ ant/core/trunk/src/tests/antunit/filters/uniq-test.xml Thu Aug 27 07:17:24 
2009
@@ -26,7 +26,9 @@
     <copy file="input/uniq.txt"
           tofile="${output}/uniq.txt">
       <filterchain>
-        <uniqfilter/>
+        <tokenfilter>
+          <uniqfilter/>
+        </tokenfilter>
       </filterchain>
     </copy>
     <au:assertFilesMatch
@@ -39,11 +41,29 @@
           tofile="${output}/uniq.txt">
       <filterchain>
         <sortfilter/>
-        <uniqfilter/>
+        <tokenfilter>
+          <uniqfilter/>
+        </tokenfilter>
       </filterchain>
     </copy>
     <au:assertFilesMatch
        expected="expected/sortuniq.txt"
        actual="${output}/uniq.txt"/>
   </target>
+
+  <target name="testUniqueColumns" depends="setUp">
+    <copy file="input/unique-columns.txt"
+          tofile="${output}/unique-columns.txt">
+      <filterchain>
+        <tokenfilter>
+          <stringtokenizer/>
+          <uniqfilter/>
+        </tokenfilter>
+      </filterchain>
+    </copy>
+    <au:assertFilesMatch
+       expected="expected/unique-columns.txt"
+       actual="${output}/unique-columns.txt"/>
+  </target>
+
 </project>


Reply via email to