| 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 dart.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 /** |
| 14 * Represents an observable map of model values. If any items are added, |
| 15 * removed, or replaced, then observers that are listening to [changes] |
| 16 * will be notified. |
| 17 */ |
| 18 class ObservableMap<K, V> extends ObservableMixin implements Map<K, V> { |
| 19 final Map<K, V> _map; |
| 20 |
| 21 /** Creates an observable map. */ |
| 22 ObservableMap() : _map = new HashMap<K, V>(); |
| 23 |
| 24 /** Creates a new observable map using a [LinkedHashMap]. */ |
| 25 ObservableMap.linked() : _map = new LinkedHashMap<K, V>(); |
| 26 |
| 27 /** Creates a new observable map using a [SplayTreeMap]. */ |
| 28 ObservableMap.sorted() : _map = new SplayTreeMap<K, V>(); |
| 29 |
| 30 /** |
| 31 * Creates an observable map that contains all key value pairs of [other]. |
| 32 * It will attempt to use the same backing map type if the other map is a |
| 33 * [LinkedHashMap], [SplayTreeMap], or [HashMap]. Otherwise it defaults to |
| 34 * [HashMap]. |
| 35 * |
| 36 * Note this will perform a shallow conversion. If you want a deep conversion |
| 37 * you should use [toObservable]. |
| 38 */ |
| 39 factory ObservableMap.from(Map<K, V> other) { |
| 40 var result = new ObservableMap<K, V>._createFromType(other); |
| 41 other.forEach((K key, V value) { result[key] = value; }); |
| 42 return result; |
| 43 } |
| 44 |
| 45 factory ObservableMap._createFromType(Map<K, V> other) { |
| 46 ObservableMap result; |
| 47 if (other is SplayTreeMap) { |
| 48 result = new ObservableMap<K, V>.sorted(); |
| 49 } else if (other is LinkedHashMap) { |
| 50 result = new ObservableMap<K, V>.linked(); |
| 51 } else { |
| 52 result = new ObservableMap<K, V>(); |
| 53 } |
| 54 return result; |
| 55 } |
| 56 |
| 57 Iterable<K> get keys => _map.keys; |
| 58 |
| 59 Iterable<V> get values => _map.values; |
| 60 |
| 61 int get length =>_map.length; |
| 62 |
| 63 bool get isEmpty => length == 0; |
| 64 |
| 65 bool containsValue(V value) => _map.containsValue(value); |
| 66 |
| 67 bool containsKey(K key) => _map.containsKey(key); |
| 68 |
| 69 V operator [](K key) => _map[key]; |
| 70 |
| 71 void operator []=(K key, V value) { |
| 72 int len = _map.length; |
| 73 V oldValue = _map[key]; |
| 74 _map[key] = value; |
| 75 if (hasObservers) { |
| 76 if (len != _map.length) { |
| 77 notifyChange('length', len, _map.length); |
| 78 notifyChange(key, oldValue, value, kind: ChangeRecord.INSERT); |
| 79 } else if (oldValue != value) { |
| 80 notifyChange(key, oldValue, value, kind: ChangeRecord.INDEX); |
| 81 } |
| 82 } |
| 83 } |
| 84 |
| 85 V putIfAbsent(K key, V ifAbsent()) { |
| 86 int len = _map.length; |
| 87 V result = _map.putIfAbsent(key, ifAbsent); |
| 88 if (hasObservers && len != _map.length) { |
| 89 notifyChange('length', len, _map.length); |
| 90 notifyChange(key, null, result, kind: ChangeRecord.INSERT); |
| 91 } |
| 92 return result; |
| 93 } |
| 94 |
| 95 V remove(K key) { |
| 96 int len = _map.length; |
| 97 V result = _map.remove(key); |
| 98 if (hasObservers && len != _map.length) { |
| 99 notifyChange(key, result, null, kind: ChangeRecord.REMOVE); |
| 100 notifyChange('length', len, _map.length); |
| 101 } |
| 102 return result; |
| 103 } |
| 104 |
| 105 void clear() { |
| 106 int len = _map.length; |
| 107 if (hasObservers && len > 0) { |
| 108 _map.forEach((key, value) { |
| 109 notifyChange(key, value, null, kind: ChangeRecord.REMOVE); |
| 110 }); |
| 111 notifyChange('length', len, 0); |
| 112 } |
| 113 _map.clear(); |
| 114 } |
| 115 |
| 116 void forEach(void f(K key, V value)) => _map.forEach(f); |
| 117 |
| 118 String toString() => Maps.mapToString(this); |
| 119 } |
| OLD | NEW |