| 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 /** | |
| 6 * The [Collections] class implements static methods useful when | |
| 7 * writing a class that implements [Collection] and the [iterator] | |
| 8 * method. | |
| 9 */ | |
| 10 class Collections { | |
| 11 static bool contains(Iterable iterable, var element) { | |
| 12 for (final e in iterable) { | |
| 13 if (element == e) return true; | |
| 14 } | |
| 15 return false; | |
| 16 } | |
| 17 | |
| 18 static void forEach(Iterable iterable, void f(o)) { | |
| 19 for (final e in iterable) { | |
| 20 f(e); | |
| 21 } | |
| 22 } | |
| 23 | |
| 24 static bool some(Iterable iterable, bool f(o)) { | |
| 25 for (final e in iterable) { | |
| 26 if (f(e)) return true; | |
| 27 } | |
| 28 return false; | |
| 29 } | |
| 30 | |
| 31 static bool every(Iterable iterable, bool f(o)) { | |
| 32 for (final e in iterable) { | |
| 33 if (!f(e)) return false; | |
| 34 } | |
| 35 return true; | |
| 36 } | |
| 37 | |
| 38 static List map(Iterable source, List destination, f(o)) { | |
| 39 for (final e in source) { | |
| 40 destination.add(f(e)); | |
| 41 } | |
| 42 return destination; | |
| 43 } | |
| 44 | |
| 45 static Dynamic reduce(Iterable iterable, | |
| 46 Dynamic initialValue, | |
| 47 Dynamic combine(Dynamic previousValue, element)) { | |
| 48 for (final element in iterable) { | |
| 49 initialValue = combine(initialValue, element); | |
| 50 } | |
| 51 return initialValue; | |
| 52 } | |
| 53 | |
| 54 static List filter(Iterable source, List destination, bool f(o)) { | |
| 55 for (final e in source) { | |
| 56 if (f(e)) destination.add(e); | |
| 57 } | |
| 58 return destination; | |
| 59 } | |
| 60 | |
| 61 static bool isEmpty(Iterable iterable) { | |
| 62 return !iterable.iterator().hasNext; | |
| 63 } | |
| 64 | |
| 65 // TODO(jjb): visiting list should be an identityHashSet when it exists | |
| 66 | |
| 67 /** | |
| 68 * Returns a string representing the specified collection. If the | |
| 69 * collection is a [List], the returned string looks like this: | |
| 70 * [:'[element0, element1, ... elementN]':]. The value returned by its | |
| 71 * [toString] method is used to represent each element. If the specified | |
| 72 * collection is not a list, the returned string looks like this: | |
| 73 * [:{element0, element1, ... elementN}:]. In other words, the strings | |
| 74 * returned for lists are surrounded by square brackets, while the strings | |
| 75 * returned for other collections are surrounded by curly braces. | |
| 76 * | |
| 77 * If the specified collection contains a reference to itself, either | |
| 78 * directly or indirectly through other collections or maps, the contained | |
| 79 * reference is rendered as [:'[...]':] if it is a list, or [:'{...}':] if | |
| 80 * it is not. This prevents the infinite regress that would otherwise occur. | |
| 81 * So, for example, calling this method on a list whose sole element is a | |
| 82 * reference to itself would return [:'[[...]]':]. | |
| 83 * | |
| 84 * A typical implementation of a collection's [toString] method will | |
| 85 * simply return the results of this method applied to the collection. | |
| 86 */ | |
| 87 static String collectionToString(Collection c) { | |
| 88 var result = new StringBuffer(); | |
| 89 _emitCollection(c, result, new List()); | |
| 90 return result.toString(); | |
| 91 } | |
| 92 | |
| 93 /** | |
| 94 * Appends a string representing the specified collection to the specified | |
| 95 * string buffer. The string is formatted as per [collectionToString]. | |
| 96 * The [:visiting:] list contains references to all of the enclosing | |
| 97 * collections and maps (which are currently in the process of being | |
| 98 * emitted into [:result:]). The [:visiting:] parameter allows this method to | |
| 99 * generate a [:'[...]':] or [:'{...}':] where required. In other words, | |
| 100 * it allows this method and [_emitMap] to identify recursive collections | |
| 101 * and maps. | |
| 102 */ | |
| 103 static void _emitCollection(Collection c, StringBuffer result, List visiting)
{ | |
| 104 visiting.add(c); | |
| 105 bool isList = c is List; | |
| 106 result.add(isList ? '[' : '{'); | |
| 107 | |
| 108 bool first = true; | |
| 109 for (var e in c) { | |
| 110 if (!first) { | |
| 111 result.add(', '); | |
| 112 } | |
| 113 first = false; | |
| 114 _emitObject(e, result, visiting); | |
| 115 } | |
| 116 | |
| 117 result.add(isList ? ']' : '}'); | |
| 118 visiting.removeLast(); | |
| 119 } | |
| 120 | |
| 121 /** | |
| 122 * Appends a string representing the specified object to the specified | |
| 123 * string buffer. If the object is a [Collection] or [Map], it is formatted | |
| 124 * as per [collectionToString] or [mapToString]; otherwise, it is formatted | |
| 125 * by invoking its own [toString] method. | |
| 126 * | |
| 127 * The [:visiting:] list contains references to all of the enclosing | |
| 128 * collections and maps (which are currently in the process of being | |
| 129 * emitted into [:result:]). The [:visiting:] parameter allows this method | |
| 130 * to generate a [:'[...]':] or [:'{...}':] where required. In other words, | |
| 131 * it allows this method and [_emitCollection] to identify recursive maps | |
| 132 * and collections. | |
| 133 */ | |
| 134 static void _emitObject(Object o, StringBuffer result, List visiting) { | |
| 135 if (o is Collection) { | |
| 136 if (_containsRef(visiting, o)) { | |
| 137 result.add(o is List ? '[...]' : '{...}'); | |
| 138 } else { | |
| 139 _emitCollection(o, result, visiting); | |
| 140 } | |
| 141 } else if (o is Map) { | |
| 142 if (_containsRef(visiting, o)) { | |
| 143 result.add('{...}'); | |
| 144 } else { | |
| 145 Maps._emitMap(o, result, visiting); | |
| 146 } | |
| 147 } else { // o is neither a collection nor a map | |
| 148 result.add(o); | |
| 149 } | |
| 150 } | |
| 151 | |
| 152 /** | |
| 153 * Returns true if the specified collection contains the specified object | |
| 154 * reference. | |
| 155 */ | |
| 156 static _containsRef(Collection c, Object ref) { | |
| 157 for (var e in c) { | |
| 158 if (e === ref) return true; | |
| 159 } | |
| 160 return false; | |
| 161 } | |
| 162 } | |
| OLD | NEW |