< prev index next >

src/java.base/share/classes/java/io/File.java

Print this page
rev 52881 : 8214971: Replace use of string.equals("") with isEmpty()
Reviewed-by: jlaskey, prappo, lancea, dfuchs, redestad


 240     public static final String pathSeparator = "" + pathSeparatorChar;
 241 
 242 
 243     /* -- Constructors -- */
 244 
 245     /**
 246      * Internal constructor for already-normalized pathname strings.
 247      */
 248     private File(String pathname, int prefixLength) {
 249         this.path = pathname;
 250         this.prefixLength = prefixLength;
 251     }
 252 
 253     /**
 254      * Internal constructor for already-normalized pathname strings.
 255      * The parameter order is used to disambiguate this method from the
 256      * public(File, String) constructor.
 257      */
 258     private File(String child, File parent) {
 259         assert parent.path != null;
 260         assert (!parent.path.equals(""));
 261         this.path = fs.resolve(parent.path, child);
 262         this.prefixLength = parent.prefixLength;
 263     }
 264 
 265     /**
 266      * Creates a new <code>File</code> instance by converting the given
 267      * pathname string into an abstract pathname.  If the given string is
 268      * the empty string, then the result is the empty abstract pathname.
 269      *
 270      * @param   pathname  A pathname string
 271      * @throws  NullPointerException
 272      *          If the <code>pathname</code> argument is <code>null</code>
 273      */
 274     public File(String pathname) {
 275         if (pathname == null) {
 276             throw new NullPointerException();
 277         }
 278         this.path = fs.normalize(pathname);
 279         this.prefixLength = fs.prefixLength(this.path);
 280     }


 299      * a directory, and the <code>child</code> pathname string is taken to
 300      * denote either a directory or a file.  If the <code>child</code> pathname
 301      * string is absolute then it is converted into a relative pathname in a
 302      * system-dependent way.  If <code>parent</code> is the empty string then
 303      * the new <code>File</code> instance is created by converting
 304      * <code>child</code> into an abstract pathname and resolving the result
 305      * against a system-dependent default directory.  Otherwise each pathname
 306      * string is converted into an abstract pathname and the child abstract
 307      * pathname is resolved against the parent.
 308      *
 309      * @param   parent  The parent pathname string
 310      * @param   child   The child pathname string
 311      * @throws  NullPointerException
 312      *          If <code>child</code> is <code>null</code>
 313      */
 314     public File(String parent, String child) {
 315         if (child == null) {
 316             throw new NullPointerException();
 317         }
 318         if (parent != null) {
 319             if (parent.equals("")) {
 320                 this.path = fs.resolve(fs.getDefaultParent(),
 321                                        fs.normalize(child));
 322             } else {
 323                 this.path = fs.resolve(fs.normalize(parent),
 324                                        fs.normalize(child));
 325             }
 326         } else {
 327             this.path = fs.normalize(child);
 328         }
 329         this.prefixLength = fs.prefixLength(this.path);
 330     }
 331 
 332     /**
 333      * Creates a new <code>File</code> instance from a parent abstract
 334      * pathname and a child pathname string.
 335      *
 336      * <p> If <code>parent</code> is <code>null</code> then the new
 337      * <code>File</code> instance is created as if by invoking the
 338      * single-argument <code>File</code> constructor on the given
 339      * <code>child</code> pathname string.


 342      * denote a directory, and the <code>child</code> pathname string is taken
 343      * to denote either a directory or a file.  If the <code>child</code>
 344      * pathname string is absolute then it is converted into a relative
 345      * pathname in a system-dependent way.  If <code>parent</code> is the empty
 346      * abstract pathname then the new <code>File</code> instance is created by
 347      * converting <code>child</code> into an abstract pathname and resolving
 348      * the result against a system-dependent default directory.  Otherwise each
 349      * pathname string is converted into an abstract pathname and the child
 350      * abstract pathname is resolved against the parent.
 351      *
 352      * @param   parent  The parent abstract pathname
 353      * @param   child   The child pathname string
 354      * @throws  NullPointerException
 355      *          If <code>child</code> is <code>null</code>
 356      */
 357     public File(File parent, String child) {
 358         if (child == null) {
 359             throw new NullPointerException();
 360         }
 361         if (parent != null) {
 362             if (parent.path.equals("")) {
 363                 this.path = fs.resolve(fs.getDefaultParent(),
 364                                        fs.normalize(child));
 365             } else {
 366                 this.path = fs.resolve(parent.path,
 367                                        fs.normalize(child));
 368             }
 369         } else {
 370             this.path = fs.normalize(child);
 371         }
 372         this.prefixLength = fs.prefixLength(this.path);
 373     }
 374 
 375     /**
 376      * Creates a new {@code File} instance by converting the given
 377      * {@code file:} URI into an abstract pathname.
 378      *
 379      * <p> The exact form of a {@code file:} URI is system-dependent, hence
 380      * the transformation performed by this constructor is also
 381      * system-dependent.
 382      *


 409      * @see java.net.URI
 410      * @since 1.4
 411      */
 412     public File(URI uri) {
 413 
 414         // Check our many preconditions
 415         if (!uri.isAbsolute())
 416             throw new IllegalArgumentException("URI is not absolute");
 417         if (uri.isOpaque())
 418             throw new IllegalArgumentException("URI is not hierarchical");
 419         String scheme = uri.getScheme();
 420         if ((scheme == null) || !scheme.equalsIgnoreCase("file"))
 421             throw new IllegalArgumentException("URI scheme is not \"file\"");
 422         if (uri.getRawAuthority() != null)
 423             throw new IllegalArgumentException("URI has an authority component");
 424         if (uri.getRawFragment() != null)
 425             throw new IllegalArgumentException("URI has a fragment component");
 426         if (uri.getRawQuery() != null)
 427             throw new IllegalArgumentException("URI has a query component");
 428         String p = uri.getPath();
 429         if (p.equals(""))
 430             throw new IllegalArgumentException("URI path component is empty");
 431 
 432         // Okay, now initialize
 433         p = fs.fromURIPath(p);
 434         if (File.separatorChar != '/')
 435             p = p.replace('/', File.separatorChar);
 436         this.path = fs.normalize(p);
 437         this.prefixLength = fs.prefixLength(this.path);
 438     }
 439 
 440 
 441     /* -- Path-component accessors -- */
 442 
 443     /**
 444      * Returns the name of the file or directory denoted by this abstract
 445      * pathname.  This is just the last name in the pathname's name
 446      * sequence.  If the pathname's name sequence is empty, then the empty
 447      * string is returned.
 448      *
 449      * @return  The name of the file or directory denoted by this abstract




 240     public static final String pathSeparator = "" + pathSeparatorChar;
 241 
 242 
 243     /* -- Constructors -- */
 244 
 245     /**
 246      * Internal constructor for already-normalized pathname strings.
 247      */
 248     private File(String pathname, int prefixLength) {
 249         this.path = pathname;
 250         this.prefixLength = prefixLength;
 251     }
 252 
 253     /**
 254      * Internal constructor for already-normalized pathname strings.
 255      * The parameter order is used to disambiguate this method from the
 256      * public(File, String) constructor.
 257      */
 258     private File(String child, File parent) {
 259         assert parent.path != null;
 260         assert (!parent.path.isEmpty());
 261         this.path = fs.resolve(parent.path, child);
 262         this.prefixLength = parent.prefixLength;
 263     }
 264 
 265     /**
 266      * Creates a new <code>File</code> instance by converting the given
 267      * pathname string into an abstract pathname.  If the given string is
 268      * the empty string, then the result is the empty abstract pathname.
 269      *
 270      * @param   pathname  A pathname string
 271      * @throws  NullPointerException
 272      *          If the <code>pathname</code> argument is <code>null</code>
 273      */
 274     public File(String pathname) {
 275         if (pathname == null) {
 276             throw new NullPointerException();
 277         }
 278         this.path = fs.normalize(pathname);
 279         this.prefixLength = fs.prefixLength(this.path);
 280     }


 299      * a directory, and the <code>child</code> pathname string is taken to
 300      * denote either a directory or a file.  If the <code>child</code> pathname
 301      * string is absolute then it is converted into a relative pathname in a
 302      * system-dependent way.  If <code>parent</code> is the empty string then
 303      * the new <code>File</code> instance is created by converting
 304      * <code>child</code> into an abstract pathname and resolving the result
 305      * against a system-dependent default directory.  Otherwise each pathname
 306      * string is converted into an abstract pathname and the child abstract
 307      * pathname is resolved against the parent.
 308      *
 309      * @param   parent  The parent pathname string
 310      * @param   child   The child pathname string
 311      * @throws  NullPointerException
 312      *          If <code>child</code> is <code>null</code>
 313      */
 314     public File(String parent, String child) {
 315         if (child == null) {
 316             throw new NullPointerException();
 317         }
 318         if (parent != null) {
 319             if (parent.isEmpty()) {
 320                 this.path = fs.resolve(fs.getDefaultParent(),
 321                                        fs.normalize(child));
 322             } else {
 323                 this.path = fs.resolve(fs.normalize(parent),
 324                                        fs.normalize(child));
 325             }
 326         } else {
 327             this.path = fs.normalize(child);
 328         }
 329         this.prefixLength = fs.prefixLength(this.path);
 330     }
 331 
 332     /**
 333      * Creates a new <code>File</code> instance from a parent abstract
 334      * pathname and a child pathname string.
 335      *
 336      * <p> If <code>parent</code> is <code>null</code> then the new
 337      * <code>File</code> instance is created as if by invoking the
 338      * single-argument <code>File</code> constructor on the given
 339      * <code>child</code> pathname string.


 342      * denote a directory, and the <code>child</code> pathname string is taken
 343      * to denote either a directory or a file.  If the <code>child</code>
 344      * pathname string is absolute then it is converted into a relative
 345      * pathname in a system-dependent way.  If <code>parent</code> is the empty
 346      * abstract pathname then the new <code>File</code> instance is created by
 347      * converting <code>child</code> into an abstract pathname and resolving
 348      * the result against a system-dependent default directory.  Otherwise each
 349      * pathname string is converted into an abstract pathname and the child
 350      * abstract pathname is resolved against the parent.
 351      *
 352      * @param   parent  The parent abstract pathname
 353      * @param   child   The child pathname string
 354      * @throws  NullPointerException
 355      *          If <code>child</code> is <code>null</code>
 356      */
 357     public File(File parent, String child) {
 358         if (child == null) {
 359             throw new NullPointerException();
 360         }
 361         if (parent != null) {
 362             if (parent.path.isEmpty()) {
 363                 this.path = fs.resolve(fs.getDefaultParent(),
 364                                        fs.normalize(child));
 365             } else {
 366                 this.path = fs.resolve(parent.path,
 367                                        fs.normalize(child));
 368             }
 369         } else {
 370             this.path = fs.normalize(child);
 371         }
 372         this.prefixLength = fs.prefixLength(this.path);
 373     }
 374 
 375     /**
 376      * Creates a new {@code File} instance by converting the given
 377      * {@code file:} URI into an abstract pathname.
 378      *
 379      * <p> The exact form of a {@code file:} URI is system-dependent, hence
 380      * the transformation performed by this constructor is also
 381      * system-dependent.
 382      *


 409      * @see java.net.URI
 410      * @since 1.4
 411      */
 412     public File(URI uri) {
 413 
 414         // Check our many preconditions
 415         if (!uri.isAbsolute())
 416             throw new IllegalArgumentException("URI is not absolute");
 417         if (uri.isOpaque())
 418             throw new IllegalArgumentException("URI is not hierarchical");
 419         String scheme = uri.getScheme();
 420         if ((scheme == null) || !scheme.equalsIgnoreCase("file"))
 421             throw new IllegalArgumentException("URI scheme is not \"file\"");
 422         if (uri.getRawAuthority() != null)
 423             throw new IllegalArgumentException("URI has an authority component");
 424         if (uri.getRawFragment() != null)
 425             throw new IllegalArgumentException("URI has a fragment component");
 426         if (uri.getRawQuery() != null)
 427             throw new IllegalArgumentException("URI has a query component");
 428         String p = uri.getPath();
 429         if (p.isEmpty())
 430             throw new IllegalArgumentException("URI path component is empty");
 431 
 432         // Okay, now initialize
 433         p = fs.fromURIPath(p);
 434         if (File.separatorChar != '/')
 435             p = p.replace('/', File.separatorChar);
 436         this.path = fs.normalize(p);
 437         this.prefixLength = fs.prefixLength(this.path);
 438     }
 439 
 440 
 441     /* -- Path-component accessors -- */
 442 
 443     /**
 444      * Returns the name of the file or directory denoted by this abstract
 445      * pathname.  This is just the last name in the pathname's name
 446      * sequence.  If the pathname's name sequence is empty, then the empty
 447      * string is returned.
 448      *
 449      * @return  The name of the file or directory denoted by this abstract


< prev index next >