OLD | NEW |
(Empty) | |
| 1 library java.engine; |
| 2 |
| 3 import 'interner.dart'; |
| 4 import 'java_core.dart'; |
| 5 |
| 6 /** |
| 7 * A predicate is a one-argument function that returns a boolean value. |
| 8 */ |
| 9 typedef bool Predicate<E>(E argument); |
| 10 |
| 11 /** |
| 12 * Instances of the class `AnalysisException` represent an exception that |
| 13 * occurred during the analysis of one or more sources. |
| 14 */ |
| 15 class AnalysisException implements Exception { |
| 16 /** |
| 17 * The message that explains why the exception occurred. |
| 18 */ |
| 19 final String message; |
| 20 |
| 21 /** |
| 22 * The exception that caused this exception, or `null` if this exception was |
| 23 * not caused by another exception. |
| 24 */ |
| 25 final CaughtException cause; |
| 26 |
| 27 /** |
| 28 * Initialize a newly created exception to have the given [message] and |
| 29 * [cause]. |
| 30 */ |
| 31 AnalysisException([this.message = 'Exception', this.cause = null]); |
| 32 |
| 33 String toString() { |
| 34 StringBuffer buffer = new StringBuffer(); |
| 35 buffer.write("AnalysisException: "); |
| 36 buffer.writeln(message); |
| 37 if (cause != null) { |
| 38 buffer.write('Caused by '); |
| 39 cause._writeOn(buffer); |
| 40 } |
| 41 return buffer.toString(); |
| 42 } |
| 43 } |
| 44 |
| 45 /** |
| 46 * Instances of the class `CaughtException` represent an exception that was |
| 47 * caught and has an associated stack trace. |
| 48 */ |
| 49 class CaughtException implements Exception { |
| 50 /** |
| 51 * The exception that was caught. |
| 52 */ |
| 53 final Object exception; |
| 54 |
| 55 /** |
| 56 * The stack trace associated with the exception. |
| 57 */ |
| 58 StackTrace stackTrace; |
| 59 |
| 60 /** |
| 61 * Initialize a newly created caught exception to have the given [exception] |
| 62 * and [stackTrace]. |
| 63 */ |
| 64 CaughtException(this.exception, stackTrace) { |
| 65 if (stackTrace == null) { |
| 66 try { |
| 67 throw this; |
| 68 } catch (_, st) { |
| 69 stackTrace = st; |
| 70 } |
| 71 } |
| 72 this.stackTrace = stackTrace; |
| 73 } |
| 74 |
| 75 @override |
| 76 String toString() { |
| 77 StringBuffer buffer = new StringBuffer(); |
| 78 _writeOn(buffer); |
| 79 return buffer.toString(); |
| 80 } |
| 81 |
| 82 /** |
| 83 * Write a textual representation of the caught exception and its associated |
| 84 * stack trace. |
| 85 */ |
| 86 void _writeOn(StringBuffer buffer) { |
| 87 if (exception is AnalysisException) { |
| 88 AnalysisException analysisException = exception; |
| 89 buffer.writeln(analysisException.message); |
| 90 if (stackTrace != null) { |
| 91 buffer.writeln(stackTrace.toString()); |
| 92 } |
| 93 CaughtException cause = analysisException.cause; |
| 94 if (cause != null) { |
| 95 buffer.write('Caused by '); |
| 96 cause._writeOn(buffer); |
| 97 } |
| 98 } else { |
| 99 buffer.writeln(exception.toString()); |
| 100 buffer.writeln(stackTrace.toString()); |
| 101 } |
| 102 } |
| 103 } |
| 104 |
| 105 class FileNameUtilities { |
| 106 static String getExtension(String fileName) { |
| 107 if (fileName == null) { |
| 108 return ""; |
| 109 } |
| 110 int index = fileName.lastIndexOf('.'); |
| 111 if (index >= 0) { |
| 112 return fileName.substring(index + 1); |
| 113 } |
| 114 return ""; |
| 115 } |
| 116 } |
| 117 |
| 118 class StringUtilities { |
| 119 static const String EMPTY = ''; |
| 120 static const List<String> EMPTY_ARRAY = const <String>[]; |
| 121 |
| 122 static Interner INTERNER = new NullInterner(); |
| 123 |
| 124 static endsWith3(String str, int c1, int c2, int c3) { |
| 125 var length = str.length; |
| 126 return length >= 3 && |
| 127 str.codeUnitAt(length - 3) == c1 && |
| 128 str.codeUnitAt(length - 2) == c2 && |
| 129 str.codeUnitAt(length - 1) == c3; |
| 130 } |
| 131 static endsWithChar(String str, int c) { |
| 132 int length = str.length; |
| 133 return length > 0 && str.codeUnitAt(length - 1) == c; |
| 134 } |
| 135 static int indexOf1(String str, int start, int c) { |
| 136 int index = start; |
| 137 int last = str.length; |
| 138 while (index < last) { |
| 139 if (str.codeUnitAt(index) == c) { |
| 140 return index; |
| 141 } |
| 142 index++; |
| 143 } |
| 144 return -1; |
| 145 } |
| 146 static int indexOf2(String str, int start, int c1, int c2) { |
| 147 int index = start; |
| 148 int last = str.length - 1; |
| 149 while (index < last) { |
| 150 if (str.codeUnitAt(index) == c1 && str.codeUnitAt(index + 1) == c2) { |
| 151 return index; |
| 152 } |
| 153 index++; |
| 154 } |
| 155 return -1; |
| 156 } |
| 157 static int indexOf4( |
| 158 String string, int start, int c1, int c2, int c3, int c4) { |
| 159 int index = start; |
| 160 int last = string.length - 3; |
| 161 while (index < last) { |
| 162 if (string.codeUnitAt(index) == c1 && |
| 163 string.codeUnitAt(index + 1) == c2 && |
| 164 string.codeUnitAt(index + 2) == c3 && |
| 165 string.codeUnitAt(index + 3) == c4) { |
| 166 return index; |
| 167 } |
| 168 index++; |
| 169 } |
| 170 return -1; |
| 171 } |
| 172 static int indexOf5( |
| 173 String str, int start, int c1, int c2, int c3, int c4, int c5) { |
| 174 int index = start; |
| 175 int last = str.length - 4; |
| 176 while (index < last) { |
| 177 if (str.codeUnitAt(index) == c1 && |
| 178 str.codeUnitAt(index + 1) == c2 && |
| 179 str.codeUnitAt(index + 2) == c3 && |
| 180 str.codeUnitAt(index + 3) == c4 && |
| 181 str.codeUnitAt(index + 4) == c5) { |
| 182 return index; |
| 183 } |
| 184 index++; |
| 185 } |
| 186 return -1; |
| 187 } |
| 188 |
| 189 /** |
| 190 * Return the index of the first not letter/digit character in the [string] |
| 191 * that is at or after the [startIndex]. Return the length of the [string] if |
| 192 * all characters to the end are letters/digits. |
| 193 */ |
| 194 static int indexOfFirstNotLetterDigit(String string, int startIndex) { |
| 195 int index = startIndex; |
| 196 int last = string.length; |
| 197 while (index < last) { |
| 198 int c = string.codeUnitAt(index); |
| 199 if (!Character.isLetterOrDigit(c)) { |
| 200 return index; |
| 201 } |
| 202 index++; |
| 203 } |
| 204 return last; |
| 205 } |
| 206 static String intern(String string) => INTERNER.intern(string); |
| 207 static bool isEmpty(String s) { |
| 208 return s == null || s.isEmpty; |
| 209 } |
| 210 static bool isTagName(String s) { |
| 211 if (s == null || s.length == 0) { |
| 212 return false; |
| 213 } |
| 214 int sz = s.length; |
| 215 for (int i = 0; i < sz; i++) { |
| 216 int c = s.codeUnitAt(i); |
| 217 if (!Character.isLetter(c)) { |
| 218 if (i == 0) { |
| 219 return false; |
| 220 } |
| 221 if (!Character.isDigit(c) && c != 0x2D) { |
| 222 return false; |
| 223 } |
| 224 } |
| 225 } |
| 226 return true; |
| 227 } |
| 228 /** |
| 229 * Produce a string containing all of the names in the given array, surrounded
by single quotes, |
| 230 * and separated by commas. The list must contain at least two elements. |
| 231 * |
| 232 * @param names the names to be printed |
| 233 * @return the result of printing the names |
| 234 */ |
| 235 static String printListOfQuotedNames(List<String> names) { |
| 236 if (names == null) { |
| 237 throw new IllegalArgumentException("The list must not be null"); |
| 238 } |
| 239 int count = names.length; |
| 240 if (count < 2) { |
| 241 throw new IllegalArgumentException( |
| 242 "The list must contain at least two names"); |
| 243 } |
| 244 StringBuffer buffer = new StringBuffer(); |
| 245 buffer.write("'"); |
| 246 buffer.write(names[0]); |
| 247 buffer.write("'"); |
| 248 for (int i = 1; i < count - 1; i++) { |
| 249 buffer.write(", '"); |
| 250 buffer.write(names[i]); |
| 251 buffer.write("'"); |
| 252 } |
| 253 buffer.write(" and '"); |
| 254 buffer.write(names[count - 1]); |
| 255 buffer.write("'"); |
| 256 return buffer.toString(); |
| 257 } |
| 258 static startsWith2(String str, int start, int c1, int c2) { |
| 259 return str.length - start >= 2 && |
| 260 str.codeUnitAt(start) == c1 && |
| 261 str.codeUnitAt(start + 1) == c2; |
| 262 } |
| 263 static startsWith3(String str, int start, int c1, int c2, int c3) { |
| 264 return str.length - start >= 3 && |
| 265 str.codeUnitAt(start) == c1 && |
| 266 str.codeUnitAt(start + 1) == c2 && |
| 267 str.codeUnitAt(start + 2) == c3; |
| 268 } |
| 269 static startsWith4(String str, int start, int c1, int c2, int c3, int c4) { |
| 270 return str.length - start >= 4 && |
| 271 str.codeUnitAt(start) == c1 && |
| 272 str.codeUnitAt(start + 1) == c2 && |
| 273 str.codeUnitAt(start + 2) == c3 && |
| 274 str.codeUnitAt(start + 3) == c4; |
| 275 } |
| 276 static startsWith5( |
| 277 String str, int start, int c1, int c2, int c3, int c4, int c5) { |
| 278 return str.length - start >= 5 && |
| 279 str.codeUnitAt(start) == c1 && |
| 280 str.codeUnitAt(start + 1) == c2 && |
| 281 str.codeUnitAt(start + 2) == c3 && |
| 282 str.codeUnitAt(start + 3) == c4 && |
| 283 str.codeUnitAt(start + 4) == c5; |
| 284 } |
| 285 static startsWith6( |
| 286 String str, int start, int c1, int c2, int c3, int c4, int c5, int c6) { |
| 287 return str.length - start >= 6 && |
| 288 str.codeUnitAt(start) == c1 && |
| 289 str.codeUnitAt(start + 1) == c2 && |
| 290 str.codeUnitAt(start + 2) == c3 && |
| 291 str.codeUnitAt(start + 3) == c4 && |
| 292 str.codeUnitAt(start + 4) == c5 && |
| 293 str.codeUnitAt(start + 5) == c6; |
| 294 } |
| 295 static startsWithChar(String str, int c) { |
| 296 return str.length != 0 && str.codeUnitAt(0) == c; |
| 297 } |
| 298 |
| 299 static String substringBefore(String str, String separator) { |
| 300 if (str == null || str.isEmpty) { |
| 301 return str; |
| 302 } |
| 303 if (separator == null) { |
| 304 return str; |
| 305 } |
| 306 int pos = str.indexOf(separator); |
| 307 if (pos < 0) { |
| 308 return str; |
| 309 } |
| 310 return str.substring(0, pos); |
| 311 } |
| 312 |
| 313 static String substringBeforeChar(String str, int c) { |
| 314 if (isEmpty(str)) { |
| 315 return str; |
| 316 } |
| 317 int pos = indexOf1(str, 0, c); |
| 318 if (pos < 0) { |
| 319 return str; |
| 320 } |
| 321 return str.substring(0, pos); |
| 322 } |
| 323 } |
| 324 |
| 325 class UUID { |
| 326 static int __nextId = 0; |
| 327 final String id; |
| 328 UUID(this.id); |
| 329 String toString() => id; |
| 330 static UUID randomUUID() => new UUID((__nextId).toString()); |
| 331 } |
OLD | NEW |