| OLD | NEW |
| (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 mdv_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 String toString() { |
| 36 var kind = isInsert ? 'insert' : isRemove ? 'remove' : 'set'; |
| 37 return '#<MapChangeRecord $kind $key>'; |
| 38 } |
| 39 } |
| 40 |
| 41 /** |
| 42 * Represents an observable map of model values. If any items are added, |
| 43 * removed, or replaced, then observers that are listening to [changes] |
| 44 * will be notified. |
| 45 */ |
| 46 class ObservableMap<K, V> extends ObservableBase implements Map<K, V> { |
| 47 static const _LENGTH = const Symbol('length'); |
| 48 |
| 49 final Map<K, V> _map; |
| 50 |
| 51 /** Creates an observable map. */ |
| 52 ObservableMap() : _map = new HashMap<K, V>(); |
| 53 |
| 54 /** Creates a new observable map using a [LinkedHashMap]. */ |
| 55 ObservableMap.linked() : _map = new LinkedHashMap<K, V>(); |
| 56 |
| 57 /** Creates a new observable map using a [SplayTreeMap]. */ |
| 58 ObservableMap.sorted() : _map = new SplayTreeMap<K, V>(); |
| 59 |
| 60 /** |
| 61 * Creates an observable map that contains all key value pairs of [other]. |
| 62 * It will attempt to use the same backing map type if the other map is a |
| 63 * [LinkedHashMap], [SplayTreeMap], or [HashMap]. Otherwise it defaults to |
| 64 * [HashMap]. |
| 65 * |
| 66 * Note this will perform a shallow conversion. If you want a deep conversion |
| 67 * you should use [toObservable]. |
| 68 */ |
| 69 factory ObservableMap.from(Map<K, V> other) { |
| 70 var result = new ObservableMap<K, V>._createFromType(other); |
| 71 other.forEach((K key, V value) { result[key] = value; }); |
| 72 return result; |
| 73 } |
| 74 |
| 75 factory ObservableMap._createFromType(Map<K, V> other) { |
| 76 ObservableMap result; |
| 77 if (other is SplayTreeMap) { |
| 78 result = new ObservableMap<K, V>.sorted(); |
| 79 } else if (other is LinkedHashMap) { |
| 80 result = new ObservableMap<K, V>.linked(); |
| 81 } else { |
| 82 result = new ObservableMap<K, V>(); |
| 83 } |
| 84 return result; |
| 85 } |
| 86 |
| 87 Iterable<K> get keys => _map.keys; |
| 88 |
| 89 Iterable<V> get values => _map.values; |
| 90 |
| 91 int get length =>_map.length; |
| 92 |
| 93 bool get isEmpty => length == 0; |
| 94 |
| 95 bool containsValue(V value) => _map.containsValue(value); |
| 96 |
| 97 bool containsKey(K key) => _map.containsKey(key); |
| 98 |
| 99 V operator [](K key) => _map[key]; |
| 100 |
| 101 void operator []=(K key, V value) { |
| 102 int len = _map.length; |
| 103 V oldValue = _map[key]; |
| 104 _map[key] = value; |
| 105 if (hasObservers) { |
| 106 if (len != _map.length) { |
| 107 notifyPropertyChange(_LENGTH, len, _map.length); |
| 108 notifyChange(new MapChangeRecord(key, isInsert: true)); |
| 109 } else if (!identical(oldValue, value)) { |
| 110 notifyChange(new MapChangeRecord(key)); |
| 111 } |
| 112 } |
| 113 } |
| 114 |
| 115 V putIfAbsent(K key, V ifAbsent()) { |
| 116 int len = _map.length; |
| 117 V result = _map.putIfAbsent(key, ifAbsent); |
| 118 if (hasObservers && len != _map.length) { |
| 119 notifyPropertyChange(_LENGTH, len, _map.length); |
| 120 notifyChange(new MapChangeRecord(key, isInsert: true)); |
| 121 } |
| 122 return result; |
| 123 } |
| 124 |
| 125 V remove(K key) { |
| 126 int len = _map.length; |
| 127 V result = _map.remove(key); |
| 128 if (hasObservers && len != _map.length) { |
| 129 notifyChange(new MapChangeRecord(key, isRemove: true)); |
| 130 notifyPropertyChange(_LENGTH, len, _map.length); |
| 131 } |
| 132 return result; |
| 133 } |
| 134 |
| 135 void clear() { |
| 136 int len = _map.length; |
| 137 if (hasObservers && len > 0) { |
| 138 _map.forEach((key, value) { |
| 139 notifyChange(new MapChangeRecord(key, isRemove: true)); |
| 140 }); |
| 141 notifyPropertyChange(_LENGTH, len, 0); |
| 142 } |
| 143 _map.clear(); |
| 144 } |
| 145 |
| 146 void forEach(void f(K key, V value)) => _map.forEach(f); |
| 147 |
| 148 String toString() => Maps.mapToString(this); |
| 149 } |
| OLD | NEW |