Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Path.resolve(other) java.nio.file.Path method

Tags:

java

path

nio

Directly from this API:

resolve

Path resolve(Path other)

Resolve the given path against this path.

If the other parameter is an absolute path then this method trivially returns other. If other is an empty path then this method trivially returns this path. Otherwise this method considers this path to be a directory and resolves the given path against this path. In the simplest case, the given path does not have a root component, in which case this method joins the given path to this path and returns a resulting path that ends with the given path. Where the given path has a root component then resolution is highly implementation dependent and therefore unspecified.

(emphasis mine)

There is a little of a contradiction here, first they say:

  1. If the other parameter is an absolute path then this method trivially returns other.

    and then they say:

  2. Where the given path has a root component then resolution is highly implementation dependent and therefore unspecified.

Does not an absolute path have to include a root component in order to be such? Thanks in advance.

like image 430
Rollerball Avatar asked Oct 22 '22 01:10

Rollerball


1 Answers

The short answer to your question is no, an absolute path does not need to have a root component, however, depending on the provider, it might.

If we look at the source code for UnixPath, we see that, indeed, if it is an absolute path, then it will return a root component, and it will only return a root component if it is an absolute path.

However, there is no requirement that it be implemented in this way. It is, theoretically at least, possible for getRoot() to return something, and for isAbsolute() to return false. In this then the results are undefined. Or, to put it in a truth map form:

Result of resolve() when:
                     getRoot()==null  getRoot()!=null
isAbsolute()==true   defined          defined
isAbsolute()==false  defined          undefined
like image 189
Paul Wagland Avatar answered Nov 03 '22 02:11

Paul Wagland