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

Side by Side Diff: lib/dartdoc/mirrors/util.dart

Issue 10692040: Mirrors prototype added to dartdoc. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Utility libraries added Created 8 years, 5 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
(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 #library('util');
6
7 /**
8 * An abstract map implementation. This class can be used as a superclass for
9 * implementing maps, requiring only the further implementation of the
10 * [:operator []:], [:forEach:] and [:length:] methods to provide a fully
11 * implemented immutable map.
Lasse Reichstein Nielsen 2012/07/04 10:58:59 That's what the Map class itself should be! Eventu
Johnni Winther 2012/07/04 13:19:17 Yes, an iterator is wanted/needed.
12 */
13 abstract class AbstractMap<K,V> implements Map<K,V> {
14 AbstractMap();
15 AbstractMap.from(Map<K,V> other) {
16 other.forEach((k,v) => this[k] = v);
17 }
18 void operator []=(K key, value) {
19 throw new UnsupportedOperationException('[]= is not supported');
20 }
21 void clear() {
22 throw new UnsupportedOperationException('clear() is not supported');
23 }
24 bool containsKey(K key) {
25 var found = false;
26 forEach((k,_) {
27 if (k == key) {
28 found = true;
29 }
30 });
31 return found;
32 }
33 bool containsValue(V value) {
34 var found = false;
35 forEach((_,v) {
36 if (v == value) {
37 found = true;
38 }
39 });
40 return found;
41 }
42 Collection<K> getKeys() {
43 var keys = <K>[];
44 forEach((k,_) => keys.add(k));
45 return keys;
46 }
47 Collection<V> getValues() {
48 var values = <V>[];
49 forEach((_,v) => values.add(v));
50 return values;
51 }
52 bool isEmpty() => length == 0;
53 V putIfAbsent(K key, V ifAbsent()) {
54 if (!containsKey(key)) {
55 V value = this[key];
56 this[key] = ifAbsent();
57 return value;
58 }
59 return null;
60 }
61 V remove(K key) {
62 throw new UnsupportedOperationException('V remove(K key) is not supported');
63 }
64 }
65
66 /**
67 * [ImmutableMapWrapper] wraps a (mutable) map as an immutable map where all
68 * mutable operations throw [UnsupportedOperationException] upon invocation.
Lasse Reichstein Nielsen 2012/07/04 10:58:59 "mutable operations" -> "mutating operations"
Johnni Winther 2012/07/04 13:19:17 Done.
69 */
70 class ImmutableMapWrapper<K,V> extends AbstractMap<K,V> {
71 final Map<K,V> _map;
72
73 ImmutableMapWrapper(this._map);
74
75 int get length() => _map.length;
76
77 V operator [](K key) {
78 if (key is K) {
79 return _map[key];
80 }
81 return null;
82 }
83
84 void forEach(void f(K key, V value)) {
85 _map.forEach(f);
86 }
87 }
88
89 /**
90 * A [Filter] function returns [:true:] iff [value] should be included.
91 */
92 typedef bool Filter<V>(V value);
93
94 /**
95 * An immutable map wrapper capable of filtering the input map.
96 */
97 class FilteredImmutableMapWrapper<K,V> extends ImmutableMapWrapper<K,V> {
Lasse Reichstein Nielsen 2012/07/04 10:58:59 ImmutableValueFilterMap? I don't see the need for
98 final Filter<V> _filter;
99
100 FilteredImmutableMapWrapper(Map<K,V> map, this._filter) : super(map);
101
102 int get length() {
103 var count = 0;
104 forEach((k,v) {
105 count++;
106 });
107 return count;
108 }
109
110 void forEach(void f(K key, V value)) {
111 _map.forEach((K k, V v) {
112 if (_filter(v)) {
113 f(k, v);
114 }
115 });
116 }
117 }
118
119 /**
120 * An [AsFilter] takes a [value] of type [V1] and returns [value] iff it is of
121 * type [V2] or [:null:] otherwise. An [AsFilter] therefore behaves like the
122 * [:as:] expression.
123 */
124 typedef V2 AsFilter<V1,V2>(V1 value);
Lasse Reichstein Nielsen 2012/07/04 10:58:59 Space after comma, evertwhere.
Johnni Winther 2012/07/04 13:19:17 Done.
125
126 /**
127 * An immutable map wrapper capable of filtering the input map based on types.
Lasse Reichstein Nielsen 2012/07/04 10:58:59 Tricky. Might need more comments.
Johnni Winther 2012/07/04 13:19:17 Done. (Or tried!)
128 */
129 class AsFilteredImmutableMapWrapper<K,Vin,Vout> extends AbstractMap<K,Vout> {
Lasse Reichstein Nielsen 2012/07/04 10:58:59 The "filter" method don't need to do a cast. It ca
Johnni Winther 2012/07/04 13:19:17 The filter method could be used for other purposes
130 final Map<K,Vin> _map;
131 final AsFilter<Vin,Vout> _filter;
132
133 AsFilteredImmutableMapWrapper(this._map, this._filter);
Lasse Reichstein Nielsen 2012/07/04 10:58:59 Indentation is one too deep from here.
Johnni Winther 2012/07/04 13:19:17 Done.
134
Lasse Reichstein Nielsen 2012/07/04 10:58:59 Should you override containsKey/containsValue to r
Johnni Winther 2012/07/04 13:19:17 It works with the current implementation of Abstra
135 int get length() {
136 var count = 0;
137 forEach((k,v) {
138 count++;
139 });
140 return count;
141 }
142
143 Vout operator [](K key) {
144 if (key is K) {
145 return _filter(_map[key]);
146 }
147 return null;
148 }
149
150 void forEach(void f(K key, Vout value)) {
151 _map.forEach((K k, Vin v) {
152 var value = _filter(v);
153 if (value !== null) {
154 f(k, value);
155 }
156 });
157 }
158 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698