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

Side by Side Diff: pkg/serialization/lib/src/serialization_helpers.dart

Issue 11783009: Big merge from experimental to bleeding edge. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 11 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
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 /** 5 /**
6 * This contains extra functions and classes useful for implementing 6 * This contains extra functions and classes useful for implementing
7 * serialiation. Some or all of these will be removed once the functionality is 7 * serialiation. Some or all of these will be removed once the functionality is
8 * available in the core library. 8 * available in the core library.
9 */ 9 */
10 library serialization_helpers; 10 library serialization_helpers;
11 11
12 /** 12 /**
13 * A named function of one argument that just returns it. Useful for using 13 * A named function of one argument that just returns it. Useful for using
14 * as a default value for a function parameter or other places where you want 14 * as a default value for a function parameter or other places where you want
15 * to concisely provide a function that just returns its argument. 15 * to concisely provide a function that just returns its argument.
16 */ 16 */
17 doNothing(x) => x; 17 doNothing(x) => x;
18 18
19 /** Concatenate two lists. Handle the case where one or both might be null. */ 19 /** Concatenate two lists. Handle the case where one or both might be null. */
20 // TODO(alanknight): Remove once issue 5342 is resolved. 20 // TODO(alanknight): Remove once issue 5342 is resolved.
21 List append(List a, List b) { 21 List append(Iterable a, Iterable b) {
22 if (a == null) { 22 if (a == null) {
23 return (b == null) ? [] : new List.from(b); 23 return (b == null) ? [] : new List.from(b);
24 } 24 }
25 if (b == null) return new List.from(a); 25 if (b == null) return new List.from(a);
26 var result = new List.from(a); 26 var result = new List.from(a);
27 result.addAll(b); 27 result.addAll(b);
28 return result; 28 return result;
29 } 29 }
30 30
31 /** 31 /**
32 * Return a sorted version of [aCollection], using the default sort criterion. 32 * Return a sorted version of [anIterable], using the default sort criterion.
33 * Always returns a List, regardless of the type of [aCollection]. 33 * Always returns a List, regardless of the type of [anIterable].
34 */ 34 */
35 List sorted(aCollection) { 35 List sorted(anIterable) {
36 var result = new List.from(aCollection); 36 var result = new List.from(anIterable);
37 result.sort(); 37 result.sort();
38 return result; 38 return result;
39 } 39 }
40 40
41 /** Helper function for PrimitiveRule to tell which objects it applies to. */ 41 /** Helper function for PrimitiveRule to tell which objects it applies to. */
42 bool isPrimitive(object) { 42 bool isPrimitive(object) {
43 return identical(object, null) || object is num || object is String || 43 return identical(object, null) || object is num || object is String ||
44 identical(object, true) || identical(object, false); 44 identical(object, true) || identical(object, false);
45 } 45 }
46 46
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
98 MapLikeIterableForMap(collection) : super(collection); 98 MapLikeIterableForMap(collection) : super(collection);
99 99
100 void forEach(Function f) { collection.forEach(f);} 100 void forEach(Function f) { collection.forEach(f);}
101 Map copyEmpty() => new Map(); 101 Map copyEmpty() => new Map();
102 } 102 }
103 103
104 class MapLikeIterableForList extends MapLikeIterable { 104 class MapLikeIterableForList extends MapLikeIterable {
105 MapLikeIterableForList(collection) : super(collection); 105 MapLikeIterableForList(collection) : super(collection);
106 106
107 void forEach(f) { 107 void forEach(f) {
108 Iterator iterator = collection.iterator(); 108 Iterator iterator = collection.iterator;
109 for (var i = 0; i < collection.length; i++) { 109 for (var i = 0; i < collection.length; i++) {
110 f(i, iterator.next()); 110 iterator.moveNext();
111 f(i, iterator.current);
111 } 112 }
112 } 113 }
113 114
114 List copyEmpty() => new List(collection.length); 115 List copyEmpty() => new List(collection.length);
115 } 116 }
116 117
117 /** 118 /**
118 * An inverse of MapLikeIterable. Lets you iterate polymorphically between 119 * An inverse of MapLikeIterable. Lets you iterate polymorphically between
119 * List-like and Map-like things, but making them behave like Lists, instead 120 * List-like and Map-like things, but making them behave like Lists, instead
120 * of behaving like Maps. 121 * of behaving like Maps.
121 * So values(["a", "b", "c"]).forEach((value) => ...); 122 * So values(["a", "b", "c"]).forEach((value) => ...);
122 * will loop over the values "a", "b", "c", as if it were a List of values. 123 * will loop over the values "a", "b", "c", as if it were a List of values.
123 * Only supports forEach() and map() operations because that was all I needed 124 * Only supports forEach() and map() operations because that was all I needed
124 * for the moment. 125 * for the moment.
125 */ 126 */
126 values(x) { 127 values(x) {
127 if (x is Iterable) return x; 128 if (x is Iterable) return x;
128 if (x is Map) return new ListLikeIterable(x); 129 if (x is Map) return new ListLikeIterable(x);
129 throw new ArgumentError("Invalid argument"); 130 throw new ArgumentError("Invalid argument");
130 } 131 }
131 132
133 mapValues(x, f) {
134 if (x is Set) return x.mappedBy(f).toSet();
135 if (x is Iterable) return x.mappedBy(f).toList();
136 if (x is Map) return new ListLikeIterable(x).map(f);
137 throw new ArgumentError("Invalid argument");
138 }
139
132 /** 140 /**
133 * A class for iterating over things as if they were Lists, which primarily 141 * A class for iterating over things as if they were Lists, which primarily
134 * means that forEach passes one argument, and map() returns a new Map 142 * means that forEach passes one argument, and map() returns a new Map
135 * with the same keys as [collection] and values which haev been transformed 143 * with the same keys as [collection] and values which haev been transformed
136 * by the argument to map(). 144 * by the argument to map().
137 */ 145 */
138 class ListLikeIterable { 146 class ListLikeIterable {
139 ListLikeIterable(this.collection); 147 ListLikeIterable(this.collection);
140 final Map collection; 148 final Map collection;
141 149
(...skipping 10 matching lines...) Expand all
152 */ 160 */
153 map(Function f) { 161 map(Function f) {
154 var result = new Map(); 162 var result = new Map();
155 collection.forEach((key, value) => result[key] = f(value)); 163 collection.forEach((key, value) => result[key] = f(value));
156 return result; 164 return result;
157 } 165 }
158 166
159 /** 167 /**
160 * Return an iterator that behaves like a List iterator, taking one parameter. 168 * Return an iterator that behaves like a List iterator, taking one parameter.
161 */ 169 */
162 Iterator iterator() => collection.values.iterator(); 170 Iterator get iterator => collection.values.iterator;
163 } 171 }
164 172
165 /** 173 /**
166 * This acts as a stand-in for some value that cannot be hashed. We can't 174 * This acts as a stand-in for some value that cannot be hashed. We can't
167 * just use const Object() because the compiler will fold them together. 175 * just use const Object() because the compiler will fold them together.
168 */ 176 */
169 class _Sentinel { 177 class _Sentinel {
170 final _wrappedObject; 178 final _wrappedObject;
171 const _Sentinel(this._wrappedObject); 179 const _Sentinel(this._wrappedObject);
172 } 180 }
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
237 int get length => keys.length; 245 int get length => keys.length;
238 void clear() { 246 void clear() {
239 keys.clear(); 247 keys.clear();
240 values.clear(); 248 values.clear();
241 } 249 }
242 bool get isEmpty => keys.isEmpty; 250 bool get isEmpty => keys.isEmpty;
243 251
244 // Note that this is doing an equality comparison. 252 // Note that this is doing an equality comparison.
245 bool containsValue(x) => values.contains(x); 253 bool containsValue(x) => values.contains(x);
246 } 254 }
OLDNEW
« no previous file with comments | « pkg/serialization/lib/src/reader_writer.dart ('k') | pkg/serialization/lib/src/serialization_rule.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698