Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8312123

(fs) Add Path::resolve with varargs string

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P4 P4
    • 22
    • core-libs
    • None
    • behavioral
    • low
    • Some risk of collision with methods of a class which implements java.nio.file.Path.
    • Java API
    • SE

      Summary

      Add to java.nio.file.Path two methods, resolve(Path first, Path...) and resolve(String first, String... more), for iteratively resolving a sequence of paths against a path.

      Problem

      Path does not define any variadic methods for path resolution. Such methods would be useful in resolving a sequence of paths similar to how Path.of(String first, String... more) creates a path from a sequence of strings.

      This was considered in JDK-7006126 but not implemented.

      Solution

      Add the proposed methods.

      Specification

      --- a/src/java.base/share/classes/java/nio/file/Path.java
      +++ b/src/java.base/share/classes/java/nio/file/Path.java
      @@ -516,6 +516,90 @@ default Path resolve(String other) {
               return resolve(getFileSystem().getPath(other));
           }
      
      +    /**
      +     * Resolves a path against this path, and then iteratively resolves any
      +     * additional paths.
      +     *
      +     * <p> This method resolves {@code first} against this {@code Path} as if
      +     * by calling {@link #resolve(Path)}. If {@code more} has one or more
      +     * elements then it resolves the first element against the result, then
      +     * iteratively resolves all subsequent elements. This method returns the
      +     * result from the final resolve.
      +     *
      +     * @implSpec
      +     * The default implementation is equivalent to the result obtained with:
      +     * {@snippet lang=java :
      +     *     Path result = resolve(first);
      +     *     for (Path p : more) {
      +     *         result = result.resolve(p);
      +     *     }
      +     * }
      +     *
      +     * @param   first
      +     *          the first path to resolve against this path
      +     * @param   more
      +     *          additional paths to iteratively resolve
      +     *
      +     * @return  the resulting path
      +     *
      +     * @see #resolve(Path)
      +     * @since 22
      +     */
      +    default Path resolve(Path first, Path... more) {}
      +
      +    /**
      +     * Converts a path string to a path, resolves that path against this path,
      +     * and then iteratively performs the same procedure for any additional
      +     * path strings.
      +     *
      +     * <p> This method converts {@code first} to a {@code Path} and resolves
      +     * that {@code Path} against this {@code Path} as if by calling
      +     * {@link #resolve(String)}. If {@code more} has one or more elements
      +     * then it converts the first element to a path, resolves that path against
      +     * the result, then iteratively converts and resolves all subsequent
      +     * elements. This method returns the result from the final resolve.
      +     *
      +     * @implSpec
      +     * The default implementation is equivalent to the result obtained with:
      +     * {@snippet lang=java :
      +     * Path result = resolve(first);
      +     * for (String s : more) {
      +     *     result = result.resolve(s);
      +     * }
      +     * }
      +     *
      +     * @param   first
      +     *          the first path string to convert to a path and
      +     *          resolve against this path
      +     *
      +     * @param   more
      +     *          additional path strings to be iteratively converted to
      +     *          paths and resolved
      +     *
      +     * @return  the resulting path
      +     *
      +     * @throws  InvalidPathException
      +     *          if a path string cannot be converted to a Path.
      +     *
      +     * @see #resolve(Path,Path...)
      +     * @see #resolve(String)
      +     *
      +     * @since 22
      +     */
      +    default Path resolve(String first, String... more) {}
      +

            bpb Brian Burkhalter
            webbuggrp Webbug Group
            Alan Bateman
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: