OLD | NEW |
| (Empty) |
1 library java.core; | |
2 | |
3 import "dart:math" as math; | |
4 | |
5 class JavaSystem { | |
6 static int currentTimeMillis() { | |
7 return (new DateTime.now()).millisecondsSinceEpoch; | |
8 } | |
9 | |
10 static void arraycopy(List src, int srcPos, List dest, int destPos, int length
) { | |
11 for (int i = 0; i < length; i++) { | |
12 dest[destPos + i] = src[srcPos + i]; | |
13 } | |
14 } | |
15 } | |
16 | |
17 /** | |
18 * Limited implementation of "o is instanceOfType", see | |
19 * http://code.google.com/p/dart/issues/detail?id=8184 | |
20 */ | |
21 bool isInstanceOf(o, Type t) { | |
22 if (o == null) { | |
23 return false; | |
24 } | |
25 if (o.runtimeType == t) { | |
26 return true; | |
27 } | |
28 String oTypeName = o.runtimeType.toString(); | |
29 String tTypeName = t.toString(); | |
30 if (oTypeName == tTypeName) { | |
31 return true; | |
32 } | |
33 if (oTypeName.startsWith("List") && tTypeName == "List") { | |
34 return true; | |
35 } | |
36 if (tTypeName == "Map" && o is Map) { | |
37 return true; | |
38 } | |
39 // Dart Analysis Engine specific | |
40 if (oTypeName == "${tTypeName}Impl") { | |
41 return true; | |
42 } | |
43 if (tTypeName == "MethodElement") { | |
44 if (oTypeName == "MethodMember") { | |
45 return true; | |
46 } | |
47 } | |
48 if (tTypeName == "ExecutableElement") { | |
49 if (oTypeName == "MethodElementImpl" || | |
50 oTypeName == "FunctionElementImpl" || | |
51 oTypeName == "PropertyAccessorElementImpl") { | |
52 return true; | |
53 } | |
54 } | |
55 if (tTypeName == "ParameterElement") { | |
56 if (oTypeName == "FieldFormalParameterElementImpl" || | |
57 oTypeName == "DefaultFieldFormalParameterElementImpl" || | |
58 oTypeName == "DefaultParameterElementImpl") { | |
59 return true; | |
60 } | |
61 } | |
62 if (tTypeName == "VariableElement") { | |
63 if (oTypeName == "LocalVariableElementImpl" || | |
64 oTypeName == "ConstLocalVariableElementImpl" || | |
65 oTypeName == "FieldElementImpl" || | |
66 oTypeName == "ConstFieldElementImpl" || | |
67 oTypeName == "TopLevelVariableElementImpl" || | |
68 oTypeName == "ConstTopLevelVariableElementImpl") { | |
69 return true; | |
70 } | |
71 } | |
72 // no | |
73 return false; | |
74 } | |
75 | |
76 class JavaArrays { | |
77 static bool equals(List a, List b) { | |
78 if (a.length != b.length) { | |
79 return false; | |
80 } | |
81 var len = a.length; | |
82 for (int i = 0; i < len; i++) { | |
83 if (a[i] != b[i]) { | |
84 return false; | |
85 } | |
86 } | |
87 return true; | |
88 } | |
89 static int makeHashCode(List a) { | |
90 if (a == null) { | |
91 return 0; | |
92 } | |
93 int result = 1; | |
94 for (var element in a) { | |
95 result = 31 * result + (element == null ? 0 : element.hashCode); | |
96 } | |
97 return result; | |
98 } | |
99 static List asList(List list) => list; | |
100 } | |
101 | |
102 class Character { | |
103 static const int MAX_VALUE = 0xffff; | |
104 static const int MAX_CODE_POINT = 0x10ffff; | |
105 static const int MIN_SUPPLEMENTARY_CODE_POINT = 0x010000; | |
106 static const int MIN_LOW_SURROGATE = 0xDC00; | |
107 static const int MIN_HIGH_SURROGATE = 0xD800; | |
108 static bool isLetter(int c) { | |
109 return c >= 0x41 && c <= 0x5A || c >= 0x61 && c <= 0x7A; | |
110 } | |
111 static bool isLetterOrDigit(int c) { | |
112 return isLetter(c) || c >= 0x30 && c <= 0x39; | |
113 } | |
114 static bool isWhitespace(int c) { | |
115 return c == 0x09 || c == 0x20 || c == 0x0A || c == 0x0D; | |
116 } | |
117 static int digit(int codePoint, int radix) { | |
118 if (radix != 16) { | |
119 throw new ArgumentError("only radix == 16 is supported"); | |
120 } | |
121 if (0x30 <= codePoint && codePoint <= 0x39) { | |
122 return codePoint - 0x30; | |
123 } | |
124 if (0x41 <= codePoint && codePoint <= 0x46) { | |
125 return 0xA + (codePoint - 0x41); | |
126 } | |
127 if (0x61 <= codePoint && codePoint <= 0x66) { | |
128 return 0xA + (codePoint - 0x61); | |
129 } | |
130 return -1; | |
131 } | |
132 static String toChars(int codePoint) { | |
133 if (codePoint < 0 || codePoint > MAX_CODE_POINT) { | |
134 throw new IllegalArgumentException(); | |
135 } | |
136 if (codePoint < MIN_SUPPLEMENTARY_CODE_POINT) { | |
137 return new String.fromCharCode(codePoint); | |
138 } | |
139 int offset = codePoint - MIN_SUPPLEMENTARY_CODE_POINT; | |
140 int c0 = ((offset & 0x7FFFFFFF) >> 10) + MIN_HIGH_SURROGATE; | |
141 int c1 = (offset & 0x3ff) + MIN_LOW_SURROGATE; | |
142 return new String.fromCharCodes([c0, c1]); | |
143 } | |
144 } | |
145 | |
146 class CharSequence { | |
147 final String _content; | |
148 CharSequence(this._content); | |
149 static CharSequence wrap(String content) => new CharBuffer(content); | |
150 int charAt(int index) => _content.codeUnitAt(index); | |
151 int length() => _content.length; | |
152 String subSequence(int start, int end) => _content.substring(start, end); | |
153 } | |
154 | |
155 class CharBuffer extends CharSequence { | |
156 CharBuffer(String content) : super(content); | |
157 static CharBuffer wrap(String content) => new CharBuffer(content); | |
158 } | |
159 | |
160 class JavaString { | |
161 static String format(String fmt, List args) { | |
162 var index = 0; | |
163 return fmt.replaceAllMapped(new RegExp(r'%(.)'), (match) { | |
164 switch (match.group(1)) { | |
165 case '%': return '%'; | |
166 case 'd': | |
167 case 's': | |
168 if (index >= args.length) { | |
169 throw new MissingFormatArgumentException(match.group(0)); | |
170 } | |
171 return args[index++].toString(); | |
172 default: return match.group(1); | |
173 } | |
174 }); | |
175 } | |
176 static int indexOf(String target, String str, int fromIndex) { | |
177 if (fromIndex > target.length) return -1; | |
178 if (fromIndex < 0) fromIndex = 0; | |
179 return target.indexOf(str, fromIndex); | |
180 } | |
181 static int lastIndexOf(String target, String str, int fromIndex) { | |
182 if (fromIndex > target.length) return -1; | |
183 if (fromIndex < 0) fromIndex = 0; | |
184 return target.lastIndexOf(str, fromIndex); | |
185 } | |
186 static bool startsWithBefore(String s, String other, int start) { | |
187 return s.indexOf(other, start) != -1; | |
188 } | |
189 } | |
190 | |
191 /** | |
192 * Very limited printf implementation, supports only %s and %d. | |
193 */ | |
194 String _printf(String fmt, List args) { | |
195 StringBuffer sb = new StringBuffer(); | |
196 bool markFound = false; | |
197 int argIndex = 0; | |
198 for (int i = 0; i < fmt.length; i++) { | |
199 int c = fmt.codeUnitAt(i); | |
200 if (c == 0x25) { | |
201 if (markFound) { | |
202 sb.writeCharCode(c); | |
203 markFound = false; | |
204 } else { | |
205 markFound = true; | |
206 } | |
207 continue; | |
208 } | |
209 if (markFound) { | |
210 markFound = false; | |
211 // %d | |
212 if (c == 0x64) { | |
213 sb.writeCharCode(args[argIndex++]); | |
214 continue; | |
215 } | |
216 // %s | |
217 if (c == 0x73) { | |
218 sb.writeCharCode(args[argIndex++]); | |
219 continue; | |
220 } | |
221 // unknown | |
222 throw new IllegalArgumentException('[$fmt][$i] = 0x${c.toRadixString(16)}'
); | |
223 } else { | |
224 sb.writeCharCode(c); | |
225 } | |
226 } | |
227 return sb.toString(); | |
228 } | |
229 | |
230 abstract class PrintWriter { | |
231 void print(x); | |
232 | |
233 void newLine() { | |
234 this.print('\n'); | |
235 } | |
236 | |
237 void println(String s) { | |
238 this.print(s); | |
239 this.newLine(); | |
240 } | |
241 | |
242 void printf(String fmt, List args) { | |
243 this.print(_printf(fmt, args)); | |
244 } | |
245 } | |
246 | |
247 class PrintStringWriter extends PrintWriter { | |
248 final StringBuffer _sb = new StringBuffer(); | |
249 | |
250 void print(x) { | |
251 _sb.write(x); | |
252 } | |
253 | |
254 String toString() => _sb.toString(); | |
255 } | |
256 | |
257 class StringUtils { | |
258 static List<String> split(String s, String pattern) => s.split(pattern); | |
259 static String replace(String s, String from, String to) => s.replaceAll(from,
to); | |
260 static String repeat(String s, int n) { | |
261 StringBuffer sb = new StringBuffer(); | |
262 for (int i = 0; i < n; i++) { | |
263 sb.write(s); | |
264 } | |
265 return sb.toString(); | |
266 } | |
267 } | |
268 | |
269 class Math { | |
270 static num max(num a, num b) => math.max(a, b); | |
271 static num min(num a, num b) => math.min(a, b); | |
272 } | |
273 | |
274 class RuntimeException extends JavaException { | |
275 RuntimeException([String message = "", Exception cause = null]) : | |
276 super(message, cause); | |
277 } | |
278 | |
279 class JavaException implements Exception { | |
280 final String message; | |
281 final Exception cause; | |
282 JavaException([this.message = "", this.cause = null]); | |
283 JavaException.withCause(this.cause) : message = null; | |
284 String toString() => "${runtimeType}: $message $cause"; | |
285 } | |
286 | |
287 class JavaIOException extends JavaException { | |
288 JavaIOException([message = "", cause = null]) : super(message, cause); | |
289 } | |
290 | |
291 class IllegalArgumentException extends JavaException { | |
292 IllegalArgumentException([message = "", cause = null]) : super(message, cause)
; | |
293 } | |
294 | |
295 class StringIndexOutOfBoundsException extends JavaException { | |
296 StringIndexOutOfBoundsException(int index) : super('$index'); | |
297 } | |
298 | |
299 class IllegalStateException extends JavaException { | |
300 IllegalStateException([message = ""]) : super(message); | |
301 } | |
302 | |
303 class UnsupportedOperationException extends JavaException { | |
304 String toString() => "UnsupportedOperationException"; | |
305 } | |
306 | |
307 class NumberFormatException extends JavaException { | |
308 String toString() => "NumberFormatException"; | |
309 } | |
310 | |
311 /// Parses given string to [Uri], throws [URISyntaxException] if invalid. | |
312 Uri parseUriWithException(String str) { | |
313 Uri uri = Uri.parse(str); | |
314 if (uri.path.isEmpty) { | |
315 throw new URISyntaxException(); | |
316 } | |
317 return uri; | |
318 } | |
319 | |
320 class URISyntaxException implements Exception { | |
321 String toString() => "URISyntaxException"; | |
322 } | |
323 | |
324 class MissingFormatArgumentException implements Exception { | |
325 final String s; | |
326 | |
327 String toString() => "MissingFormatArgumentException: $s"; | |
328 | |
329 MissingFormatArgumentException(this.s); | |
330 } | |
331 | |
332 class JavaIterator<E> { | |
333 Iterable<E> _iterable; | |
334 List<E> _elements = new List<E>(); | |
335 int _coPos = 0; | |
336 int _elPos = 0; | |
337 E _current = null; | |
338 JavaIterator(this._iterable) { | |
339 Iterator iterator = _iterable.iterator; | |
340 while (iterator.moveNext()) { | |
341 _elements.add(iterator.current); | |
342 } | |
343 } | |
344 | |
345 bool get hasNext { | |
346 return _elPos < _elements.length; | |
347 } | |
348 | |
349 E next() { | |
350 _current = _elements[_elPos]; | |
351 _coPos++; | |
352 _elPos++; | |
353 return _current; | |
354 } | |
355 | |
356 void remove() { | |
357 if (_iterable is List) { | |
358 _coPos--; | |
359 (_iterable as List).remove(_coPos); | |
360 } else if (_iterable is Set) { | |
361 (_iterable as Set).remove(_current); | |
362 } else { | |
363 throw new StateError("Unsupported iterable ${_iterable.runtimeType}"); | |
364 } | |
365 } | |
366 } | |
367 | |
368 class MapEntry<K, V> { | |
369 final Map<K, V> _map; | |
370 final K _key; | |
371 V _value; | |
372 MapEntry(this._map, this._key, this._value); | |
373 K getKey() => _key; | |
374 V getValue() => _value; | |
375 V setValue(V v) { | |
376 V prevValue = _value; | |
377 _value = v; | |
378 _map[_key] = v; | |
379 return prevValue; | |
380 } | |
381 } | |
382 | |
383 Iterable<MapEntry> getMapEntrySet(Map m) { | |
384 List<MapEntry> result = []; | |
385 m.forEach((k, v) { | |
386 result.add(new MapEntry(m, k, v)); | |
387 }); | |
388 return result; | |
389 } | |
390 | |
391 javaListSet(List list, int index, newValue) { | |
392 var oldValue = list[index]; | |
393 list[index] = newValue; | |
394 return oldValue; | |
395 } | |
396 | |
397 bool javaSetAdd(Set s, o) { | |
398 if (!s.contains(o)) { | |
399 s.add(o); | |
400 return true; | |
401 } | |
402 return false; | |
403 } | |
404 | |
405 bool javaCollectionContainsAll(Iterable list, Iterable c) { | |
406 return c.fold(true, (bool prev, e) => prev && list.contains(e)); | |
407 } | |
408 | |
409 javaMapPut(Map target, key, value) { | |
410 var oldValue = target[key]; | |
411 target[key] = value; | |
412 return oldValue; | |
413 } | |
414 | |
415 void javaMapPutAll(Map target, Map source) { | |
416 source.forEach((k, v) { | |
417 target[k] = v; | |
418 }); | |
419 } | |
420 | |
421 bool javaStringEqualsIgnoreCase(String a, String b) { | |
422 return a.toLowerCase() == b.toLowerCase(); | |
423 } | |
424 | |
425 bool javaStringRegionMatches(String t, int toffset, String o, int ooffset, int l
en) { | |
426 if (toffset < 0) return false; | |
427 if (ooffset < 0) return false; | |
428 var tend = toffset + len; | |
429 var oend = ooffset + len; | |
430 if (tend > t.length) return false; | |
431 if (oend > o.length) return false; | |
432 return t.substring(toffset, tend) == o.substring(ooffset, oend); | |
433 } | |
434 | |
435 bool javaBooleanOr(bool a, bool b) { | |
436 return a || b; | |
437 } | |
438 | |
439 class JavaStringBuilder { | |
440 StringBuffer sb = new StringBuffer(); | |
441 String toString() => sb.toString(); | |
442 JavaStringBuilder append(x) { | |
443 sb.write(x); | |
444 return this; | |
445 } | |
446 JavaStringBuilder appendChar(int c) { | |
447 sb.writeCharCode(c); | |
448 return this; | |
449 } | |
450 int get length => sb.length; | |
451 void set length(int newLength) { | |
452 if (newLength < 0) { | |
453 throw new StringIndexOutOfBoundsException(newLength); | |
454 } | |
455 if (sb.length < newLength) { | |
456 while (sb.length < newLength) { | |
457 sb.writeCharCode(0); | |
458 } | |
459 } else if (sb.length > newLength) { | |
460 var s = sb.toString().substring(0, newLength); | |
461 sb = new StringBuffer(s); | |
462 } | |
463 } | |
464 void clear() { | |
465 sb = new StringBuffer(); | |
466 } | |
467 } | |
468 | |
469 abstract class Enum<E extends Enum> implements Comparable<E> { | |
470 /// The name of this enum constant, as declared in the enum declaration. | |
471 final String name; | |
472 /// The position in the enum declaration. | |
473 final int ordinal; | |
474 Enum(this.name, this.ordinal); | |
475 int get hashCode => ordinal; | |
476 String toString() => name; | |
477 int compareTo(E other) => ordinal - other.ordinal; | |
478 } | |
479 | |
480 class JavaPatternMatcher { | |
481 Iterator<Match> _matches; | |
482 Match _match; | |
483 JavaPatternMatcher(RegExp re, String input) { | |
484 _matches = re.allMatches(input).iterator; | |
485 } | |
486 bool find() { | |
487 if (!_matches.moveNext()) { | |
488 return false; | |
489 } | |
490 _match = _matches.current; | |
491 return true; | |
492 } | |
493 String group(int i) => _match[i]; | |
494 int start() => _match.start; | |
495 int end() => _match.end; | |
496 } | |
OLD | NEW |