OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 class ArrayFactory { | 5 class ArrayFactory { |
6 factory Array<E>.from(Iterable<E> other) { | 6 factory Array<E>.from(Iterable<E> other) { |
7 Array<E> array = new Array<E>(); | 7 Array<E> array = new Array<E>(); |
8 for (final e in other) { | 8 for (final e in other) { |
9 array.add(e); | 9 array.add(e); |
10 } | 10 } |
(...skipping 11 matching lines...) Expand all Loading... |
22 } | 22 } |
23 return array; | 23 return array; |
24 } | 24 } |
25 | 25 |
26 factory Array<E>([int length = null]) { | 26 factory Array<E>([int length = null]) { |
27 bool isFixed = true; | 27 bool isFixed = true; |
28 if (length === null) { | 28 if (length === null) { |
29 length = 0; | 29 length = 0; |
30 isFixed = false; | 30 isFixed = false; |
31 } else if (length < 0) { | 31 } else if (length < 0) { |
32 throw new IllegalArgumentException(length); | 32 throw new IllegalArgumentException("negative length $length"); |
33 } | 33 } |
34 // TODO(floitsch): make array creation more efficient. Currently we allocate | 34 // TODO(floitsch): make array creation more efficient. Currently we allocate |
35 // a new TypeToken at every allocation. Either we can optimize them away, | 35 // a new TypeToken at every allocation. Either we can optimize them away, |
36 // or we need to find other ways to pass type-information from Dart to JS. | 36 // or we need to find other ways to pass type-information from Dart to JS. |
37 ObjectArray array = _new(new TypeToken<E>(), length); | 37 ObjectArray array = _new(new TypeToken<E>(), length); |
38 array._isFixed = isFixed; | 38 array._isFixed = isFixed; |
39 return array; | 39 return array; |
40 } | 40 } |
41 | 41 |
42 static ObjectArray _new(TypeToken typeToken, int length) native; | 42 static ObjectArray _new(TypeToken typeToken, int length) native; |
(...skipping 21 matching lines...) Expand all Loading... |
64 } | 64 } |
65 return list; | 65 return list; |
66 } | 66 } |
67 | 67 |
68 factory List<E>([int length = null]) { | 68 factory List<E>([int length = null]) { |
69 bool isFixed = true; | 69 bool isFixed = true; |
70 if (length === null) { | 70 if (length === null) { |
71 length = 0; | 71 length = 0; |
72 isFixed = false; | 72 isFixed = false; |
73 } else if (length < 0) { | 73 } else if (length < 0) { |
74 throw new IllegalArgumentException(length); | 74 throw new IllegalArgumentException("negative length $length"); |
75 } | 75 } |
76 // TODO(floitsch): make array creation more efficient. Currently we allocate | 76 // TODO(floitsch): make array creation more efficient. Currently we allocate |
77 // a new TypeToken at every allocation. Either we can optimize them away, | 77 // a new TypeToken at every allocation. Either we can optimize them away, |
78 // or we need to find other ways to pass type-information from Dart to JS. | 78 // or we need to find other ways to pass type-information from Dart to JS. |
79 ObjectArray list = _new(new TypeToken<E>(), length); | 79 ObjectArray list = _new(new TypeToken<E>(), length); |
80 list._isFixed = isFixed; | 80 list._isFixed = isFixed; |
81 return list; | 81 return list; |
82 } | 82 } |
83 | 83 |
84 static ObjectArray _new(TypeToken typeToken, int length) native; | 84 static ObjectArray _new(TypeToken typeToken, int length) native; |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 | 152 |
153 void setRange(int start, int length, List<T> from, [int startFrom = 0]) { | 153 void setRange(int start, int length, List<T> from, [int startFrom = 0]) { |
154 if (_isFixed) { | 154 if (_isFixed) { |
155 throw const UnsupportedOperationException( | 155 throw const UnsupportedOperationException( |
156 "Cannot remove range of a non-extendable array"); | 156 "Cannot remove range of a non-extendable array"); |
157 } | 157 } |
158 if (length == 0) { | 158 if (length == 0) { |
159 return; | 159 return; |
160 } | 160 } |
161 if (length < 0) { | 161 if (length < 0) { |
162 throw const IllegalArgumentException(); | 162 throw new IllegalArgumentException("negative length $length"); |
163 } | 163 } |
164 Arrays.copy(from, startFrom, this, start, length); | 164 Arrays.copy(from, startFrom, this, start, length); |
165 } | 165 } |
166 | 166 |
167 void removeRange(int start, int length) { | 167 void removeRange(int start, int length) { |
168 if (_isFixed) { | 168 if (_isFixed) { |
169 throw const UnsupportedOperationException( | 169 throw const UnsupportedOperationException( |
170 "Cannot remove range of a non-extendable array"); | 170 "Cannot remove range of a non-extendable array"); |
171 } | 171 } |
172 if (length == 0) { | 172 if (length == 0) { |
173 return; | 173 return; |
174 } | 174 } |
175 if (length < 0) { | 175 if (length < 0) { |
176 throw const IllegalArgumentException(); | 176 throw new IllegalArgumentException("negative length $length"); |
177 } | 177 } |
178 if (start < 0 || start >= this.length) { | 178 if (start < 0 || start >= this.length) { |
179 throw new IndexOutOfRangeException(start); | 179 throw new IndexOutOfRangeException(start); |
180 } | 180 } |
181 if (start + length > this.length) { | 181 if (start + length > this.length) { |
182 throw new IndexOutOfRangeException(start + length); | 182 throw new IndexOutOfRangeException(start + length); |
183 } | 183 } |
184 _splice(start, length); | 184 _splice(start, length); |
185 } | 185 } |
186 | 186 |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
303 } | 303 } |
304 | 304 |
305 static Array _newArray(int len) native { | 305 static Array _newArray(int len) native { |
306 return new Array(len); | 306 return new Array(len); |
307 } | 307 } |
308 | 308 |
309 static void _throwIndexOutOfRangeException(int index) native { | 309 static void _throwIndexOutOfRangeException(int index) native { |
310 throw new IndexOutOfRangeException(index); | 310 throw new IndexOutOfRangeException(index); |
311 } | 311 } |
312 } | 312 } |
OLD | NEW |