Equality of attributes of two files

Alan Bateman Alan.Bateman at Sun.COM
Sun Jul 20 07:09:03 PDT 2008


Rajendra Gutupalli wrote:
> Hi Alan,
>
> I am looking for a method in BasicFileAttributes such as 'equals() or 
> compareTo' which compares basic file attributes of two files.
>
> It would be helpful code such as below works:
>
> BasicFileAttributes ba1 = 
> Attributes.readBasicFileAttributes(file1,false);
> BasicFileAttributes ba2 = 
> Attributes.readBasicFileAttributes(file2,false);
> if (ba1.equals(ba2)){
> S.O.P("Basic File Attributes of two files are same");
> }
>
> As of now BasicFileAttribute implementation does not seem to override 
> equals method.
> If the implementation has equals or compareTo methods then it is handy 
> for users to compare file attributes of two files without comparing 
> each attribute (like ba1.isDirectory()==ba2.isDirectory() etc.).
>
I don't think it would be possible to implement Comparable here as basic 
file attributes don't really have a natural ordering (as a group that 
is; many of the specific attributes do have a natural ordering).  I'm 
not sure that it would be feasible to override equals/hashCode here 
either. The reason is that there are a number of equivalence relations 
that must be implemented and I don't think it would be possible to 
implement the symmetry and transitivity requirements. For example,  
PosixFileAttributes extends BasicFileAttributes and I don't think they 
could agree whether they are equal. (There are more knowledgeable people 
on such matters on this mailing lists that may wish to jump in if I've 
got this wrong).

In any case, I'm interested to understand the motive for the question. 
Is this part of a test that checks if the attributes of two files are 
the same or would you anticipate storing BasicFileAttributes as elements 
of a collection? If the former, then 
test/java/nio/file/Path/CopyAndMove.java defines a number of 
check*Attributes methods that may be useful.

-Alan.



More information about the nio-dev mailing list