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

add default method implementations to java.nio.file.Path

XMLWordPrintable

    • generic
    • generic

      A DESCRIPTION OF THE REQUEST :
      When implementing the Path interface many methods need to be implemented that are same for all Path implementations.

      The following methods can be implemented as a default method relying on the other non-default methods in Path:
          public Path getFileName()
          public Path getParent()
          public boolean startsWith(Path other)
          public boolean startsWith(String other)
          public boolean endsWith(Path other)
          public boolean endsWith(String other)
          public Path resolve(String other)
          public Path resolveSibling(String other)
          public boolean equals(Object obj)
          public Path resolveSibling(Path other)
          public Iterator<Path> iterator()
          public Path normalize()
          public WatchKey register(WatchService watcher, WatchEvent.Kind<?>... events) throws IOException


      JUSTIFICATION :
      Implementing java.nio.file.Path takes unnecessarily long time.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The methods implementented as the code in the 'Workaround' section of this bug report.

      CUSTOMER SUBMITTED WORKAROUND :
          @Override
          public Path getFileName() {
              return getName(getNameCount() - 1);
          }

          @Override
          public Path getParent() {
              return getName(getNameCount() - 2);
          }

          @Override
          public boolean startsWith(Path other) {
              if (other.getNameCount() > getNameCount()) {
                  return false;
              }

              return subpath(0, other.getNameCount()).equals(other);
          }

          @Override
          public boolean startsWith(String other) {
              return startsWith(getFileSystem().getPath(other));
          }

          @Override
          public boolean endsWith(Path other) {
              if (other.getNameCount() > getNameCount()) {
                  return false;
              }

              return subpath(getNameCount() - other.getNameCount(),
                      getNameCount()).equals(other);
          }

          @Override
          public boolean endsWith(String other) {
              return startsWith(getFileSystem().getPath(other));
          }

          @Override
          public Path resolve(String other) {
              return resolve(getFileSystem().getPath(other));
          }

          @Override
          public Path resolveSibling(String other) {
              return resolveSibling(getFileSystem().getPath(other));
          }

          @Override
          public boolean equals(Object obj) {
              if (obj == null) {
                  return false;
              }
              if (obj.getClass() != getClass()) {
                  return false;
              }
              return compareTo((Path) obj) == 0;
          }

          @Override
          public Path resolveSibling(Path other) {
              if (getParent() == null) {
                  return other.toAbsolutePath();
              }
              return getParent().resolve(other);
          }

          @Override
          public Iterator<Path> iterator() {
              return IntStream.range(0, getNameCount()).
                      mapToObj(this::getName).
                      iterator();
          }

          @Override
          public Path normalize() {
              Path result = getName(0);
              for (int i = 1; i < getNameCount(); i++) {
                  Path name = getName(i);
                  switch (name.toString()) {
                      case ".":
                          continue;
                      case "..":
                          result = result.getParent();
                      default:
                          result = result.resolve(name);
                  }
              }
              return result;
          }

          @Override
          public WatchKey register(WatchService watcher, WatchEvent.Kind<?>... events) throws IOException {
              return register(watcher, events, new WatchEvent.Modifier[0]);
          }

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: