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

Tue, 25 Sep 2012 13:06:58 -0700

author
jjg
date
Tue, 25 Sep 2012 13:06:58 -0700
changeset 1339
0e5899f09dab
parent 1326
30c36e23f154
child 2525
2eb010b6cb22
permissions
-rw-r--r--

7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
Reviewed-by: mcimadamore, jjg
Contributed-by: vicenterz@yahoo.es

duke@1 1 /*
jjg@1326 2 * Copyright (c) 1999, 2012, 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@1326 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@1326 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@1326 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@1326 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@1326 145 @Override
jjg@113 146 public String toString() {
jjg@113 147 return Convert.utf2string(getByteArray(), getByteOffset(), getByteLength());
duke@1 148 }
duke@1 149
jjg@113 150 /** Return the Utf8 representation of this name.
duke@1 151 */
jjg@113 152 public byte[] toUtf() {
jjg@113 153 byte[] bs = new byte[getByteLength()];
duke@1 154 getBytes(bs, 0);
jjg@113 155 return bs;
duke@1 156 }
duke@1 157
jjg@113 158 /* Get a "reasonably small" value that uniquely identifies this name
jjg@113 159 * within its name table.
duke@1 160 */
jjg@113 161 public abstract int getIndex();
jjg@113 162
jjg@113 163 /** Get the length (in bytes) of this name.
jjg@113 164 */
jjg@113 165 public abstract int getByteLength();
jjg@113 166
jjg@113 167 /** Returns i'th byte of this name.
jjg@113 168 */
jjg@113 169 public abstract byte getByteAt(int i);
jjg@113 170
jjg@113 171 /** Copy all bytes of this name to buffer cs, starting at start.
jjg@113 172 */
jjg@113 173 public void getBytes(byte cs[], int start) {
jjg@113 174 System.arraycopy(getByteArray(), getByteOffset(), cs, start, getByteLength());
duke@1 175 }
duke@1 176
jjg@113 177 /** Get the underlying byte array for this name. The contents of the
jjg@113 178 * array must not be modified.
duke@1 179 */
jjg@113 180 public abstract byte[] getByteArray();
duke@1 181
jjg@113 182 /** Get the start offset of this name within its byte array.
duke@1 183 */
jjg@113 184 public abstract int getByteOffset();
duke@1 185
jjg@113 186 /** An abstraction for the hash table used to create unique Name instances.
jjg@113 187 */
jjg@113 188 public static abstract class Table {
jjg@113 189 /** Standard name table.
jjg@113 190 */
jjg@113 191 public final Names names;
duke@1 192
jjg@113 193 Table(Names names) {
jjg@113 194 this.names = names;
duke@1 195 }
duke@1 196
jjg@113 197 /** Get the name from the characters in cs[start..start+len-1].
jjg@113 198 */
jjg@113 199 public abstract Name fromChars(char[] cs, int start, int len);
jjg@113 200
jjg@113 201 /** Get the name for the characters in string s.
jjg@113 202 */
jjg@113 203 public Name fromString(String s) {
jjg@113 204 char[] cs = s.toCharArray();
jjg@113 205 return fromChars(cs, 0, cs.length);
duke@1 206 }
duke@1 207
jjg@113 208 /** Get the name for the bytes in array cs.
jjg@113 209 * Assume that bytes are in utf8 format.
jjg@113 210 */
jjg@113 211 public Name fromUtf(byte[] cs) {
jjg@113 212 return fromUtf(cs, 0, cs.length);
duke@1 213 }
duke@1 214
jjg@113 215 /** get the name for the bytes in cs[start..start+len-1].
jjg@113 216 * Assume that bytes are in utf8 format.
jjg@113 217 */
jjg@113 218 public abstract Name fromUtf(byte[] cs, int start, int len);
duke@1 219
jjg@113 220 /** Release any resources used by this table.
jjg@113 221 */
jjg@113 222 public abstract void dispose();
jjg@113 223
jjg@113 224 /** The hashcode of a name.
jjg@113 225 */
jjg@113 226 protected static int hashValue(byte bytes[], int offset, int length) {
jjg@113 227 int h = 0;
jjg@113 228 int off = offset;
jjg@113 229
jjg@113 230 for (int i = 0; i < length; i++) {
jjg@113 231 h = (h << 5) - h + bytes[off++];
duke@1 232 }
jjg@113 233 return h;
duke@1 234 }
duke@1 235
jjg@113 236 /** Compare two subarrays
duke@1 237 */
jjg@113 238 protected static boolean equals(byte[] bytes1, int offset1,
jjg@113 239 byte[] bytes2, int offset2, int length) {
jjg@113 240 int i = 0;
jjg@113 241 while (i < length && bytes1[offset1 + i] == bytes2[offset2 + i]) {
jjg@113 242 i++;
jjg@113 243 }
jjg@113 244 return i == length;
duke@1 245 }
duke@1 246 }
duke@1 247 }

mercurial