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

File.isAncestorOf(File descendant) to determine file relationship

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 5.0
    • core-libs
    • x86
    • windows_xp

      Name: rmT116609 Date: 02/24/2004


      A DESCRIPTION OF THE REQUEST :
      It would be nice to determine if a java.io.File is an ancestor/descendant of another java.io.File. You will also need it as you get more into the file system while building the Java OS and Java Desktop (UI).

      JUSTIFICATION :
      Instead of the same code being reproduced by everyone that needs it, it can exist once and simply be referenced via java.io.File.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      boolean isAncestorOf(java.io.File descendant) returns true if descendant is a descendant of ancestor (this), false otherwise. isDescendantOf(java.io.File ancestor) returns true if ancestor is an ancestor of descendant (this), false otherwise. If ancestor.equals(descendant) initially, false should be returned.
      ACTUAL -
      See Expected Behavior.

      ---------- BEGIN SOURCE ----------
      // provided for example purposes
      public class File extends java.io.File {

          // this method is provided for example purposes
          public File(String s) {
              super(s);
          }

          // **** (RFE) incorporate this method into java.io.File
          public boolean isAncestorOf(java.io.File descendant) {
          // or public boolean isParentOf(java.io.File child) {
              if (equals(descendant)) {
                  return false;
              }
              while ((descendant != null) && !equals(descendant)) {
                  descendant = descendant.getParentFile();
              }
              return descendant != null;
          }

          // **** (RFE) incorporate this method into java.io.File
          public boolean isDescendantOf(java.io.File ancestor) {
          // or public boolean isChildOf(java.io.File parent) {
              // the cast will be eliminated when incorporporated into java.io.File
              return ((File)ancestor).isAncestorOf(this);
              // return should read:
              //return ancestor.isAncestorOf(this);
          }

          // this method is provided for example and test purposes
          public static void main(String[] args) {
              File ancestor = new File(args[0]);
              File descendant = new File(args[1]);
              System.out.println(
                  ancestor
                  + " is ancestor of "
                  + descendant
                  + ": "
                  + ancestor.isAncestorOf(descendant));
              System.out.println(
                  descendant
                  + " is descendant of "
                  + ancestor
                  + ": "
                  + descendant.isDescendantOf(ancestor));
          }

      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
          public boolean isParentOf(
              java.io.File ancestor,
              java.io.File descendant
          ) {
              if (ancestor != null) {
                  if (ancestor.equals(descendant)) {
                      return false;
                  }
                  while ((descendant != null) && !ancestor.equals(descendant)) {
                      descendant = descendant.getParentFile();
                  }
              }
              return (ancestor != null) && (descendant != null);
          }
      (Incident Review ID: 240160)
      ======================================================================

            alanb Alan Bateman
            rmandalasunw Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: