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

Wed, 23 Jan 2013 13:27:24 -0800

author
jjg
date
Wed, 23 Jan 2013 13:27:24 -0800
changeset 1521
71f35e4b93a5
parent 581
f2fdd52e4e87
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8006775: JSR 308: Compiler changes in JDK8
Reviewed-by: jjg
Contributed-by: mernst@cs.washington.edu, wmdietl@cs.washington.edu, mpapi@csail.mit.edu, mahmood@notnoop.com

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

mercurial