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