src/share/classes/com/sun/tools/javac/file/RelativePath.java

Mon, 27 Jul 2009 19:52:42 -0700

author
jjg
date
Mon, 27 Jul 2009 19:52:42 -0700
changeset 333
7c2d6da61646
parent 103
e571266ae14f
child 554
9d9f26857129
permissions
-rw-r--r--

6865399: some javac files are missing Sun internal API comment
Reviewed-by: darcy

     1 /*
     2  * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Sun designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Sun in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
    23  * have any questions.
    24  */
    26 package com.sun.tools.javac.file;
    28 import java.io.File;
    29 import java.util.zip.ZipEntry;
    30 import java.util.zip.ZipFile;
    31 import javax.tools.JavaFileObject;
    33 /**
    34  * Used to represent a platform-neutral path within a platform-specific
    35  * container, such as a directory or zip file.
    36  * Internally, the file separator is always '/'.
    37  *
    38  * <p><b>This is NOT part of any API supported by Sun Microsystems.
    39  * If you write code that depends on this, you do so at your own risk.
    40  * This code and its internal interfaces are subject to change or
    41  * deletion without notice.</b>
    42  */
    43 public abstract class RelativePath implements Comparable<RelativePath> {
    44     /**
    45      * @param p must use '/' as an internal separator
    46      */
    47     protected RelativePath(String p) {
    48         path = p;
    49     }
    51     public abstract RelativeDirectory dirname();
    53     public abstract String basename();
    55     public File getFile(File directory) {
    56         if (path.length() == 0)
    57             return directory;
    58         return new File(directory, path.replace('/', File.separatorChar));
    59     }
    61     public int compareTo(RelativePath other) {
    62         return path.compareTo(other.path);
    63     }
    65     @Override
    66     public boolean equals(Object other) {
    67         if (!(other instanceof RelativePath))
    68             return false;
    69          return path.equals(((RelativePath) other).path);
    70     }
    72     @Override
    73     public int hashCode() {
    74         return path.hashCode();
    75     }
    77     @Override
    78     public String toString() {
    79         return "RelPath[" + path + "]";
    80     }
    82     public String getPath() {
    83         return path;
    84     }
    86     protected final String path;
    88     /**
    89      * Used to represent a platform-neutral subdirectory within a platform-specific
    90      * container, such as a directory or zip file.
    91      * Internally, the file separator is always '/', and if the path is not empty,
    92      * it always ends in a '/' as well.
    93      */
    94     public static class RelativeDirectory extends RelativePath {
    96         static RelativeDirectory forPackage(CharSequence packageName) {
    97             return new RelativeDirectory(packageName.toString().replace('.', '/'));
    98         }
   100         /**
   101          * @param p must use '/' as an internal separator
   102          */
   103         public RelativeDirectory(String p) {
   104             super(p.length() == 0 || p.endsWith("/") ? p : p + "/");
   105         }
   107         /**
   108          * @param p must use '/' as an internal separator
   109          */
   110         public RelativeDirectory(RelativeDirectory d, String p) {
   111             this(d.path + p);
   112         }
   114         @Override
   115         public RelativeDirectory dirname() {
   116             int l = path.length();
   117             if (l == 0)
   118                 return this;
   119             int sep = path.lastIndexOf('/', l - 2);
   120             return new RelativeDirectory(path.substring(0, sep + 1));
   121         }
   123         @Override
   124         public String basename() {
   125             int l = path.length();
   126             if (l == 0)
   127                 return path;
   128             int sep = path.lastIndexOf('/', l - 2);
   129             return path.substring(sep + 1, l - 1);
   130         }
   132         /**
   133          * Return true if this subdirectory "contains" the other path.
   134          * A subdirectory path does not contain itself.
   135          **/
   136         boolean contains(RelativePath other) {
   137             return other.path.length() > path.length() && other.path.startsWith(path);
   138         }
   140         @Override
   141         public String toString() {
   142             return "RelativeDirectory[" + path + "]";
   143         }
   144     }
   146     /**
   147      * Used to represent a platform-neutral file within a platform-specific
   148      * container, such as a directory or zip file.
   149      * Internally, the file separator is always '/'. It never ends in '/'.
   150      */
   151     public static class RelativeFile extends RelativePath {
   152         static RelativeFile forClass(CharSequence className, JavaFileObject.Kind kind) {
   153             return new RelativeFile(className.toString().replace('.', '/') + kind.extension);
   154         }
   156         public RelativeFile(String p) {
   157             super(p);
   158             if (p.endsWith("/"))
   159                 throw new IllegalArgumentException(p);
   160         }
   162         /**
   163          * @param p must use '/' as an internal separator
   164          */
   165         public RelativeFile(RelativeDirectory d, String p) {
   166             this(d.path + p);
   167         }
   169         RelativeFile(RelativeDirectory d, RelativePath p) {
   170             this(d, p.path);
   171         }
   173         @Override
   174         public RelativeDirectory dirname() {
   175             int sep = path.lastIndexOf('/');
   176             return new RelativeDirectory(path.substring(0, sep + 1));
   177         }
   179         @Override
   180         public String basename() {
   181             int sep = path.lastIndexOf('/');
   182             return path.substring(sep + 1);
   183         }
   185         ZipEntry getZipEntry(ZipFile zip) {
   186             return zip.getEntry(path);
   187         }
   189         @Override
   190         public String toString() {
   191             return "RelativeFile[" + path + "]";
   192         }
   194     }
   196 }

mercurial