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 var key = list[i].key; |
| 18 if (key === object) { |
| 19 result = list[i].value; |
| 20 break; |
| 21 } |
| 22 if (key === null) { |
| 23 do_compact = true; |
| 24 list[i] = null; |
| 25 } |
| 26 } |
| 27 if (do_compact) { |
| 28 weak_property.value = list.filter((e) => (e !== null)); |
| 29 } |
| 30 return result; |
| 31 } |
| 32 |
| 33 void operator[]=(Object object, T value) { |
| 34 checkType(object); |
| 35 var weak_property = find(this); |
| 36 var list = weak_property.value; |
| 37 var do_compact = false; |
| 38 int i = 0; |
| 39 for (; i < list.length; ++i) { |
| 40 var key = list[i].key; |
| 41 if (key === object) { |
| 42 break; |
| 43 } |
| 44 if (key === null) { |
| 45 do_compact = true; |
| 46 list[i] = null; |
| 47 } |
| 48 } |
| 49 if (i !== list.length && value === null) { |
| 50 do_compact = true; |
| 51 list[i] = null; |
| 52 } else if (i !== list.length) { |
| 53 list[i].value = value; |
| 54 } else { |
| 55 list.add(new _WeakProperty(object, value)); |
| 56 } |
| 57 if (do_compact) { |
| 58 weak_property.value = list.filter((e) => (e !== null)); |
| 59 } |
| 60 } |
| 61 |
| 62 String toString() => "Expando:$name"; |
| 63 |
| 64 static checkType(object) { |
| 65 if (object === null) { |
| 66 throw new NullPointerException(); |
| 67 } |
| 68 if (object is bool || object is num || object is String) { |
| 69 throw new IllegalArgumentException(object); |
| 70 } |
| 71 } |
| 72 |
| 73 static find(expando) { |
| 74 if (data === null) data = new List(); |
| 75 var do_compact = false; |
| 76 int i = 0; |
| 77 for (; i < data.length; ++i) { |
| 78 var key = data[i].key; |
| 79 if (key == expando) { |
| 80 break; |
| 81 } |
| 82 if (key === null) { |
| 83 do_compact = true; |
| 84 data[i] = null; |
| 85 } |
| 86 } |
| 87 if (i == data.length) { |
| 88 data.add(new _WeakProperty(expando, new List())); |
| 89 } |
| 90 var result = data[i]; |
| 91 if (do_compact) { |
| 92 data = data.filter((e) => (e !== null)); |
| 93 } |
| 94 return result; |
| 95 } |
| 96 |
| 97 static List data; |
| 98 } |
OLD | NEW |