Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 5002170
Votes 0
Synopsis File.isAncestorOf(File descendant) to determine file relationship
Category java:classes_io
Reported Against tiger-beta2
Release Fixed
State 11-Closed, duplicate of 4313887, request for enhancement
Priority: 4-Low
Related Bugs
Submit Date 24-FEB-2004
Description


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) 
======================================================================
Posted Date : 2005-08-03 19:16:16.0
Work Around
N/A
Evaluation
Could also be considered for JSR203.
  xxxxx@xxxxx   2004-03-29
JSR-203 defines the Path#startsWith and endsWith methods which should satisfy the requirement here.
Posted Date : 2009-02-16 13:43:18.0
Comments
  
  Include a link with my name & email   


PLEASE NOTE: JDK6 is formerly known as Project Mustang