You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Adrian Herscu <bm...@fastmail.fm> on 2007/10/19 20:31:49 UTC

[io] DirectoryWalker scan order

Hi all,

I have a test that depends on the scanning order of the 
DirectoryWalker#walk method. The (or, my) problem with this method is 
that it depends on the order of the file array returned by the 
File#listFiles method which by definition does not guarantee any 
particular order...

My specific problem is that tests which pass on a Win32 OS fail on a 
*nix OS because of different scanning order :-(

I cannot see any way to make it scan directories in a specific order... 
Both walk methods cannot be overridden (one is private and the other is 
protected and final).

Any reason for this design?

Perhaps in the next release someone will add an additional walk version 
that accepts a sorter...

Adrian.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [io] DirectoryWalker scan order

Posted by Adrian Herscu <bm...@fastmail.fm>.

Niall Pemberton wrote:
> On 10/19/07, Adrian Herscu <bm...@fastmail.fm> wrote:
>> Hi all,
>>
>> I have a test that depends on the scanning order of the
>> DirectoryWalker#walk method. The (or, my) problem with this method is
>> that it depends on the order of the file array returned by the
>> File#listFiles method which by definition does not guarantee any
>> particular order...
>>
>> My specific problem is that tests which pass on a Win32 OS fail on a
>> *nix OS because of different scanning order :-(
>>
>> I cannot see any way to make it scan directories in a specific order...
>> Both walk methods cannot be overridden (one is private and the other is
>> protected and final).
>>
>> Any reason for this design?
> 
> The private walk method is the guts of the DirectoryWalker - if it
> could be overriden then it could be done in one of 2 ways - either 1)
> just providing different logic (i.e. not calling super.walk()) and in
> this case then its better to just do this in completely different
> method (e.g. run() ) - or 2) doing something and calling super.walk()
> - in this instance there is already a handleDirectoryStart() method
> that could be used to achieve the same thing.

Agree.

> 
>> Perhaps in the next release someone will add an additional walk version
>> that accepts a sorter...
> 
> Guess you mean java.util.Comparator? We could do that, although it may
> be confusing since someone may expect all files/directories to be
> processed in that order - rather than just the contents of individual
> directories to be sorted.

Yes. The java.util.Comparator seems to fit the purpose.
After looking at the code again, I think that the comparator should be 
regarded as an invariant -- that is, once the user constructs a 
directory walker, the directories and their files should be "walked" in 
the same order. Hence, the comparator should be passed during 
construction and stored in some private field as depthLimit and filter 
are stored.

Then, after line 357, the comparator should be used to sort the 
childFiles of the currently processed directory.

It will be nice to have another constructor which creates that 
comparator given an enumeration like ASCENDING=1, DESCENDING=-1, UNSORTED=0.

> 
> The best thing to do if you want this is to file an enhancement
> request using Jira:
> 
> http://commons.apache.org/io/issue-tracking.html
> 
> Niall
> 
>> Adrian.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [io] DirectoryWalker scan order

Posted by Niall Pemberton <ni...@gmail.com>.
On 10/19/07, Adrian Herscu <bm...@fastmail.fm> wrote:
> Hi all,
>
> I have a test that depends on the scanning order of the
> DirectoryWalker#walk method. The (or, my) problem with this method is
> that it depends on the order of the file array returned by the
> File#listFiles method which by definition does not guarantee any
> particular order...
>
> My specific problem is that tests which pass on a Win32 OS fail on a
> *nix OS because of different scanning order :-(
>
> I cannot see any way to make it scan directories in a specific order...
> Both walk methods cannot be overridden (one is private and the other is
> protected and final).
>
> Any reason for this design?

The private walk method is the guts of the DirectoryWalker - if it
could be overriden then it could be done in one of 2 ways - either 1)
just providing different logic (i.e. not calling super.walk()) and in
this case then its better to just do this in completely different
method (e.g. run() ) - or 2) doing something and calling super.walk()
- in this instance there is already a handleDirectoryStart() method
that could be used to achieve the same thing.

> Perhaps in the next release someone will add an additional walk version
> that accepts a sorter...

Guess you mean java.util.Comparator? We could do that, although it may
be confusing since someone may expect all files/directories to be
processed in that order - rather than just the contents of individual
directories to be sorted.

The best thing to do if you want this is to file an enhancement
request using Jira:

http://commons.apache.org/io/issue-tracking.html

Niall

> Adrian.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org