Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(233)

Side by Side Diff: sdk/lib/collection/iterable.dart

Issue 262803003: Unify error messages for iterables and lists. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Capitalize messages. Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/lib/growable_array.dart ('k') | sdk/lib/internal/iterable.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 part of dart.collection; 5 part of dart.collection;
6 6
7 /** 7 /**
8 * This [Iterable] mixin implements all [Iterable] members except `iterator`. 8 * This [Iterable] mixin implements all [Iterable] members except `iterator`.
9 * 9 *
10 * All other methods are implemented in terms of `iterator`. 10 * All other methods are implemented in terms of `iterator`.
(...skipping 13 matching lines...) Expand all
24 return false; 24 return false;
25 } 25 }
26 26
27 void forEach(void f(E element)) { 27 void forEach(void f(E element)) {
28 for (E element in this) f(element); 28 for (E element in this) f(element);
29 } 29 }
30 30
31 E reduce(E combine(E value, E element)) { 31 E reduce(E combine(E value, E element)) {
32 Iterator<E> iterator = this.iterator; 32 Iterator<E> iterator = this.iterator;
33 if (!iterator.moveNext()) { 33 if (!iterator.moveNext()) {
34 throw new StateError("No elements"); 34 throw IterableElementError.noElement();
35 } 35 }
36 E value = iterator.current; 36 E value = iterator.current;
37 while (iterator.moveNext()) { 37 while (iterator.moveNext()) {
38 value = combine(value, iterator.current); 38 value = combine(value, iterator.current);
39 } 39 }
40 return value; 40 return value;
41 } 41 }
42 42
43 dynamic fold(var initialValue, 43 dynamic fold(var initialValue,
44 dynamic combine(var previousValue, E element)) { 44 dynamic combine(var previousValue, E element)) {
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 return new SkipIterable<E>(this, n); 110 return new SkipIterable<E>(this, n);
111 } 111 }
112 112
113 Iterable<E> skipWhile(bool test(E value)) { 113 Iterable<E> skipWhile(bool test(E value)) {
114 return new SkipWhileIterable<E>(this, test); 114 return new SkipWhileIterable<E>(this, test);
115 } 115 }
116 116
117 E get first { 117 E get first {
118 Iterator it = iterator; 118 Iterator it = iterator;
119 if (!it.moveNext()) { 119 if (!it.moveNext()) {
120 throw new StateError("No elements"); 120 throw IterableElementError.noElement();
121 } 121 }
122 return it.current; 122 return it.current;
123 } 123 }
124 124
125 E get last { 125 E get last {
126 Iterator it = iterator; 126 Iterator it = iterator;
127 if (!it.moveNext()) { 127 if (!it.moveNext()) {
128 throw new StateError("No elements"); 128 throw IterableElementError.noElement();
129 } 129 }
130 E result; 130 E result;
131 do { 131 do {
132 result = it.current; 132 result = it.current;
133 } while(it.moveNext()); 133 } while(it.moveNext());
134 return result; 134 return result;
135 } 135 }
136 136
137 E get single { 137 E get single {
138 Iterator it = iterator; 138 Iterator it = iterator;
139 if (!it.moveNext()) throw new StateError("No elements"); 139 if (!it.moveNext()) throw IterableElementError.noElement();
140 E result = it.current; 140 E result = it.current;
141 if (it.moveNext()) throw new StateError("More than one element"); 141 if (it.moveNext()) throw IterableElementError.tooMany();
142 return result; 142 return result;
143 } 143 }
144 144
145 dynamic firstWhere(bool test(E value), { Object orElse() }) { 145 dynamic firstWhere(bool test(E value), { Object orElse() }) {
146 for (E element in this) { 146 for (E element in this) {
147 if (test(element)) return element; 147 if (test(element)) return element;
148 } 148 }
149 if (orElse != null) return orElse(); 149 if (orElse != null) return orElse();
150 throw new StateError("No matching element"); 150 throw IterableElementError.noElement();
151 } 151 }
152 152
153 dynamic lastWhere(bool test(E value), { Object orElse() }) { 153 dynamic lastWhere(bool test(E value), { Object orElse() }) {
154 E result = null; 154 E result = null;
155 bool foundMatching = false; 155 bool foundMatching = false;
156 for (E element in this) { 156 for (E element in this) {
157 if (test(element)) { 157 if (test(element)) {
158 result = element; 158 result = element;
159 foundMatching = true; 159 foundMatching = true;
160 } 160 }
161 } 161 }
162 if (foundMatching) return result; 162 if (foundMatching) return result;
163 if (orElse != null) return orElse(); 163 if (orElse != null) return orElse();
164 throw new StateError("No matching element"); 164 throw IterableElementError.noElement();
165 } 165 }
166 166
167 E singleWhere(bool test(E value)) { 167 E singleWhere(bool test(E value)) {
168 E result = null; 168 E result = null;
169 bool foundMatching = false; 169 bool foundMatching = false;
170 for (E element in this) { 170 for (E element in this) {
171 if (test(element)) { 171 if (test(element)) {
172 if (foundMatching) { 172 if (foundMatching) {
173 throw new StateError("More than one matching element"); 173 throw IterableElementError.tooMany();
174 } 174 }
175 result = element; 175 result = element;
176 foundMatching = true; 176 foundMatching = true;
177 } 177 }
178 } 178 }
179 if (foundMatching) return result; 179 if (foundMatching) return result;
180 throw new StateError("No matching element"); 180 throw IterableElementError.noElement();
181 } 181 }
182 182
183 E elementAt(int index) { 183 E elementAt(int index) {
184 if (index is! int || index < 0) throw new RangeError.value(index); 184 if (index is! int || index < 0) throw new RangeError.value(index);
185 int remaining = index; 185 int remaining = index;
186 for (E element in this) { 186 for (E element in this) {
187 if (remaining == 0) return element; 187 if (remaining == 0) return element;
188 remaining--; 188 remaining--;
189 } 189 }
190 throw new RangeError.value(index); 190 throw new RangeError.value(index);
(...skipping 27 matching lines...) Expand all
218 return false; 218 return false;
219 } 219 }
220 220
221 void forEach(void f(E element)) { 221 void forEach(void f(E element)) {
222 for (E element in this) f(element); 222 for (E element in this) f(element);
223 } 223 }
224 224
225 E reduce(E combine(E value, E element)) { 225 E reduce(E combine(E value, E element)) {
226 Iterator<E> iterator = this.iterator; 226 Iterator<E> iterator = this.iterator;
227 if (!iterator.moveNext()) { 227 if (!iterator.moveNext()) {
228 throw new StateError("No elements"); 228 throw IterableElementError.noElement();
229 } 229 }
230 E value = iterator.current; 230 E value = iterator.current;
231 while (iterator.moveNext()) { 231 while (iterator.moveNext()) {
232 value = combine(value, iterator.current); 232 value = combine(value, iterator.current);
233 } 233 }
234 return value; 234 return value;
235 } 235 }
236 236
237 dynamic fold(var initialValue, 237 dynamic fold(var initialValue,
238 dynamic combine(var previousValue, E element)) { 238 dynamic combine(var previousValue, E element)) {
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
304 return new SkipIterable<E>(this, n); 304 return new SkipIterable<E>(this, n);
305 } 305 }
306 306
307 Iterable<E> skipWhile(bool test(E value)) { 307 Iterable<E> skipWhile(bool test(E value)) {
308 return new SkipWhileIterable<E>(this, test); 308 return new SkipWhileIterable<E>(this, test);
309 } 309 }
310 310
311 E get first { 311 E get first {
312 Iterator it = iterator; 312 Iterator it = iterator;
313 if (!it.moveNext()) { 313 if (!it.moveNext()) {
314 throw new StateError("No elements"); 314 throw IterableElementError.noElement();
315 } 315 }
316 return it.current; 316 return it.current;
317 } 317 }
318 318
319 E get last { 319 E get last {
320 Iterator it = iterator; 320 Iterator it = iterator;
321 if (!it.moveNext()) { 321 if (!it.moveNext()) {
322 throw new StateError("No elements"); 322 throw IterableElementError.noElement();
323 } 323 }
324 E result; 324 E result;
325 do { 325 do {
326 result = it.current; 326 result = it.current;
327 } while(it.moveNext()); 327 } while(it.moveNext());
328 return result; 328 return result;
329 } 329 }
330 330
331 E get single { 331 E get single {
332 Iterator it = iterator; 332 Iterator it = iterator;
333 if (!it.moveNext()) throw new StateError("No elements"); 333 if (!it.moveNext()) throw IterableElementError.noElement();
334 E result = it.current; 334 E result = it.current;
335 if (it.moveNext()) throw new StateError("More than one element"); 335 if (it.moveNext()) throw IterableElementError.tooMany();
336 return result; 336 return result;
337 } 337 }
338 338
339 dynamic firstWhere(bool test(E value), { Object orElse() }) { 339 dynamic firstWhere(bool test(E value), { Object orElse() }) {
340 for (E element in this) { 340 for (E element in this) {
341 if (test(element)) return element; 341 if (test(element)) return element;
342 } 342 }
343 if (orElse != null) return orElse(); 343 if (orElse != null) return orElse();
344 throw new StateError("No matching element"); 344 throw IterableElementError.noElement();
345 } 345 }
346 346
347 dynamic lastWhere(bool test(E value), { Object orElse() }) { 347 dynamic lastWhere(bool test(E value), { Object orElse() }) {
348 E result = null; 348 E result = null;
349 bool foundMatching = false; 349 bool foundMatching = false;
350 for (E element in this) { 350 for (E element in this) {
351 if (test(element)) { 351 if (test(element)) {
352 result = element; 352 result = element;
353 foundMatching = true; 353 foundMatching = true;
354 } 354 }
355 } 355 }
356 if (foundMatching) return result; 356 if (foundMatching) return result;
357 if (orElse != null) return orElse(); 357 if (orElse != null) return orElse();
358 throw new StateError("No matching element"); 358 throw IterableElementError.noElement();
359 } 359 }
360 360
361 E singleWhere(bool test(E value)) { 361 E singleWhere(bool test(E value)) {
362 E result = null; 362 E result = null;
363 bool foundMatching = false; 363 bool foundMatching = false;
364 for (E element in this) { 364 for (E element in this) {
365 if (test(element)) { 365 if (test(element)) {
366 if (foundMatching) { 366 if (foundMatching) {
367 throw new StateError("More than one matching element"); 367 throw IterableElementError.tooMany();
368 } 368 }
369 result = element; 369 result = element;
370 foundMatching = true; 370 foundMatching = true;
371 } 371 }
372 } 372 }
373 if (foundMatching) return result; 373 if (foundMatching) return result;
374 throw new StateError("No matching element"); 374 throw IterableElementError.noElement();
375 } 375 }
376 376
377 E elementAt(int index) { 377 E elementAt(int index) {
378 if (index is! int || index < 0) throw new RangeError.value(index); 378 if (index is! int || index < 0) throw new RangeError.value(index);
379 int remaining = index; 379 int remaining = index;
380 for (E element in this) { 380 for (E element in this) {
381 if (remaining == 0) return element; 381 if (remaining == 0) return element;
382 remaining--; 382 remaining--;
383 } 383 }
384 throw new RangeError.value(index); 384 throw new RangeError.value(index);
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
516 elision = "..."; 516 elision = "...";
517 length += ELLIPSIS_SIZE + OVERHEAD; 517 length += ELLIPSIS_SIZE + OVERHEAD;
518 } 518 }
519 } 519 }
520 if (elision != null) { 520 if (elision != null) {
521 parts.add(elision); 521 parts.add(elision);
522 } 522 }
523 parts.add(penultimateString); 523 parts.add(penultimateString);
524 parts.add(ultimateString); 524 parts.add(ultimateString);
525 } 525 }
OLDNEW
« no previous file with comments | « runtime/lib/growable_array.dart ('k') | sdk/lib/internal/iterable.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698