DataSet class
Set
class DataSet extends DataSetView implements Set { /** * Creates an empty set. */ DataSet() { } /** * Generates Set from [Iterable] of [data]. */ factory DataSet.from(Iterable data) { var set = new DataSet(); set._silentAddAll(data); return set; } /** * Appends the [dataObj] to the set. If the element * was already in the set, [false] is returned and * nothing happens. */ bool add(dataObj, {author: null}) { var res = !_data.contains(dataObj); this._addAll([dataObj], author: author); return res; } /** * Appends all [elements] to the set. */ void addAll(Iterable elements, {author: null}) { this._addAll(elements, author: author); } /** * Removes multiple data objects from the set. */ void removeAll(Iterable toBeRemoved, {author: null}) { this._removeAll(toBeRemoved, author: author); } /** * Removes a data object from the set. If the object was not in * the set, returns [false] and nothing happens. */ bool remove(dataObj, {author: null}) { var res = _data.contains(dataObj); this._removeAll([dataObj], author: author); return res; } /** * Removes all objects that have [property] equal to [value] from this set. */ Iterable removeBy(String property, dynamic value, {author: null}) { if (!_index.containsKey(property)) { throw new NoIndexException('Property $property is not indexed.'); } this._removeAll(_index[property][value], author: author); } /** * Removes all objects satisfying filter [test] */ void _removeWhere(DataTestFunction test, {author: null}) { List toBeRemoved = []; for (var dataObj in _data) { if(test(dataObj)) { toBeRemoved.add(dataObj); } } this._removeAll(toBeRemoved, author: author); } void removeWhere(DataTestFunction test, {author: null}) { _removeWhere(test, author:author); } lookup(Object object) => _data.lookup(object); bool containsAll(Iterable other) => _data.containsAll(other); void retainWhere(bool test(element), {author: null}) { this._removeWhere((data) => !test(data), author: author); } void retainAll(Iterable<Object> elements, {author: null}) { var toKeep = new Set.from(elements); this._removeWhere((data) => !toKeep.contains(data), author:author); } Set difference(Set other) => _data.difference(other); Set intersection(Set other) => _data.intersection(other); Set union(Set other) => _data.union(other); /** * Removes all data objects from the set. */ void clear({author: null}) { // we shallow copy _data to avoid concurent modification of // the _data field during removal this._removeAll(new List.from(this._data), author:author); } void dispose() { super.dispose(); if (_dataListeners != null) { _dataListeners.forEach((data, subscription) => subscription.cancel()); } } }
Extends
DataSetView > DataSet
Implements
Constructors
new DataSet() #
Creates an empty set.
DataSet() { }
Properties
final E first #
Returns the first element.
If this
is empty throws a StateError. Otherwise this method is
equivalent to this.elementAt(0)
E get first { Iterator it = iterator; if (!it.moveNext()) { throw new StateError("No elements"); } return it.current; }
final bool isEmpty #
final bool isNotEmpty #
final Iterator iterator #
Returns an Iterator that iterates over this Iterable object.
Iterator get iterator => _data.iterator;
final E last #
E get last { Iterator it = iterator; if (!it.moveNext()) { throw new StateError("No elements"); } E result; do { result = it.current; } while(it.moveNext()); return result; }
final int length #
Returns the number of elements in this
.
Counting all elements may be involve running through all elements and can therefore be slow.
int get length => _data.length;
final Stream<dynamic> onBeforeAdd #
Stream populated with DataMapView events before any data object is added.
Stream<dynamic> get onBeforeAdd { if(_onBeforeAddedController == null) { _onBeforeAddedController = new StreamController.broadcast(sync: true); } return _onBeforeAddedController.stream; }
final Stream<dynamic> onBeforeRemove #
Stream populated with DataMapView events before any data object is removed.
Stream<dynamic> get onBeforeRemove { if(_onBeforeRemovedController == null) { _onBeforeRemovedController = new StreamController.broadcast(sync: true); } return _onBeforeRemovedController.stream; }
final Stream<ChangeSet> onChange #
Stream populated with ChangeSet events whenever the collection or any of data object contained gets changed.
Stream<ChangeSet> get onChange { if(_onChangeController == null) { _onChangeController = new StreamController.broadcast(); } return _onChangeController.stream; }
final Stream<Map> onChangeSync #
Stream populated with {'change': ChangeSet, 'author': dynamic
} events
synchronously at the moment when the collection or any data object contained
gets changed.
Stream<Map> get onChangeSync => _onChangeSyncController.stream;
final E single #
Returns the single element in this
.
If this
is empty or has more than one element throws a StateError.
E get single { Iterator it = iterator; if (!it.moveNext()) throw new StateError("No elements"); E result = it.current; if (it.moveNext()) throw new StateError("More than one element"); return result; }
final StreamController_onBeforeAddedController #
final StreamController_onBeforeAddedController = new StreamController.broadcast(sync: true)
Methods
bool add(dataObj, {author: null}) #
Appends the
dataObj to the set. If the element
was already in the set, false
is returned and
nothing happens.
bool add(dataObj, {author: null}) { var res = !_data.contains(dataObj); this._addAll([dataObj], author: author); return res; }
void addAll(Iterable elements, {author: null}) #
Appends all elements to the set.
void addAll(Iterable elements, {author: null}) { this._addAll(elements, author: author); }
void addIndex([Iterable<String> indexedProps]) #
Adds indices on chosen properties. Indexed properties can be used to retrieve data by their value with the findBy method, or removed by their value with the removeBy method.
void addIndex([Iterable<String> indexedProps]) { if (indexedProps != null) { // initialize change listener; lazy if (_index.keys.length == 0) { _initIndexListener(); } for (String prop in indexedProps) { if (!_index.containsKey(prop)) { // create and initialize the index _index[prop] = new HashIndex(); _rebuildIndex(prop); } } } }
bool any(bool f(E element)) #
void clear({author: null}) #
Removes all data objects from the set.
void clear({author: null}) { // we shallow copy _data to avoid concurent modification of // the _data field during removal this._removeAll(new List.from(this._data), author:author); }
bool contains(dataObj) #
Returns true iff this set contains the given dataObj.
@param dataObj Data object to be searched for.
bool contains(dataObj) => _data.contains(dataObj);
bool containsAll(Iterable other) #
Returns whether this Set contains all the elements of other.
bool containsAll(Iterable other) => _data.containsAll(other);
Set difference(Set other) #
Returns a new set with the the elements of this that are not in other.
That is, the returned set contains all the elements of this Set
that
are not elements of
other according to other.contains
.
Set difference(Set other) => _data.difference(other);
void dispose() #
Stream all new changes marked in ChangeSet.
void dispose() { super.dispose(); if (_dataListeners != null) { _dataListeners.forEach((data, subscription) => subscription.cancel()); } }
E elementAt(int index) #
Returns the indexth element.
If this
has fewer than
index elements throws a RangeError.
Note: if this
does not have a deterministic iteration order then the
function may simply return any element without any iteration if there are
at least
index elements in this
.
E elementAt(int index) { if (index is! int || index < 0) throw new RangeError.value(index); int remaining = index; for (E element in this) { if (remaining == 0) return element; remaining--; } throw new RangeError.value(index); }
bool every(bool f(E element)) #
Iterable expand(Iterable f(E element)) #
Expands each element of this Iterable into zero or more elements.
The resulting Iterable runs through the elements returned by f for each element of this, in order.
The returned Iterable is lazy, and calls f for each element of this every time it's iterated.
Iterable expand(Iterable f(E element)) => new ExpandIterable<E, dynamic>(this, f);
Iterable<DataMapView> findBy(String property, value) #
Finds all objects that have property equal to value in this set.
Iterable<DataMapView> findBy(String property, dynamic value) { if (!_index.containsKey(property)) { throw new NoIndexException('Property $property is not indexed.'); } return _index[property][value]; }
dynamic firstWhere(bool test(E value), {Object orElse()}) #
Returns the first element that satisfies the given predicate test.
If none matches, the result of invoking the
orElse function is
returned. By default, when
orElse is null
, a StateError is
thrown.
dynamic firstWhere(bool test(E value), { Object orElse() }) { for (E element in this) { if (test(element)) return element; } if (orElse != null) return orElse(); throw new StateError("No matching element"); }
dynamic fold(initialValue, combine(previousValue, E element)) #
Reduces a collection to a single value by iteratively combining each element of the collection with an existing value using the provided function.
Use initialValue as the initial value, and the function combine to create a new value from the previous one and an element.
Example of calculating the sum of an iterable:
iterable.fold(0, (prev, element) => prev + element);
dynamic fold(var initialValue, dynamic combine(var previousValue, E element)) { var value = initialValue; for (E element in this) value = combine(value, element); return value; }
void forEach(void f(E element)) #
Set intersection(Set other) #
Returns a new set which is the intersection between this set and other.
That is, the returned set contains all the elements of this Set
that
are also elements of other
according to other.contains
.
Set intersection(Set other) => _data.intersection(other);
String join([String separator = ""]) #
Converts each element to a String and concatenates the strings.
Converts each element to a String by calling Object.toString on it. Then concatenates the strings, optionally separated by the separator string.
String join([String separator = ""]) { Iterator<E> iterator = this.iterator; if (!iterator.moveNext()) return ""; StringBuffer buffer = new StringBuffer(); if (separator == null || separator == "") { do { buffer.write("${iterator.current}"); } while (iterator.moveNext()); } else { buffer.write("${iterator.current}"); while (iterator.moveNext()) { buffer.write(separator); buffer.write("${iterator.current}"); } } return buffer.toString(); }
dynamic lastWhere(bool test(E value), {Object orElse()}) #
Returns the last element that satisfies the given predicate test.
If none matches, the result of invoking the
orElse function is
returned. By default, when
orElse is null
, a StateError is
thrown.
dynamic lastWhere(bool test(E value), { Object orElse() }) { E result = null; bool foundMatching = false; for (E element in this) { if (test(element)) { result = element; foundMatching = true; } } if (foundMatching) return result; if (orElse != null) return orElse(); throw new StateError("No matching element"); }
DataSetView liveDifference(DataSetView other) #
Minuses the data set with another DataSetView to form a new, ExceptedDataSetView.
The set remains up-to-date w.r.t. to the source set via background synchronization.
DataSetView liveDifference(DataSetView other) { return new ExceptedDataSetView(this, other); }
DataSetView liveIntersection(DataSetView other) #
Intersects the data set with another DataSetView to form a new, IntersectedSetView
.
The set remains up-to-date w.r.t. to the source set via background synchronization.
DataSetView liveIntersection(DataSetView other) { return other == this ? this : new IntersectedDataSetView(this, other); }
DataListView liveSort(int compare(a, b)) #
Sorts the data set with the given compare function.
DataListView liveSort(int compare(a, b)) { return new SortedDataListView(this, compare); }
DataListView liveSortByKey(key(x)) #
Sorts the data set by comparing keys generated by key function.
DataListView liveSortByKey(key(x)) { return new SortedDataListView.fromKey(this, key); }
DataSetView liveUnion(DataSetView other) #
Unions the data set with another DataSetView to form a new, UnionedSetView
.
The set remains up-to-date w.r.t. to the source set via background synchronization.
DataSetView liveUnion(DataSetView other) { return other == this ? this : new UnionedDataSetView(this, other); }
DataSetView liveWhere(test, [ChangeNotificationsMixin args = null]) #
Filters the data set w.r.t. the given filter function test.
The set remains up-to-date w.r.t. to the source set via background synchronization.
DataSetView liveWhere(test, [ChangeNotificationsMixin args = null]) { return new FilteredDataSetView(this, test, args); }
dynamic lookup(Object object) #
If an object equal to object is in the set, return it.
Checks if there is an object in the set that is equal to object. If so, that object is returned, otherwise returns null.
lookup(Object object) => _data.lookup(object);
Iterable map(f(E element)) #
Returns a lazy Iterable where each element e
of this
is replaced
by the result of f(e)
.
This method returns a view of the mapped elements. As long as the returned Iterable is not iterated over, the supplied function f will not be invoked. The transformed elements will not be cached. Iterating multiple times over the the returned Iterable will invoke the supplied function f multiple times on the same element.
Iterable map(f(E element)) => new MappedIterable<E, dynamic>(this, f);
E reduce(E combine(E value, E element)) #
Reduces a collection to a single value by iteratively combining elements of the collection using the provided function.
Example of calculating the sum of an iterable:
iterable.reduce((value, element) => value + element);
E reduce(E combine(E value, E element)) { Iterator<E> iterator = this.iterator; if (!iterator.moveNext()) { throw new StateError("No elements"); } E value = iterator.current; while (iterator.moveNext()) { value = combine(value, iterator.current); } return value; }
bool remove(dataObj, {author: null}) #
Removes a data object from the set. If the object was not in
the set, returns false
and nothing happens.
bool remove(dataObj, {author: null}) { var res = _data.contains(dataObj); this._removeAll([dataObj], author: author); return res; }
void removeAll(Iterable toBeRemoved, {author: null}) #
Removes multiple data objects from the set.
void removeAll(Iterable toBeRemoved, {author: null}) { this._removeAll(toBeRemoved, author: author); }
Iterable removeBy(String property, value, {author: null}) #
Removes all objects that have property equal to value from this set.
Iterable removeBy(String property, dynamic value, {author: null}) { if (!_index.containsKey(property)) { throw new NoIndexException('Property $property is not indexed.'); } this._removeAll(_index[property][value], author: author); }
void removeWhere(DataTestFunction test, {author: null}) #
Removes all elements of this set that satisfy test.
void removeWhere(DataTestFunction test, {author: null}) { _removeWhere(test, author:author); }
void retainAll(Iterable<Object> elements, {author: null}) #
Removes all elements of this set that are not elements in elements.
Checks for each element of
elements whether there is an element in this
set that is equal to it (according to this.contains
), and if so, the
equal element in this set is retained, and elements that are not equal
to any element in elements
are removed.
void retainAll(Iterable<Object> elements, {author: null}) { var toKeep = new Set.from(elements); this._removeWhere((data) => !toKeep.contains(data), author:author); }
void retainWhere(bool test(element), {author: null}) #
Removes all elements of this set that fail to satisfy test.
void retainWhere(bool test(element), {author: null}) { this._removeWhere((data) => !test(data), author: author); }
E singleWhere(bool test(E value)) #
Returns the single element that satisfies test. If no or more than one element match then a StateError is thrown.
E singleWhere(bool test(E value)) { E result = null; bool foundMatching = false; for (E element in this) { if (test(element)) { if (foundMatching) { throw new StateError("More than one matching element"); } result = element; foundMatching = true; } } if (foundMatching) return result; throw new StateError("No matching element"); }
Iterable<E> skip(int n) #
Iterable<E> skipWhile(bool test(E value)) #
Returns an Iterable that skips elements while test is satisfied.
The filtering happens lazily. Every new Iterator of the returned
Iterable iterates over all elements of this
.
As long as the iterator's elements satisfy
test they are
discarded. Once an element does not satisfy the
test the iterator stops
testing and uses every later element unconditionally. That is, the elements
of the returned Iterable are the elements of this
starting from the
first element that does not satisfy
test.
Iterable<E> skipWhile(bool test(E value)) { return new SkipWhileIterable<E>(this, test); }
Iterable<E> take(int n) #
Iterable<E> takeWhile(bool test(E value)) #
Returns an Iterable that stops once test is not satisfied anymore.
The filtering happens lazily. Every new Iterator of the returned
Iterable starts iterating over the elements of this
.
When the iterator encounters an element e
that does not satisfy
test,
it discards e
and moves into the finished state. That is, it does not
get or provide any more elements.
Iterable<E> takeWhile(bool test(E value)) { return new TakeWhileIterable<E>(this, test); }
List<E> toList({bool growable: true}) #
Set<E> toSet() #
String toString() #
Returns a string representation of this object.
String toString() => toList().toString();
void unattachListeners() #
void unattachListeners() { _onChangeController.close(); }
Set union(Set other) #
Returns a new set which contains all the elements of this set and other.
That is, the returned set contains all the elements of this Set
and
all the elements of
other.
Set union(Set other) => _data.union(other);
Iterable<E> where(bool f(E element)) #
Returns a lazy Iterable with all elements that satisfy the
predicate test
.
This method returns a view of the mapped elements. As long as the
returned Iterable is not iterated over, the supplied function test
will
not be invoked. Iterating will not cache results, and thus iterating
multiple times over the returned Iterable will invoke the supplied
function test
multiple times on the same element.
Iterable<E> where(bool f(E element)) => new WhereIterable<E>(this, f);