diff --git a/WHATSNEW b/WHATSNEW index 387733d09..1e796e510 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -927,6 +927,9 @@ Other changes: added. Bugzilla Report 40504. + * A new filterreader that suppresses lines that match + their ancestor line 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 6e2ef1e0b..d95e7ac97 100644 --- a/docs/manual/CoreTypes/filterchain.html +++ b/docs/manual/CoreTypes/filterchain.html @@ -125,6 +125,7 @@ nested elements.
TokenFilter
FixCRLF
SortFilter
+UniqFilter

FilterReader

@@ -1494,97 +1495,21 @@ This may be used as follows: </copy> -

SortFilter

+

UniqFilter

-

This filter sorts lines lexicographically but you can specifiy a - custom comparator as well.

+

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

- - - - - - - - - - - - - - - - -
Parameter TypeParameter ValueRequired
reverseWhether to reverse the sort order - (boolean). Will be ignored if a custom comparator has been - specified.No
comparatorClassname of a class - implementing java.util.Comparator an instance of - which will be used when comparing lines.No
-

Example:

-This will sort the lines. -
-<filterreader classname="org.apache.tools.ant.filters.SortFilter"/>
-
- -Convenience method: -
-<sortfilter/>
-
- -This will reverse the sort order. - +This suppresses duplicate lines.
-<filterreader classname="org.apache.tools.ant.filters.SortFilter">
-  <param name="reverse" value="true"/>
-</filterreader>
+<filterreader classname="org.apache.tools.ant.filters.UniqFilter"/>
 
Convenience method:
-<sortfilter reverse="true"/>
-
- -You can use your own comparator, the easiest way is by using typedef -and the convenience method which allows to specify the comparator as a -nested element: -
-public final class EvenFirstCmp implements Comparator {
-
-    public int compare(Object o1, Object o2) {
-        String s1 = ((String) o1).substring(5).trim();
-        String s2 = ((String) o2).substring(5).trim();
-        int n1 = Integer.parseInt(s1);
-        int n2 = Integer.parseInt(s2);
-        if (n1 % 2 == 0) {
-            if (n2 % 2 == 0) {
-                return n1 - n2;
-            } else {
-                return -1;
-            }
-        } else {
-            if (n2 % 2 == 0) {
-                return 1;
-            } else {
-                return n1 - n2;
-            }
-        }
-    }
-}
-
- -and used as - -
-<typedef classname="org.apache.tools.ant.filters.EvenFirstCmp"
-         name="evenfirst"/>
-...
-  <filterchain>
-    <sortfilter>
-      <evenfirst/>
-    </sortfilter>
-  </filterchain>
+<uniqfilter/>
 
diff --git a/src/main/org/apache/tools/ant/antlib.xml b/src/main/org/apache/tools/ant/antlib.xml index a3cdac4d9..b11bac527 100644 --- a/src/main/org/apache/tools/ant/antlib.xml +++ b/src/main/org/apache/tools/ant/antlib.xml @@ -138,5 +138,7 @@ + diff --git a/src/main/org/apache/tools/ant/filters/UniqFilter.java b/src/main/org/apache/tools/ant/filters/UniqFilter.java new file mode 100644 index 000000000..8975899ef --- /dev/null +++ b/src/main/org/apache/tools/ant/filters/UniqFilter.java @@ -0,0 +1,69 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +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. + * + *

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

+ * + * @since Ant 1.8.0 + */ +public class UniqFilter extends BaseFilterReader implements ChainableReader { + + 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; + } +} diff --git a/src/tests/antunit/filters/expected/sortuniq.txt b/src/tests/antunit/filters/expected/sortuniq.txt new file mode 100644 index 000000000..7dc51accc --- /dev/null +++ b/src/tests/antunit/filters/expected/sortuniq.txt @@ -0,0 +1,4 @@ +A +AA +B +C diff --git a/src/tests/antunit/filters/expected/uniq.txt b/src/tests/antunit/filters/expected/uniq.txt new file mode 100644 index 000000000..e9d0f3db2 --- /dev/null +++ b/src/tests/antunit/filters/expected/uniq.txt @@ -0,0 +1,5 @@ +A +AA +B +C +B diff --git a/src/tests/antunit/filters/input/uniq.txt b/src/tests/antunit/filters/input/uniq.txt new file mode 100644 index 000000000..9baebe55a --- /dev/null +++ b/src/tests/antunit/filters/input/uniq.txt @@ -0,0 +1,6 @@ +A +AA +AA +B +C +B diff --git a/src/tests/antunit/filters/uniq-test.xml b/src/tests/antunit/filters/uniq-test.xml new file mode 100644 index 000000000..dd470b449 --- /dev/null +++ b/src/tests/antunit/filters/uniq-test.xml @@ -0,0 +1,49 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + +