From 6aa494cb3ead80424245567eb4d3eaf71bbfb5f1 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Thu, 27 Aug 2009 07:17:24 +0000 Subject: [PATCH] UniqFilter is not only simpler if implemented as a TokenFilter, it is even more useful as well git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@808304 13f79535-47bb-0310-9956-ffa450edef68 --- WHATSNEW | 4 +- docs/manual/CoreTypes/filterchain.html | 33 ++++++------- .../apache/tools/ant/filters/UniqFilter.java | 46 ++++--------------- .../filters/expected/unique-columns.txt | 1 + .../antunit/filters/input/unique-columns.txt | 1 + src/tests/antunit/filters/uniq-test.xml | 24 +++++++++- 6 files changed, 49 insertions(+), 60 deletions(-) create mode 100644 src/tests/antunit/filters/expected/unique-columns.txt create mode 100644 src/tests/antunit/filters/input/unique-columns.txt diff --git a/WHATSNEW b/WHATSNEW index 1e796e510..0214e82dd 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -927,8 +927,8 @@ Other changes: added. Bugzilla Report 40504. - * A new filterreader that suppresses lines that match - their ancestor line has been added. + * A new token filter that suppresses tokens that match + their ancestor token has been added. Changes from Ant 1.7.0 TO Ant 1.7.1 ============================================= diff --git a/docs/manual/CoreTypes/filterchain.html b/docs/manual/CoreTypes/filterchain.html index dfde25dcd..df9cf27d8 100644 --- a/docs/manual/CoreTypes/filterchain.html +++ b/docs/manual/CoreTypes/filterchain.html @@ -125,7 +125,6 @@ nested elements.
TokenFilter
FixCRLF
SortFilter
-UniqFilter

FilterReader

@@ -1032,6 +1031,7 @@ and \\. Trim
IgnoreBlank
DeleteCharacters
+ UniqFilter

The following string filters are provided by the optional distribution. @@ -1360,6 +1360,20 @@ Delete tabs from lines, trim the lines and removes empty lines. +

UniqFilter

+ +

Suppresses all tokens that match their ancestor token. It is most + useful if combined with a sort filter.

+ +

Example:

+ +This suppresses duplicate lines. +
+<tokenfilter>
+  <uniqfilter/>
+</tokenfilter>
+
+

ScriptFilter

This is an optional filter that executes a script in a Apache BSF @@ -1495,21 +1509,4 @@ This may be used as follows: </copy> -

UniqFilter

- -

Suppresses all lines that match their ancestor line. It is most - useful if combined with a sort filter.

- -

Example:

- -This suppresses duplicate lines. -
-<filterreader classname="org.apache.tools.ant.filters.UniqFilter"/>
-
- -Convenience method: -
-<uniqfilter/>
-
- diff --git a/src/main/org/apache/tools/ant/filters/UniqFilter.java b/src/main/org/apache/tools/ant/filters/UniqFilter.java index 8975899ef..a7d2334e4 100644 --- a/src/main/org/apache/tools/ant/filters/UniqFilter.java +++ b/src/main/org/apache/tools/ant/filters/UniqFilter.java @@ -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. * - *

This filter is probably most useful if used together with a sortfilter.

+ *

This filter is probably most useful if used together with a + * sortfilter.

* * @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; } } diff --git a/src/tests/antunit/filters/expected/unique-columns.txt b/src/tests/antunit/filters/expected/unique-columns.txt new file mode 100644 index 000000000..81f684e7a --- /dev/null +++ b/src/tests/antunit/filters/expected/unique-columns.txt @@ -0,0 +1 @@ +A B C B diff --git a/src/tests/antunit/filters/input/unique-columns.txt b/src/tests/antunit/filters/input/unique-columns.txt new file mode 100644 index 000000000..ef7972597 --- /dev/null +++ b/src/tests/antunit/filters/input/unique-columns.txt @@ -0,0 +1 @@ +A A B C B diff --git a/src/tests/antunit/filters/uniq-test.xml b/src/tests/antunit/filters/uniq-test.xml index dd470b449..0957b9ebe 100644 --- a/src/tests/antunit/filters/uniq-test.xml +++ b/src/tests/antunit/filters/uniq-test.xml @@ -26,7 +26,9 @@ - + + + - + + + + + + + + + + + + + + + +