Trouble understanding the meaning of the API
Marcel Ammerlaan
marcel-snoracle-nio at dyskolus.com
Thu Feb 4 13:03:12 PST 2010
Alan Bateman wrote:
> Marcel Ammerlaan wrote:
>> I'm working with the Path API, currently implementing an HTTP
>> filesystem. I have some troubles
>> understanding what the intention of the API is regarding Paths:
>>
>> If I create a Path like this: Path p = Paths.get("/usr/bin/xset") and
>> I do a getName() on it
>> I get a Path again.
> That is correct as the method is specified to return the file name
> represented as a Path. For example, you might do
> dir.resolve(foo.getName()) to resolve the file name against another
> path. There maybe be a case for a getNameAsString or renaming getName
> but in general it's important to remember that Path != String and best
> to avoid going back and forth between Strings and Paths where possible.
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.
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.
The current wording of toString() makes it entirely possible to
translate the String to Chinese before returning it which would of
course not help the user when presenting him with the GUI when each node
is translated into Chinese ;-)
Regards,
Marcel Ammerlaan.
More information about the nio-discuss
mailing list