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 // TODO(nweiz): when all implementations we target have the same name for the | |
6 // coreimpl implementation of List<E>, extend that rather than wrapping. | |
7 class _ListWrapper<E> implements List<E> { | |
8 List _list; | |
9 | |
10 _ListWrapper(List this._list); | |
11 | |
12 Iterator<E> iterator() => _list.iterator(); | |
13 | |
14 void forEach(void f(E element)) => _list.forEach(f); | |
15 | |
16 Collection map(f(E element)) => _list.map(f); | |
17 | |
18 List<E> filter(bool f(E element)) => _list.filter(f); | |
19 | |
20 bool every(bool f(E element)) => _list.every(f); | |
21 | |
22 bool some(bool f(E element)) => _list.some(f); | |
23 | |
24 bool isEmpty() => _list.isEmpty(); | |
25 | |
26 int get length() => _list.length; | |
27 | |
28 E operator [](int index) => _list[index]; | |
29 | |
30 void operator []=(int index, E value) { _list[index] = value; } | |
31 | |
32 void set length(int newLength) { _list.length = newLength; } | |
33 | |
34 void add(E value) => _list.add(value); | |
35 | |
36 void addLast(E value) => _list.addLast(value); | |
37 | |
38 void addAll(Collection<E> collection) => _list.addAll(collection); | |
39 | |
40 void sort(int compare(E a, E b)) => _list.sort(compare); | |
41 | |
42 int indexOf(E element, [int start = 0]) => _list.indexOf(element, start); | |
43 | |
44 int lastIndexOf(E element, [int start = 0]) => | |
45 _list.lastIndexOf(element, start); | |
46 | |
47 void clear() => _list.clear(); | |
48 | |
49 E removeLast() => _list.removeLast(); | |
50 | |
51 E last() => _list.last(); | |
52 | |
53 List<E> getRange(int start, int length) => _list.getRange(start, length); | |
54 | |
55 void setRange(int start, int length, List<E> from, [int startFrom = 0]) => | |
56 _list.setRange(start, length, from, startFrom); | |
57 | |
58 void removeRange(int start, int length) => _list.removeRange(start, length); | |
59 | |
60 void insertRange(int start, int length, [E initialValue = null]) => | |
61 _list.insertRange(start, length, initialValue); | |
62 | |
63 E get first() => _list[0]; | |
64 } | |
65 | |
66 /** | |
67 * This class is used to insure the results of list operations are NodeLists | |
68 * instead of lists. | |
69 */ | |
70 class _NodeListWrapper extends _ListWrapper<Node> implements NodeList { | |
71 _NodeListWrapper(List list) : super(list); | |
72 | |
73 NodeList filter(bool f(Node element)) => | |
74 new _NodeListWrapper(_list.filter(f)); | |
75 | |
76 NodeList getRange(int start, int length) => | |
77 new _NodeListWrapper(_list.getRange(start, length)); | |
78 } | |
79 | |
80 class $CLASSNAME$EXTENDS$IMPLEMENTS$NATIVESPEC { | |
81 _NodeImpl _parent; | |
82 | |
83 // -- start List<Node> mixins. | |
84 // Node is the element type. | |
85 | |
86 // From Iterable<Node>: | |
87 | |
88 Iterator<Node> iterator() { | |
89 // Note: NodeLists are not fixed size. And most probably length shouldn't | |
90 // be cached in both iterator _and_ forEach method. For now caching it | |
91 // for consistency. | |
92 return new _FixedSizeListIterator<Node>(this); | |
93 } | |
94 | |
95 // From Collection<Node>: | |
96 | |
97 void add(_NodeImpl value) { | |
98 _parent._appendChild(value); | |
99 } | |
100 | |
101 void addLast(_NodeImpl value) { | |
102 _parent._appendChild(value); | |
103 } | |
104 | |
105 void addAll(Collection<_NodeImpl> collection) { | |
106 for (_NodeImpl node in collection) { | |
107 _parent._appendChild(node); | |
108 } | |
109 } | |
110 | |
111 _NodeImpl removeLast() { | |
112 final last = this.last(); | |
113 if (last != null) { | |
114 _parent._removeChild(last); | |
115 } | |
116 return last; | |
117 } | |
118 | |
119 void clear() { | |
120 _parent.text = ''; | |
121 } | |
122 | |
123 void operator []=(int index, _NodeImpl value) { | |
124 _parent._replaceChild(value, this[index]); | |
125 } | |
126 | |
127 void forEach(void f(Node element)) => _Collections.forEach(this, f); | |
128 | |
129 Collection map(f(Node element)) => _Collections.map(this, [], f); | |
130 | |
131 Collection<Node> filter(bool f(Node element)) => | |
132 new _NodeListWrapper(_Collections.filter(this, <Node>[], f)); | |
133 | |
134 bool every(bool f(Node element)) => _Collections.every(this, f); | |
135 | |
136 bool some(bool f(Node element)) => _Collections.some(this, f); | |
137 | |
138 bool isEmpty() => this.length == 0; | |
139 | |
140 // From List<Node>: | |
141 | |
142 void sort(int compare(Node a, Node b)) { | |
143 throw new UnsupportedOperationException("Cannot sort immutable List."); | |
144 } | |
145 | |
146 int indexOf(Node element, [int start = 0]) => | |
147 _Lists.indexOf(this, element, start, this.length); | |
148 | |
149 int lastIndexOf(Node element, [int start = 0]) => | |
150 _Lists.lastIndexOf(this, element, start); | |
151 | |
152 Node last() => this[length - 1]; | |
153 Node get first() => this[0]; | |
154 | |
155 // FIXME: implement thesee. | |
156 void setRange(int start, int length, List<Node> from, [int startFrom]) { | |
157 throw new UnsupportedOperationException("Cannot setRange on immutable List."
); | |
158 } | |
159 void removeRange(int start, int length) { | |
160 throw new UnsupportedOperationException("Cannot removeRange on immutable Lis
t."); | |
161 } | |
162 void insertRange(int start, int length, [Node initialValue]) { | |
163 throw new UnsupportedOperationException("Cannot insertRange on immutable Lis
t."); | |
164 } | |
165 NodeList getRange(int start, int length) => | |
166 new _NodeListWrapper(_Lists.getRange(this, start, length, <Node>[])); | |
167 | |
168 // -- end List<Node> mixins. | |
169 | |
170 $!MEMBERS | |
171 } | |
OLD | NEW |