| 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 part of dart.collection; | 5 part of dart.collection; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * This class provides default implementations for Iterables (including Lists). | 8 * The [Collections] class implements static methods useful when |
| 9 * | 9 * writing a class that implements [Collection] and the [iterator] |
| 10 * Once Dart receives Mixins it will be replaced with mixin classes. | 10 * method. |
| 11 */ | 11 */ |
| 12 class IterableMixinWorkaround { | 12 class Collections { |
| 13 static bool contains(Iterable iterable, var element) { | 13 static bool contains(Iterable iterable, var element) { |
| 14 for (final e in iterable) { | 14 for (final e in iterable) { |
| 15 if (element == e) return true; | 15 if (element == e) return true; |
| 16 } | 16 } |
| 17 return false; | 17 return false; |
| 18 } | 18 } |
| 19 | 19 |
| 20 static void forEach(Iterable iterable, void f(o)) { | 20 static void forEach(Iterable iterable, void f(o)) { |
| 21 for (final e in iterable) { | 21 for (final e in iterable) { |
| 22 f(e); | 22 f(e); |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 } else { | 179 } else { |
| 180 buffer.add("${iterator.current}"); | 180 buffer.add("${iterator.current}"); |
| 181 while (iterator.moveNext()) { | 181 while (iterator.moveNext()) { |
| 182 buffer.add(separator); | 182 buffer.add(separator); |
| 183 buffer.add("${iterator.current}"); | 183 buffer.add("${iterator.current}"); |
| 184 } | 184 } |
| 185 } | 185 } |
| 186 return buffer.toString(); | 186 return buffer.toString(); |
| 187 } | 187 } |
| 188 | 188 |
| 189 static String joinList(List list, [String separator]) { | 189 static String joinList(List<Object> list, [String separator]) { |
| 190 if (list.isEmpty) return ""; | 190 if (list.isEmpty) return ""; |
| 191 if (list.length == 1) return "${list[0]}"; | 191 if (list.length == 1) return "${list[0]}"; |
| 192 StringBuffer buffer = new StringBuffer(); | 192 StringBuffer buffer = new StringBuffer(); |
| 193 if (separator == null || separator == "") { | 193 if (separator == null || separator == "") { |
| 194 for (int i = 0; i < list.length; i++) { | 194 for (int i = 0; i < list.length; i++) { |
| 195 buffer.add("${list[i]}"); | 195 buffer.add("${list[i]}"); |
| 196 } | 196 } |
| 197 } else { | 197 } else { |
| 198 buffer.add("${list[0]}"); | 198 buffer.add("${list[0]}"); |
| 199 for (int i = 1; i < list.length; i++) { | 199 for (int i = 1; i < list.length; i++) { |
| 200 buffer.add(separator); | 200 buffer.add(separator); |
| 201 buffer.add("${list[i]}"); | 201 buffer.add("${list[i]}"); |
| 202 } | 202 } |
| 203 } | 203 } |
| 204 return buffer.toString(); | 204 return buffer.toString(); |
| 205 } | 205 } |
| 206 | 206 |
| 207 static Iterable where(Iterable iterable, bool f(var element)) { | |
| 208 return new WhereIterable(iterable, f); | |
| 209 } | |
| 210 | |
| 211 static List mappedByList(List list, f(var element)) { | |
| 212 return new MappedList(list, f); | |
| 213 } | |
| 214 | |
| 215 static List takeList(List list, int n) { | |
| 216 // The generic type is currently lost. It will be fixed with mixins. | |
| 217 return new ListView(list, 0, n); | |
| 218 } | |
| 219 | |
| 220 static Iterable takeWhile(Iterable iterable, bool test(var value)) { | |
| 221 // The generic type is currently lost. It will be fixed with mixins. | |
| 222 return new TakeWhileIterable(iterable, test); | |
| 223 } | |
| 224 | |
| 225 static List skipList(List list, int n) { | |
| 226 // The generic type is currently lost. It will be fixed with mixins. | |
| 227 return new ListView(list, n, null); | |
| 228 } | |
| 229 | |
| 230 static Iterable skipWhile(Iterable iterable, bool test(var value)) { | |
| 231 // The generic type is currently lost. It will be fixed with mixins. | |
| 232 return new SkipWhileIterable(iterable, test); | |
| 233 } | |
| 234 } | |
| 235 | |
| 236 /** | |
| 237 * The [Collections] class implements static methods useful when | |
| 238 * writing a class that implements [Collection] and the [iterator] | |
| 239 * method. | |
| 240 */ | |
| 241 class Collections { | |
| 242 /** Deprecated. Use the same method in [IterableMixinWorkaround] instead.*/ | |
| 243 static bool contains(Iterable iterable, var element) | |
| 244 => IterableMixinWorkaround.contains(iterable, element); | |
| 245 | |
| 246 /** Deprecated. Use the same method in [IterableMixinWorkaround] instead.*/ | |
| 247 static void forEach(Iterable iterable, void f(o)) { | |
| 248 IterableMixinWorkaround.forEach(iterable, f); | |
| 249 } | |
| 250 | |
| 251 /** Deprecated. Use the same method in [IterableMixinWorkaround] instead.*/ | |
| 252 static bool any(Iterable iterable, bool f(o)) | |
| 253 => IterableMixinWorkaround.any(iterable, f); | |
| 254 | |
| 255 /** Deprecated. Use the same method in [IterableMixinWorkaround] instead.*/ | |
| 256 static bool every(Iterable iterable, bool f(o)) | |
| 257 => IterableMixinWorkaround.every(iterable, f); | |
| 258 | |
| 259 /** Deprecated. Use the same method in [IterableMixinWorkaround] instead.*/ | |
| 260 static dynamic reduce(Iterable iterable, | |
| 261 dynamic initialValue, | |
| 262 dynamic combine(dynamic previousValue, element)) | |
| 263 => IterableMixinWorkaround.reduce(iterable, initialValue, combine); | |
| 264 | |
| 265 /** Deprecated. Use the same method in [IterableMixinWorkaround] instead.*/ | |
| 266 static bool isEmpty(Iterable iterable) | |
| 267 => IterableMixinWorkaround.isEmpty(iterable); | |
| 268 | |
| 269 /** Deprecated. Use the same method in [IterableMixinWorkaround] instead.*/ | |
| 270 static dynamic first(Iterable iterable) | |
| 271 => IterableMixinWorkaround.first(iterable); | |
| 272 | |
| 273 /** Deprecated. Use the same method in [IterableMixinWorkaround] instead.*/ | |
| 274 static dynamic last(Iterable iterable) | |
| 275 => IterableMixinWorkaround.last(iterable); | |
| 276 | |
| 277 /** Deprecated. Use the same method in [IterableMixinWorkaround] instead.*/ | |
| 278 static dynamic min(Iterable iterable, [int compare(var a, var b)]) | |
| 279 => IterableMixinWorkaround.min(iterable, compare); | |
| 280 | |
| 281 /** Deprecated. Use the same method in [IterableMixinWorkaround] instead.*/ | |
| 282 static dynamic max(Iterable iterable, [int compare(var a, var b)]) | |
| 283 => IterableMixinWorkaround.max(iterable, compare); | |
| 284 | |
| 285 /** Deprecated. Use the same method in [IterableMixinWorkaround] instead.*/ | |
| 286 static dynamic single(Iterable iterable) | |
| 287 => IterableMixinWorkaround.single(iterable); | |
| 288 | |
| 289 /** Deprecated. Use the same method in [IterableMixinWorkaround] instead.*/ | |
| 290 static dynamic firstMatching(Iterable iterable, | |
| 291 bool test(dynamic value), | |
| 292 dynamic orElse()) | |
| 293 => IterableMixinWorkaround.firstMatching(iterable, test, orElse); | |
| 294 | |
| 295 /** Deprecated. Use the same method in [IterableMixinWorkaround] instead.*/ | |
| 296 static dynamic lastMatching(Iterable iterable, | |
| 297 bool test(dynamic value), | |
| 298 dynamic orElse()) | |
| 299 => IterableMixinWorkaround.lastMatching(iterable, test, orElse); | |
| 300 | |
| 301 /** Deprecated. Use the same method in [IterableMixinWorkaround] instead.*/ | |
| 302 static dynamic lastMatchingInList(List list, | |
| 303 bool test(dynamic value), | |
| 304 dynamic orElse()) | |
| 305 => IterableMixinWorkaround.lastMatchingInList(list, test, orElse); | |
| 306 | |
| 307 /** Deprecated. Use the same method in [IterableMixinWorkaround] instead.*/ | |
| 308 static dynamic singleMatching(Iterable iterable, bool test(dynamic value)) | |
| 309 => IterableMixinWorkaround.singleMatching(iterable, test); | |
| 310 | |
| 311 /** Deprecated. Use the same method in [IterableMixinWorkaround] instead.*/ | |
| 312 static dynamic elementAt(Iterable iterable, int index) | |
| 313 => IterableMixinWorkaround.elementAt(iterable, index); | |
| 314 | |
| 315 /** Deprecated. Use the same method in [IterableMixinWorkaround] instead.*/ | |
| 316 static String join(Iterable iterable, [String separator]) | |
| 317 => IterableMixinWorkaround.join(iterable, separator); | |
| 318 | |
| 319 /** Deprecated. Use the same method in [IterableMixinWorkaround] instead.*/ | |
| 320 static String joinList(List list, [String separator]) | |
| 321 => IterableMixinWorkaround.joinList(list, separator); | |
| 322 | |
| 323 /** Deprecated. Use the same method in [IterableMixinWorkaround] instead.*/ | |
| 324 static Iterable where(Iterable iterable, bool f(var element)) | |
| 325 => IterableMixinWorkaround.where(iterable, f); | |
| 326 | |
| 327 /** Deprecated. Use the same method in [IterableMixinWorkaround] instead.*/ | |
| 328 static List mappedByList(List list, f(var element)) | |
| 329 => IterableMixinWorkaround.mappedByList(list, f); | |
| 330 | |
| 331 /** Deprecated. Use the same method in [IterableMixinWorkaround] instead.*/ | |
| 332 static List takeList(List list, int n) | |
| 333 => IterableMixinWorkaround.takeList(list, n); | |
| 334 | |
| 335 /** Deprecated. Use the same method in [IterableMixinWorkaround] instead.*/ | |
| 336 static Iterable takeWhile(Iterable iterable, bool test(var value)) | |
| 337 => IterableMixinWorkaround.takeWhile(iterable, test); | |
| 338 | |
| 339 /** Deprecated. Use the same method in [IterableMixinWorkaround] instead.*/ | |
| 340 static List skipList(List list, int n) | |
| 341 => IterableMixinWorkaround.skipList(list, n); | |
| 342 | |
| 343 /** Deprecated. Use the same method in [IterableMixinWorkaround] instead.*/ | |
| 344 static Iterable skipWhile(Iterable iterable, bool test(var value)) | |
| 345 => IterableMixinWorkaround.skipWhile(iterable, test); | |
| 346 | |
| 347 // TODO(jjb): visiting list should be an identityHashSet when it exists | 207 // TODO(jjb): visiting list should be an identityHashSet when it exists |
| 348 | 208 |
| 349 /** | 209 /** |
| 350 * Returns a string representing the specified collection. If the | 210 * Returns a string representing the specified collection. If the |
| 351 * collection is a [List], the returned string looks like this: | 211 * collection is a [List], the returned string looks like this: |
| 352 * [:'[element0, element1, ... elementN]':]. The value returned by its | 212 * [:'[element0, element1, ... elementN]':]. The value returned by its |
| 353 * [toString] method is used to represent each element. If the specified | 213 * [toString] method is used to represent each element. If the specified |
| 354 * collection is not a list, the returned string looks like this: | 214 * collection is not a list, the returned string looks like this: |
| 355 * [:{element0, element1, ... elementN}:]. In other words, the strings | 215 * [:{element0, element1, ... elementN}:]. In other words, the strings |
| 356 * returned for lists are surrounded by square brackets, while the strings | 216 * returned for lists are surrounded by square brackets, while the strings |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 437 * Returns true if the specified collection contains the specified object | 297 * Returns true if the specified collection contains the specified object |
| 438 * reference. | 298 * reference. |
| 439 */ | 299 */ |
| 440 static _containsRef(Collection c, Object ref) { | 300 static _containsRef(Collection c, Object ref) { |
| 441 for (var e in c) { | 301 for (var e in c) { |
| 442 if (identical(e, ref)) return true; | 302 if (identical(e, ref)) return true; |
| 443 } | 303 } |
| 444 return false; | 304 return false; |
| 445 } | 305 } |
| 446 } | 306 } |
| OLD | NEW |