public final class SortFilter extends BaseParamFilterReader implements ChainableReader
Sort a file before and/or after the file.
Examples:
<copy todir="build"> <fileset dir="input" includes="*.txt"/> <filterchain> <sortfilter/> </filterchain> </copy>
Sort all files *.txt
from src location and copy
them into build location. The lines of each file are sorted
in ascendant order comparing the lines via the
String.compareTo(Object o)
method.
<copy todir="build"> <fileset dir="input" includes="*.txt"/> <filterchain> <sortfilter reverse="true"/> </filterchain> </copy>
Sort all files *.txt
from src location into reverse
order and copy them into build location. If reverse parameter has
value true
(default value), then the output line of the files
will be in ascendant order.
<copy todir="build"> <fileset dir="input" includes="*.txt"/> <filterchain> <filterreader classname="org.apache.tools.ant.filters.SortFilter"> <param name="comparator" value="org.apache.tools.ant.filters.EvenFirstCmp"/> </filterreader> </filterchain> </copy>
Sort all files *.txt
from src location using as
sorting criterium EvenFirstCmp
class, that sorts the file
lines putting even lines first then odd lines for example. The modified files
are copied into build location. The EventFirstCmp
,
has to an instanciable class via Class.newInstance()
,
therefore in case of inner class has to be static. It also has to
implement java.util.Comparator
interface, for example:
package org.apache.tools.ant.filters; ...(omitted) public final class EvenFirstCmp implements <b>Comparator</b> { public int compare(Object o1, Object o2) { ...(omitted) } }
The example above is equivalent to:
<componentdef name="evenfirst" classname="org.apache.tools.ant.filters.EvenFirstCmp"/> <copy todir="build"> <fileset dir="input" includes="*.txt"/> <filterchain> <sortfilter> <evenfirst/> </sortfilter> </filterchain> </copy>
If parameter comparator
is present, then
reverse
parameter will not be taken into account.
Constructor and Description |
---|
SortFilter()
Constructor for "dummy" instances.
|
SortFilter(java.io.Reader in)
Creates a new filtered reader.
|
Modifier and Type | Method and Description |
---|---|
void |
add(java.util.Comparator comparator)
Set the comparator to be used as sorting criterium as nested element.
|
java.io.Reader |
chain(java.io.Reader rdr)
Creates a new SortReader using the passed in Reader for instantiation.
|
java.util.Comparator |
getComparator()
Returns the comparator to be used for sorting.
|
boolean |
isReverse()
Returns
true if the sorting process will be in reverse
order, otherwise the sorting process will be in ascendant order. |
int |
read()
Returns the next character in the filtered stream.
|
void |
setComparator(java.util.Comparator comparator)
Set the comparator to be used as sorting criterium.
|
void |
setReverse(boolean reverse)
Sets the sorting process will be in ascendant (
reverse=false )
or to descendant (reverse=true ). |
getParameters, setParameters
getInitialized, getProject, read, readFully, readLine, setInitialized, setProject, skip
public SortFilter()
BaseFilterReader.BaseFilterReader()
public SortFilter(java.io.Reader in)
in
- A Reader object providing the underlying stream. Must not be
null
.public int read() throws java.io.IOException
read
in class java.io.FilterReader
java.io.IOException
- if the underlying stream throws an IOException during
readingpublic java.io.Reader chain(java.io.Reader rdr)
chain
in interface ChainableReader
rdr
- A Reader object providing the underlying stream. Must not be
null
.public boolean isReverse()
true
if the sorting process will be in reverse
order, otherwise the sorting process will be in ascendant order.true
if the sorting process will be in reverse
order, otherwise the sorting process will be in ascendant order.public void setReverse(boolean reverse)
reverse=false
)
or to descendant (reverse=true
).reverse
- Boolean representing reverse ordering process.public java.util.Comparator getComparator()
public void setComparator(java.util.Comparator comparator)
comparator
- the comparator to setpublic void add(java.util.Comparator comparator)
comparator
- the comparator to set