JDK-5002170 : File.isAncestorOf(File descendant) to determine file relationship
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2004-02-24
  • Updated: 2009-02-16
  • Resolved: 2009-02-16
Related Reports
Duplicate :  
Description
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) 
======================================================================

Comments
EVALUATION JSR-203 defines the Path#startsWith and endsWith methods which should satisfy the requirement here.
16-02-2009

EVALUATION Could also be considered for JSR203. ###@###.### 2004-03-29
29-03-2004