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