src/share/classes/com/sun/tools/javac/util/Name.java

Thu, 10 Jun 2010 16:08:01 -0700

author
jjg
date
Thu, 10 Jun 2010 16:08:01 -0700
changeset 581
f2fdd52e4e87
parent 554
9d9f26857129
child 1326
30c36e23f154
permissions
-rw-r--r--

6944312: Potential rebranding issues in openjdk/langtools repository sources
Reviewed-by: darcy

duke@1 1 /*
ohair@554 2 * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 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
duke@1 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 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.
duke@1 24 */
duke@1 25
duke@1 26 package com.sun.tools.javac.util;
duke@1 27
jjg@113 28 /** An abstraction for internal compiler strings. They are stored in
jjg@113 29 * Utf8 format. Names are stored in a Name.Table, and are unique within
jjg@113 30 * that table.
duke@1 31 *
jjg@581 32 * <p><b>This is NOT part of any supported API.
jjg@581 33 * If you write code that depends on this, you do so at your own risk.
duke@1 34 * This code and its internal interfaces are subject to change or
duke@1 35 * deletion without notice.</b>
duke@1 36 */
jjg@113 37 public abstract class Name implements javax.lang.model.element.Name {
duke@1 38
jjg@113 39 public final Table table;
duke@1 40
jjg@113 41 protected Name(Table table) {
jjg@113 42 this.table = table;
duke@1 43 }
duke@1 44
jjg@113 45 /**
jjg@113 46 * @inheritDoc
duke@1 47 */
jjg@113 48 public boolean contentEquals(CharSequence cs) {
jjg@113 49 return toString().equals(cs.toString());
duke@1 50 }
duke@1 51
jjg@113 52 /**
jjg@113 53 * @inheritDoc
duke@1 54 */
duke@1 55 public int length() {
duke@1 56 return toString().length();
duke@1 57 }
duke@1 58
jjg@113 59 /**
jjg@113 60 * @inheritDoc
duke@1 61 */
jjg@113 62 public char charAt(int index) {
jjg@113 63 return toString().charAt(index);
duke@1 64 }
duke@1 65
jjg@113 66 /**
jjg@113 67 * @inheritDoc
duke@1 68 */
jjg@113 69 public CharSequence subSequence(int start, int end) {
jjg@113 70 return toString().subSequence(start, end);
jjg@113 71 }
jjg@113 72
jjg@113 73 /** Return the concatenation of this name and name `n'.
jjg@113 74 */
jjg@113 75 public Name append(Name n) {
jjg@113 76 int len = getByteLength();
jjg@113 77 byte[] bs = new byte[len + n.getByteLength()];
jjg@113 78 getBytes(bs, 0);
jjg@113 79 n.getBytes(bs, len);
jjg@113 80 return table.fromUtf(bs, 0, bs.length);
jjg@113 81 }
jjg@113 82
jjg@113 83 /** Return the concatenation of this name, the given ASCII
jjg@113 84 * character, and name `n'.
jjg@113 85 */
jjg@113 86 public Name append(char c, Name n) {
jjg@113 87 int len = getByteLength();
jjg@113 88 byte[] bs = new byte[len + 1 + n.getByteLength()];
jjg@113 89 getBytes(bs, 0);
jjg@113 90 bs[len] = (byte) c;
jjg@113 91 n.getBytes(bs, len+1);
jjg@113 92 return table.fromUtf(bs, 0, bs.length);
jjg@113 93 }
jjg@113 94
jjg@113 95 /** An arbitrary but consistent complete order among all Names.
jjg@113 96 */
jjg@113 97 public int compareTo(Name other) {
jjg@113 98 return other.getIndex() - this.getIndex();
jjg@113 99 }
jjg@113 100
jjg@113 101 /** Return true if this is the empty name.
jjg@113 102 */
jjg@113 103 public boolean isEmpty() {
jjg@113 104 return getByteLength() == 0;
duke@1 105 }
duke@1 106
duke@1 107 /** Returns last occurrence of byte b in this name, -1 if not found.
duke@1 108 */
duke@1 109 public int lastIndexOf(byte b) {
jjg@113 110 byte[] bytes = getByteArray();
jjg@113 111 int offset = getByteOffset();
jjg@113 112 int i = getByteLength() - 1;
jjg@113 113 while (i >= 0 && bytes[offset + i] != b) i--;
duke@1 114 return i;
duke@1 115 }
duke@1 116
duke@1 117 /** Does this name start with prefix?
duke@1 118 */
duke@1 119 public boolean startsWith(Name prefix) {
jjg@113 120 byte[] thisBytes = this.getByteArray();
jjg@113 121 int thisOffset = this.getByteOffset();
jjg@113 122 int thisLength = this.getByteLength();
jjg@113 123 byte[] prefixBytes = prefix.getByteArray();
jjg@113 124 int prefixOffset = prefix.getByteOffset();
jjg@113 125 int prefixLength = prefix.getByteLength();
jjg@113 126
duke@1 127 int i = 0;
jjg@113 128 while (i < prefixLength &&
jjg@113 129 i < thisLength &&
jjg@113 130 thisBytes[thisOffset + i] == prefixBytes[prefixOffset + i])
duke@1 131 i++;
jjg@113 132 return i == prefixLength;
duke@1 133 }
duke@1 134
duke@1 135 /** Returns the sub-name starting at position start, up to and
duke@1 136 * excluding position end.
duke@1 137 */
duke@1 138 public Name subName(int start, int end) {
duke@1 139 if (end < start) end = start;
jjg@113 140 return table.fromUtf(getByteArray(), getByteOffset() + start, end - start);
duke@1 141 }
duke@1 142
jjg@113 143 /** Return the string representation of this name.
duke@1 144 */
jjg@113 145 public String toString() {
jjg@113 146 return Convert.utf2string(getByteArray(), getByteOffset(), getByteLength());
duke@1 147 }
duke@1 148
jjg@113 149 /** Return the Utf8 representation of this name.
duke@1 150 */
jjg@113 151 public byte[] toUtf() {
jjg@113 152 byte[] bs = new byte[getByteLength()];
duke@1 153 getBytes(bs, 0);
jjg@113 154 return bs;
duke@1 155 }
duke@1 156
jjg@113 157 /* Get a "reasonably small" value that uniquely identifies this name
jjg@113 158 * within its name table.
duke@1 159 */
jjg@113 160 public abstract int getIndex();
jjg@113 161
jjg@113 162 /** Get the length (in bytes) of this name.
jjg@113 163 */
jjg@113 164 public abstract int getByteLength();
jjg@113 165
jjg@113 166 /** Returns i'th byte of this name.
jjg@113 167 */
jjg@113 168 public abstract byte getByteAt(int i);
jjg@113 169
jjg@113 170 /** Copy all bytes of this name to buffer cs, starting at start.
jjg@113 171 */
jjg@113 172 public void getBytes(byte cs[], int start) {
jjg@113 173 System.arraycopy(getByteArray(), getByteOffset(), cs, start, getByteLength());
duke@1 174 }
duke@1 175
jjg@113 176 /** Get the underlying byte array for this name. The contents of the
jjg@113 177 * array must not be modified.
duke@1 178 */
jjg@113 179 public abstract byte[] getByteArray();
duke@1 180
jjg@113 181 /** Get the start offset of this name within its byte array.
duke@1 182 */
jjg@113 183 public abstract int getByteOffset();
duke@1 184
jjg@113 185 /** An abstraction for the hash table used to create unique Name instances.
jjg@113 186 */
jjg@113 187 public static abstract class Table {
jjg@113 188 /** Standard name table.
jjg@113 189 */
jjg@113 190 public final Names names;
duke@1 191
jjg@113 192 Table(Names names) {
jjg@113 193 this.names = names;
duke@1 194 }
duke@1 195
jjg@113 196 /** Get the name from the characters in cs[start..start+len-1].
jjg@113 197 */
jjg@113 198 public abstract Name fromChars(char[] cs, int start, int len);
jjg@113 199
jjg@113 200 /** Get the name for the characters in string s.
jjg@113 201 */
jjg@113 202 public Name fromString(String s) {
jjg@113 203 char[] cs = s.toCharArray();
jjg@113 204 return fromChars(cs, 0, cs.length);
duke@1 205 }
duke@1 206
jjg@113 207 /** Get the name for the bytes in array cs.
jjg@113 208 * Assume that bytes are in utf8 format.
jjg@113 209 */
jjg@113 210 public Name fromUtf(byte[] cs) {
jjg@113 211 return fromUtf(cs, 0, cs.length);
duke@1 212 }
duke@1 213
jjg@113 214 /** get the name for the bytes in cs[start..start+len-1].
jjg@113 215 * Assume that bytes are in utf8 format.
jjg@113 216 */
jjg@113 217 public abstract Name fromUtf(byte[] cs, int start, int len);
duke@1 218
jjg@113 219 /** Release any resources used by this table.
jjg@113 220 */
jjg@113 221 public abstract void dispose();
jjg@113 222
jjg@113 223 /** The hashcode of a name.
jjg@113 224 */
jjg@113 225 protected static int hashValue(byte bytes[], int offset, int length) {
jjg@113 226 int h = 0;
jjg@113 227 int off = offset;
jjg@113 228
jjg@113 229 for (int i = 0; i < length; i++) {
jjg@113 230 h = (h << 5) - h + bytes[off++];
duke@1 231 }
jjg@113 232 return h;
duke@1 233 }
duke@1 234
jjg@113 235 /** Compare two subarrays
duke@1 236 */
jjg@113 237 protected static boolean equals(byte[] bytes1, int offset1,
jjg@113 238 byte[] bytes2, int offset2, int length) {
jjg@113 239 int i = 0;
jjg@113 240 while (i < length && bytes1[offset1 + i] == bytes2[offset2 + i]) {
jjg@113 241 i++;
jjg@113 242 }
jjg@113 243 return i == length;
duke@1 244 }
duke@1 245 }
duke@1 246 }

mercurial