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

Side by Side Diff: pkg/observe/lib/src/observable_map.dart

Issue 14732003: Implement Model-Driven-Views spec for Dart (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: comment tweaks Created 7 years, 7 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) 2013, 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 part of observe;
6
7 // TODO(jmesserly): this needs to be faster. We currently require multiple
8 // lookups per key to get the old value.
9 // TODO(jmesserly): this doesn't implement the precise interfaces like
10 // LinkedHashMap, SplayTreeMap or HashMap. However it can use them for the
11 // backing store.
12
13 // TODO(jmesserly): should we summarize map changes like we do for list changes?
14 class MapChangeRecord extends ChangeRecord {
15 /** The map key that changed. */
16 final key;
17
18 // TODO(jmesserly): we could store this more compactly if it matters.
19 /** True if this key was inserted. */
20 final bool isInsert;
21
22 /** True if this key was removed. */
23 final bool isRemove;
24
25 MapChangeRecord(this.key, {this.isInsert: false, this.isRemove: false}) {
26 if (isInsert && isRemove) {
27 throw new ArgumentError(
28 '$key cannot be inserted and removed in the same change');
29 }
30 }
31
32 // Use == on the key, to match equality semantics of most Maps.
33 bool changes(otherKey) => key == otherKey;
34 }
35
36 /**
37 * Represents an observable map of model values. If any items are added,
38 * removed, or replaced, then observers that are listening to [changes]
39 * will be notified.
40 */
41 class ObservableMap<K, V> extends ObservableBase implements Map<K, V> {
42 static const _LENGTH = const Symbol('length');
43
44 final Map<K, V> _map;
45
46 /** Creates an observable map. */
47 ObservableMap() : _map = new HashMap<K, V>();
48
49 /** Creates a new observable map using a [LinkedHashMap]. */
50 ObservableMap.linked() : _map = new LinkedHashMap<K, V>();
51
52 /** Creates a new observable map using a [SplayTreeMap]. */
53 ObservableMap.sorted() : _map = new SplayTreeMap<K, V>();
54
55 /**
56 * Creates an observable map that contains all key value pairs of [other].
57 * It will attempt to use the same backing map type if the other map is a
58 * [LinkedHashMap], [SplayTreeMap], or [HashMap]. Otherwise it defaults to
59 * [HashMap].
60 *
61 * Note this will perform a shallow conversion. If you want a deep conversion
62 * you should use [toObservable].
63 */
64 factory ObservableMap.from(Map<K, V> other) {
65 var result = new ObservableMap<K, V>._createFromType(other);
66 other.forEach((K key, V value) { result[key] = value; });
67 return result;
68 }
69
70 factory ObservableMap._createFromType(Map<K, V> other) {
71 ObservableMap result;
72 if (other is SplayTreeMap) {
73 result = new ObservableMap<K, V>.sorted();
74 } else if (other is LinkedHashMap) {
75 result = new ObservableMap<K, V>.linked();
76 } else {
77 result = new ObservableMap<K, V>();
78 }
79 return result;
80 }
81
82 Iterable<K> get keys => _map.keys;
83
84 Iterable<V> get values => _map.values;
85
86 int get length =>_map.length;
87
88 bool get isEmpty => length == 0;
89
90 bool containsValue(V value) => _map.containsValue(value);
91
92 bool containsKey(K key) => _map.containsKey(key);
93
94 V operator [](K key) => _map[key];
95
96 void operator []=(K key, V value) {
97 int len = _map.length;
98 V oldValue = _map[key];
99 _map[key] = value;
100 if (hasObservers) {
101 if (len != _map.length) {
102 notifyField(_LENGTH, len, _map.length);
103 notifyChange(new MapChangeRecord(key, isInsert: true));
104 } else if (!identical(oldValue, value)) {
105 notifyChange(new MapChangeRecord(key));
106 }
107 }
108 }
109
110 V putIfAbsent(K key, V ifAbsent()) {
111 int len = _map.length;
112 V result = _map.putIfAbsent(key, ifAbsent);
113 if (hasObservers && len != _map.length) {
114 notifyField(_LENGTH, len, _map.length);
115 notifyChange(new MapChangeRecord(key, isInsert: true));
116 }
117 return result;
118 }
119
120 V remove(K key) {
121 int len = _map.length;
122 V result = _map.remove(key);
123 if (hasObservers && len != _map.length) {
124 notifyChange(new MapChangeRecord(key, isRemove: true));
125 notifyField(_LENGTH, len, _map.length);
126 }
127 return result;
128 }
129
130 void clear() {
131 int len = _map.length;
132 if (hasObservers && len > 0) {
133 _map.forEach((key, value) {
134 notifyChange(new MapChangeRecord(key, isRemove: true));
135 });
136 notifyField(_LENGTH, len, 0);
137 }
138 _map.clear();
139 }
140
141 void forEach(void f(K key, V value)) => _map.forEach(f);
142
143 String toString() => Maps.mapToString(this);
144 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698