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 library web_ui.observe.set; |
| 6 |
| 7 import 'dart:collection'; |
| 8 import 'package:web_ui/observe.dart'; |
| 9 import 'list.dart'; |
| 10 import 'map.dart' show MapFactory; |
| 11 |
| 12 /** |
| 13 * Represents an observable set of model values. If any items are added, |
| 14 * removed, or replaced, then observers that are registered with |
| 15 * [observe] will be notified. |
| 16 */ |
| 17 // TODO(jmesserly): ideally this could be based ObservableMap, or Dart |
| 18 // would have a built in Set<->Map adapter as suggested in |
| 19 // https://code.google.com/p/dart/issues/detail?id=5603 |
| 20 class ObservableSet<E> extends Collection<E> implements Set<E> { |
| 21 final Map<E, Object> _map; |
| 22 final Map<E, Object> _observeKey; |
| 23 Object _observeLength; |
| 24 |
| 25 final MapFactory<E> _createMap; |
| 26 |
| 27 /** |
| 28 * Creates an observable set, optionally using the provided [createMap] |
| 29 * factory to construct a custom map type. |
| 30 */ |
| 31 ObservableSet({MapFactory<E> createMap}) |
| 32 : _map = createMap != null ? createMap() : new Map<E, Object>(), |
| 33 _observeKey = createMap != null ? createMap() : new Map<E, Object>(), |
| 34 _createMap = createMap; |
| 35 |
| 36 /** |
| 37 * Creates an observable set that contains all elements of [other]. |
| 38 */ |
| 39 factory ObservableSet.from(Iterable<E> other, {MapFactory<E> createMap}) { |
| 40 return new ObservableSet<E>(createMap: createMap)..addAll(other); |
| 41 } |
| 42 |
| 43 void _notifyReadKey(E key) { |
| 44 if (observeReads) _observeKey[key] = notifyRead(_observeKey[key]); |
| 45 } |
| 46 |
| 47 void _notifyReadAll() { |
| 48 if (!observeReads) return; |
| 49 _observeLength = notifyRead(_observeLength); |
| 50 for (E key in _map.keys) { |
| 51 _observeKey[key] = notifyRead(_observeKey[key]); |
| 52 } |
| 53 } |
| 54 |
| 55 void _notifyReadLength() { |
| 56 if (observeReads) _observeLength = notifyRead(_observeLength); |
| 57 } |
| 58 |
| 59 void _notifyWriteLength(int originalLength) { |
| 60 if (_observeLength != null && originalLength != _map.length) { |
| 61 _observeLength = notifyWrite(_observeLength); |
| 62 } |
| 63 } |
| 64 |
| 65 void _notifyWriteKey(E key) { |
| 66 var observer = _observeKey.remove(key); |
| 67 if (observer != null) notifyWrite(observer); |
| 68 } |
| 69 |
| 70 /** |
| 71 * Returns true if [value] is in the set. |
| 72 */ |
| 73 bool contains(E value) { |
| 74 _notifyReadKey(value); |
| 75 return _map.containsKey(value); |
| 76 } |
| 77 |
| 78 /** |
| 79 * Adds [value] into the set. The method has no effect if |
| 80 * [value] was already in the set. |
| 81 */ |
| 82 void add(E value) { |
| 83 int len = _map.length; |
| 84 _map[value] = const Object(); |
| 85 if (len != _map.length) _notifyWriteKey(value); |
| 86 _notifyWriteLength(len); |
| 87 } |
| 88 |
| 89 /** |
| 90 * Removes [value] from the set. Returns true if [value] was |
| 91 * in the set. Returns false otherwise. The method has no effect |
| 92 * if [value] value was not in the set. |
| 93 */ |
| 94 bool remove(E value) { |
| 95 // notifyRead because result depends on if the key already exists |
| 96 _notifyReadKey(value); |
| 97 |
| 98 int len = _map.length; |
| 99 bool result = _map.remove(value) != null; |
| 100 if (len != _map.length) _notifyWriteKey(value); |
| 101 _notifyWriteLength(len); |
| 102 return result; |
| 103 } |
| 104 |
| 105 /** |
| 106 * Removes all elements in the set. |
| 107 */ |
| 108 void clear() { |
| 109 int len = _map.length; |
| 110 _map.clear(); |
| 111 _notifyWriteLength(len); |
| 112 _observeKey.values.forEach(notifyWrite); |
| 113 _observeKey.clear(); |
| 114 } |
| 115 |
| 116 int get length { |
| 117 _notifyReadLength(); |
| 118 return _map.length; |
| 119 } |
| 120 |
| 121 bool get isEmpty => length == 0; |
| 122 |
| 123 Iterator<E> get iterator => new _ObservableSetIterator<E>(this); |
| 124 |
| 125 /** |
| 126 * Adds all the elements of the given collection to the set. |
| 127 */ |
| 128 void addAll(Collection<E> collection) => collection.forEach(add); |
| 129 |
| 130 /** |
| 131 * Removes all the elements of the given collection from the set. |
| 132 */ |
| 133 void removeAll(Collection<E> collection) => collection.forEach(remove); |
| 134 |
| 135 /** |
| 136 * Returns true if [collection] contains all the elements of this |
| 137 * collection. |
| 138 */ |
| 139 bool isSubsetOf(Collection<E> collection) => |
| 140 new Set<E>.from(collection).containsAll(this); |
| 141 |
| 142 /** |
| 143 * Returns true if this collection contains all the elements of |
| 144 * [collection]. |
| 145 */ |
| 146 bool containsAll(Collection<E> collection) => collection.every(contains); |
| 147 |
| 148 /** |
| 149 * Returns a new set which is the intersection between this set and |
| 150 * the given collection. |
| 151 */ |
| 152 ObservableSet<E> intersection(Collection<E> collection) { |
| 153 var result = new ObservableSet<E>(createMap: _createMap); |
| 154 |
| 155 for (E value in collection) { |
| 156 if (contains(value)) result.add(value); |
| 157 } |
| 158 return result; |
| 159 } |
| 160 |
| 161 String toString() => Collections.collectionToString(this); |
| 162 } |
| 163 |
| 164 class _ObservableSetIterator<E> implements Iterator<E> { |
| 165 final ObservableSet<E> _set; |
| 166 final Iterator<E> _iterator; |
| 167 |
| 168 _ObservableSetIterator(ObservableSet<E> set) |
| 169 : _set = set, _iterator = set._map.keys.iterator; |
| 170 |
| 171 bool moveNext() { |
| 172 _set._notifyReadLength(); |
| 173 return _iterator.moveNext(); |
| 174 } |
| 175 |
| 176 E get current { |
| 177 var result = _iterator.current; |
| 178 if (result != null) _set._notifyReadKey(result); |
| 179 return result; |
| 180 } |
| 181 } |
OLD | NEW |