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