The javadoc for .isAbsolute()
says:
Tells whether or not this path is absolute.
An absolute path is complete in that it doesn't need to be combined with other path information in order to locate a file.Returns:
true
if, and only if, this path is absolute
The javadoc for .getRoot()
says:
Returns the root component of this path as a Path object, or null if this path does not have a root component.
Returns: a path representing the root component of this path, or
null
OK, so, I am at a loss here; are there any filesystems out there for which a path may be absolute without a root at all?
EDIT: note that there CAN be paths which have a root but are NOT absolute. For instance, these on Windows systems:
C:foo
;\foo\bar
.
But I am asking for the reverse here: no root and absolute.
The Definition
The interface states the following about roots:
So as you see, the comment seems to imply that roots are used for file system hierarchies. Now we have to reason about what an absolute path is. The interface tells us the following:
So, as you see, there is no word about roots in the definition about absolute paths. The only restriction is that we have to be able to locate the file without further information.
Hierarchical File Systems
Most file system are hierarchical, i.e., they are trees (or graphs if we consider links) or forests. The root in a tree is a node that is not the child of another node (excluding links). Windows file systems are, for example, forests, as they have many roots (
C:
,D:
,...). Linux has usually only one root which is/
. Roots are very important as without them it would be hard to start locating a file. In such file systems, you can usually rely on each absolute path having a root.Non-Hierarchical File Systems
As long as we have a hierarchical file system, we can anticipate a root in an absolute path, but what if we don't have one? Then, an absolute path might not contain a root.
An example that comes to my mind: Distributed file systems like Chord. These are often not hierarchical so the meaning of roots is usually undefined. Instead, a file hash identifies a file (SHA-1 in Chord). So a valid Chord path might look like this:
This is an absolute path. One can retrieve the associated file without further information, so the path is absolute. However, I see no root. We could define the whole hash to be its own root (then each file would be its own root), but nobody can guarantee that every person that implements a Chord file system will agree to this. So there might be reasonable implementations that do not treat these hashes as roots. In such a file system, each path would be absolute, but none would contain a root.
If I would implement a non-hierarchical file system, I would always return
null
as root, as IMHO a root is not a defined concept in a non-hierarchical file system. Since I think like this, other devs might think so as well. Consequently, you may not assume that every absolute path has a root.Note that distributed file systems are quite common in many areas, so this is not merely a corner case that will never be implemented. I think you have to anticipate it.
Conclusion
So there will be people implementing file systems without roots; you should anticipate this.