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