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