Peter Reilly wrote:
On 1/4/07, Steve Loughran <[EMAIL PROTECTED]> wrote:
[EMAIL PROTECTED] wrote:
>> 2)
>> The java api of some types in ant were not really designed for
>> ease of use.
>
> It's much easier to use <path> instead of <fileset>, e.g.
>
> Compare the execute()-implementation between [1] and [2].
>
>
ahh, but as of ant 1.7 everything is a resource, which is something you
can iterate over. If you store everthing as resouce collections, you
just get an iterator() and let the tasks sort the details out for
themselves:
http://antbook.cvs.sourceforge.net/antbook/examples/sections/extending/ch17_tasks/filesize/first/src/org/antbook/tasks/filesize/ResourceSizeTask.java?view=markup
the best bit, filesets have an iterator() method too, and
class DirSet extends AbstractFileSet implements ResourceCollection
so if you get a dirset, you can call iterator() and get it to do all the
heavy lifting.
Summary: use the resource collection APIs. If there is one limitation,
it is that even though the ResourceCollection Types implement
iterator(), they cannot implement the java.lang.Iterable interface to
say "use us in foreach()", because of course that is java5+ only. This
I think that if we say that ant needs to be compiled with java5, we
can make use of the Iterable interface, while still allowing ant
to be used in java1.3 and java1.4 env.
This would make script intergration much nicer.
I tend to make my iterators Iterable, with an extension class that
returns itself.
public class IterableIterator<T> implements Iterator<T>,Iterable<T> {
private Iterator<T> it;
public IterableIterator<T>(Iterator<T> it) {
this.it=it;
}
public Iterator<T> iterable() {
return this;
}
//plus normal iterator methods
}
This can be used to retrofit iteration, but it also lets me have classes
that return multiple iterators, and use arguments to get the iterator,
which I do in my Xom extended XML classes:
for(Element e:child.elements("[EMAIL PROTECTED]")) {
}
We could have some stuff in a 1.5+ package that would let scripts do
this, even if we didnt retrofit iterable to a resource.
-Steve
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]