Path.resolve as a static method ?
Alan Bateman
Alan.Bateman at oracle.com
Fri Dec 10 09:31:05 PST 2010
Emmanuel Castro wrote:
> I am often tempted to use the following pattern :
> Path path=Paths.get("dir1/a")
> path.getParent().resolve("b")
> It returns dir1/a
> A problem occurs when path=Paths.get("a")
> I await "b", but I obviously get an happy NullPointerException
>
> I have to encapsulate my call to resolve() into a static method:
> static Path safeResolve(Path mainPath, Path other) {
> if (mainPath == null) return other;
> else mainPath.resolve(other);
> }
>
> and a variant for safeResolve(Path mainPath, String other).
>
> I think that this method should be standard, maybe in the Paths class.
>
> Your advice?
>
> Emmanuel CASTRO
>
We need a resolveSibling (or better named) method as it is very common
to need to resolve against the parent. In the mean-time, a possible
workaround is path.resolve("../b").normalize().
-Alan.
More information about the nio-discuss
mailing list