Trouble understanding the meaning of the API

Alan Bateman Alan.Bateman at Sun.COM
Fri Feb 5 01:26:07 PST 2010


Marcel Ammerlaan wrote:
> Thanks for the answer. What was looking for is the best way to present 
> a Path to a user. For example a file-browser would need to show the 
> individual
> parts of the path in a human-readable fashion. This could be done 
> using the toString() on each part of the Path (something like:
> for(int i=0; i<p.getNameCount(); i++) {
>    javax.swing.tree.MutableTreeNode node = new 
> javax.swing.tree.MutableTreeNode(p.getName(i).toString());
>    node.setParent(lastNode);
>    lastNode = node;
> })
>
> But this depends heavily on the toString() method of the provider to 
> actually provide a human readable name. If this is the prefered way to 
> display the Path to the user, than clarifying this in the Javadoc 
> might be usefull information for providers.
The toString method is the method to obtain the String representation. 
I'll see if we can make this clearer.

BTW: Path implements Iterable<Path> so the loop above can be replaced with:

for (Path name: p) {
    MutableTreeNode node = new MutableTreeNode(name.toString());
    :
}

>
> An even stronger requirement on toString() would be that 
> FileSystem.getPath("/bin/ls") equals 
> FileSystem.getPath(FileSystem.getPath("/bin/ls").toString())
> to make sure the the human-readable equivalent of the path can 
> actually be related to the technical path that's used by the filesystem.
It's not possible to provide this guarantee. The getPath method might 
remove redundant separators (for example) so 
getPath("////////bin/////ls").toString() returns "/bin/ls". The toString 
method has a statement to explain that it might differ. There are other 
more complications reasons that arise when the file system 
representation isn't Unicode.

-Alan.


More information about the nio-discuss mailing list