OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 class _Expando<T> implements Expando<T> { |
| 6 final String name; |
| 7 |
| 8 const _Expando([String this.name]); |
| 9 |
| 10 T operator[](Object object) { |
| 11 checkType(object); |
| 12 var weak_property = find(this); |
| 13 var list = weak_property.value; |
| 14 var do_compact = false; |
| 15 var result = null; |
| 16 for (int i = 0; i < list.length; ++i) { |
| 17 if (list[i].key === object) { |
| 18 result = list[i].value; |
| 19 break; |
| 20 } |
| 21 if (list[i].key === null) { |
| 22 do_compact = true; |
| 23 list[i] = null; |
| 24 } |
| 25 } |
| 26 if (do_compact) { |
| 27 weak_property.value = list.filter((e) => (e !== null)); |
| 28 } |
| 29 return result; |
| 30 } |
| 31 |
| 32 void operator[]=(Object object, T value) { |
| 33 checkType(object); |
| 34 var weak_property = find(this); |
| 35 var list = weak_property.value; |
| 36 var do_compact = false; |
| 37 int i = 0; |
| 38 for (; i < list.length; ++i) { |
| 39 var key = list[i].key; |
| 40 if (key === object) { |
| 41 break; |
| 42 } |
| 43 if (key === null) { |
| 44 do_compact = true; |
| 45 list[i] = null; |
| 46 } |
| 47 } |
| 48 if (i !== list.length && value === null) { |
| 49 do_compact = true; |
| 50 list[i] = null; |
| 51 } else if (i !== list.length) { |
| 52 list[i].value = value; |
| 53 } else { |
| 54 list.add(new WeakProperty(object, value)); |
| 55 } |
| 56 if (do_compact) { |
| 57 weak_property.value = list.filter((e) => (e !== null)); |
| 58 } |
| 59 } |
| 60 |
| 61 String toString() => "Expando:$name"; |
| 62 |
| 63 static checkType(object) { |
| 64 if (object === null) { |
| 65 throw new NullPointerException(); |
| 66 } |
| 67 if (object is bool || object is num || object is String) { |
| 68 throw new IllegalArgumentException(object); |
| 69 } |
| 70 } |
| 71 |
| 72 static find(expando) { |
| 73 if (data === null) data = new List(); |
| 74 var do_compact = false; |
| 75 int i = 0; |
| 76 for (; i < data.length; ++i) { |
| 77 var key = data[i].key; |
| 78 if (key == expando) { |
| 79 break; |
| 80 } |
| 81 if (key === null) { |
| 82 do_compact = true; |
| 83 data[i] = null; |
| 84 } |
| 85 } |
| 86 if (i == data.length) { |
| 87 data.add(new WeakProperty(expando, new List())); |
| 88 } |
| 89 var result = data[i]; |
| 90 if (do_compact) { |
| 91 data = data.filter((e) => (e !== null)); |
| 92 } |
| 93 return result; |
| 94 } |
| 95 |
| 96 static List data; |
| 97 } |
OLD | NEW |