| 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 get isNotEmpty => !isEmpty; | |
| 96 | |
| 97 bool containsValue(Object value) => _map.containsValue(value); | |
| 98 | |
| 99 bool containsKey(Object key) => _map.containsKey(key); | |
| 100 | |
| 101 V operator [](Object key) => _map[key]; | |
| 102 | |
| 103 void operator []=(K key, V value) { | |
| 104 int len = _map.length; | |
| 105 V oldValue = _map[key]; | |
| 106 _map[key] = value; | |
| 107 if (hasObservers) { | |
| 108 if (len != _map.length) { | |
| 109 notifyPropertyChange(_LENGTH, len, _map.length); | |
| 110 notifyChange(new MapChangeRecord(key, isInsert: true)); | |
| 111 } else if (!identical(oldValue, value)) { | |
| 112 notifyChange(new MapChangeRecord(key)); | |
| 113 } | |
| 114 } | |
| 115 } | |
| 116 | |
| 117 V putIfAbsent(K key, V ifAbsent()) { | |
| 118 int len = _map.length; | |
| 119 V result = _map.putIfAbsent(key, ifAbsent); | |
| 120 if (hasObservers && len != _map.length) { | |
| 121 notifyPropertyChange(_LENGTH, len, _map.length); | |
| 122 notifyChange(new MapChangeRecord(key, isInsert: true)); | |
| 123 } | |
| 124 return result; | |
| 125 } | |
| 126 | |
| 127 V remove(Object key) { | |
| 128 int len = _map.length; | |
| 129 V result = _map.remove(key); | |
| 130 if (hasObservers && len != _map.length) { | |
| 131 notifyChange(new MapChangeRecord(key, isRemove: true)); | |
| 132 notifyPropertyChange(_LENGTH, len, _map.length); | |
| 133 } | |
| 134 return result; | |
| 135 } | |
| 136 | |
| 137 void clear() { | |
| 138 int len = _map.length; | |
| 139 if (hasObservers && len > 0) { | |
| 140 _map.forEach((key, value) { | |
| 141 notifyChange(new MapChangeRecord(key, isRemove: true)); | |
| 142 }); | |
| 143 notifyPropertyChange(_LENGTH, len, 0); | |
| 144 } | |
| 145 _map.clear(); | |
| 146 } | |
| 147 | |
| 148 void forEach(void f(K key, V value)) => _map.forEach(f); | |
| 149 | |
| 150 String toString() => Maps.mapToString(this); | |
| 151 } | |
| OLD | NEW |