ReadOnlyCollection<T> class
Extends
Enumerable<T> > ListBase<T> > ReadOnlyCollection<T>
Constructors
new ReadOnlyCollection(Iterable<T> source) #
Copies all of the elements from source into a new collection. Add or removing items in source will not change the contents of the new collection. Note: this is not a deep copy.
ReadOnlyCollection(Iterable<T> source) : _items = new List<T>.from(source);
const ReadOnlyCollection.empty() #
const ReadOnlyCollection.empty() : _items = const [];
Methods
bool operator ==(other) #
The equality operator.
The default behavior for all Object
s is to return true if and
only if this
and
other are the same object.
If a subclass overrides the equality operator it should override
the hashCode
method as well to maintain consistency.
bool operator ==(other) => identical(this, other);
T operator [](int index) #
Returns the element at the given
index in the list or throws
an IndexOutOfRangeException
if
index is out of bounds.
T operator [](int index) { // NOTE: we're relying on List<E> to do the range checking for us. return _items[index]; }
void operator []=(int index, T value) #
Sets the entry at the given
index in the list to
value.
Throws an IndexOutOfRangeException
if
index is out of bounds.
void operator []=(int index, T value) { throw const UnsupportedOperationException('Mutation operations are not supported'); }
void add(T value) #
Adds
value at the end of the list, extending the length by
one. Throws an UnsupportedOperationException
if the list is not
extendable.
void add(T value) { throw const UnsupportedOperationException('Mutation operations are not supported'); }
void addAll(Collection<T> value) #
Appends all elements of the collection
to the end of this list.
Extends the length of the list by the number of elements in collection
.
Throws an UnsupportedOperationException
if this list is not
extendable.
void addAll(Collection<T> value) { throw const UnsupportedOperationException('Mutation operations are not supported'); }
void addLast(T value) #
Adds
value at the end of the list, extending the length by
one. Throws an UnsupportedOperationException
if the list is not
extendable.
void addLast(T value) { throw const UnsupportedOperationException('Mutation operations are not supported'); }
Object aggregate(Object seed, Func2 f) #
Object aggregate(Object seed, Func2<Object, T, Object> f) { requireArgumentNotNull(f, 'f'); return CollectionUtil.aggregate(this, seed, f); }
void clear() #
Removes all elements in the list.
The length of the list becomes zero.
Throws an UnsupportedOperationException
, and retains all elements, if the
length of the list cannot be changed.
void clear() { throw const UnsupportedOperationException('Mutation operations are not supported'); }
bool contains(T item) #
bool contains(T item) { for (final e in this) { if(e == item) { return true; } } return false; }
int count([Func1 f = null]) #
int count([Func1<T, bool> f = null]) { if(f == null) { f = (a) => true; } int c = 0; for(final e in this) { if(f(e)) { c++; } } return c; }
Enumerable<T> distinct([Func2 comparer = null]) #
Enumerable<T> distinct([Func2<T, T, bool> comparer = null]) { if(comparer == null) { comparer = (a,b) => a == b; } return new _FuncEnumerable(this, (s) => new _DistinctIterator(s, comparer)); }
bool every(bool f(T element)) #
Returns true if every elements of this collection satisify the predicate f. Returns false otherwise.
bool every(bool f(T element)) { for (var i = 0; i < length; i++) { if(!f(this[i])) { return false; } } return true; }
Enumerable<T> exclude(Iterable<T> items) #
Enumerable<T> exclude(Iterable<T> items) { requireArgumentNotNull(items, 'items'); final iEnum = $(items); Func1<T, bool> f = (e) => !iEnum.contains(e); return new _FuncEnumerable(this, (s) => new _WhereIterator<T>(s, f)); }
Enumerable<T> filter(Func1 f) #
Returns a new collection with the elements of this collection that satisfy the predicate f.
An element satisfies the predicate
f if f(element)
returns true.
Enumerable<T> filter(Func1<T, bool> f) { requireArgumentNotNull(f, 'f'); return new _FuncEnumerable(this, (s) => new _WhereIterator<T>(s, f)); }
T first([Func1 f = null]) #
T first([Func1<T, bool> f = null]) { if(f == null) { f = (e) => true; } final iter = new _WhereIterator<T>(this.iterator(), f); if(!iter.hasNext()) { throw const InvalidOperationException('The input sequence is empty.'); } return iter.next(); }
T firstOrDefault([Func1 f = null, T defaultValue = null]) #
T firstOrDefault([Func1<T, bool> f = null, T defaultValue = null]) { if(f == null) { f = (e) => true; } final iter = new _WhereIterator<T>(this.iterator(), f); if(!iter.hasNext()) { return defaultValue; } return iter.next(); }
void forEach(void f(T element)) #
Applies the function f to each element of this collection.
void forEach(void f(T element)) { for(var i = 0; i < length; i++) { f(this[i]); } }
void forEachWithIndex(Action2 f) #
void forEachWithIndex(Action2<T, int> f) { int i = 0; for(final e in this) { f(e, i++); } }
List<T> getRange(int start, int itemCount) #
Returns a new list containing
itemCount elements from the list,
starting at
start.
Returns an empty list if
itemCount is 0.
Throws an IllegalArgumentException
if
itemCount is negative.
Throws an IndexOutOfRangeException
if
start or
start + itemCount - 1
are out of range.
List<T> getRange(int start, int itemCount) { requireArgument(itemCount >= 0, 'count'); final lastIndex = start + itemCount - 1; if(itemCount > 0) { if(start < 0) { throw new IndexOutOfRangeException(start); } else if(lastIndex >= length) { throw new IndexOutOfRangeException(lastIndex); } } var list = new List<T>(); for(var i = start; i <= lastIndex; i++) { list.add(this[i]); } return list; }
Grouping<Dynamic, T> group([Func1 keyFunc = null]) #
Grouping<Dynamic, T> group([Func1<T, Object> keyFunc = null]) { return new Grouping(this, keyFunc); }
int hashCode() #
Get a hash code for this object.
All objects have hash codes. Hash codes are guaranteed to be the
same for objects that are equal when compared using the equality
operator ==
. Other than that there are no guarantees about
the hash codes. They will not be consistent between runs and
there are no distribution guarantees.
If a subclass overrides hashCode
it should override the
equality operator as well to maintain consistency.
external int hashCode();
int indexOf(T element, [int start = 0]) #
Returns the first index of element in the list. Searches the list from index start to the length of the list. Returns -1 if element is not found.
int indexOf(T element, [int start=0]) { for (var i = start; i < length; i++) { if(this[i] == element) { return i; } } return -1; }
void insertRange(int start, int length, [T initialValue]) #
Inserts a new range into the list, starting from
start to
start + length - 1
. The entries are filled with
initialValue.
Throws an UnsupportedOperationException
if the list is
not extendable.
If
length is 0, this method does not do anything.
If
start is the length of the list, this method inserts the
range at the end of the list.
Throws an ArgumentError
if
length is negative.
Throws an IndexOutOfRangeException
if
start is negative or if
start is greater than the length of the list.
void insertRange(int start, int length, [T initialValue]) { throw const UnsupportedOperationException('Mutation operations are not supported'); }
bool isEmpty() #
Returns true if there is no element in this collection.
bool isEmpty() => length == 0;
Iterator<T> iterator() #
Returns an Iterator
that iterates over this Iterable
object.
Iterator<T> iterator() { return new IndexIterator<T>(length, (i) => this[i]); }
String join([String separator = ', ']) #
String join([String separator = ', ']) { final StringBuffer sb = new StringBuffer(); for(final e in this) { if(sb.length > 0) { sb.add(separator); } sb.add(e); } return sb.toString(); }
T last() #
Returns the last element of the list, or throws an out of bounds exception if the list is empty.
T last() => this[this.length-1];
int lastIndexOf(T element, [int start = 0]) #
Returns the last index of element in the list. Searches the list from index start to 0. Returns -1 if element is not found.
int lastIndexOf(T element, [int start=0]) { // DARTBUG: having trouble testing this in the compiler. :-/ var lastIndex = -1; for (var i = start; i < length; i++) { if(this[i] == element) { lastIndex = i; } } return lastIndex; }
int get length() #
Returns the number of elements in this collection.
int get length => _items.length;
void set length(int newLength) #
Changes the length of the list. If
newLength is greater than
the current length, entries are initialized to null
. Throws
an UnsupportedOperationException
if the list is not extendable.
void set length(int newLength) { throw const UnsupportedOperationException('Mutation operations are not supported'); }
Enumerable map(Func1 f) #
Returns a new collection with the elements f(e)
for each element e
of this collection.
Note on typing: the return type of f() could be an arbitrary type and consequently the returned collection's typeis Collection.
Enumerable map(Func1<T, Object> f) { requireArgumentNotNull(f, 'f'); return new _FuncEnumerable(this, (s) => new _SelectIterator<T, Object>(s, f)); }
noSuchMethod(String name, List args) #
noSuchMethod
is invoked when users invoke a non-existant method
on an object. The name of the method and the arguments of the
invocation are passed to noSuchMethod
. If noSuchMethod
returns a value, that value becomes the result of the original
invocation.
The default behavior of noSuchMethod
is to throw a
noSuchMethodError
.
external Dynamic noSuchMethod(String name, List args);
reduce(initialValue, combine(previousValue, T element)) #
Reduce 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 a collection:
collection.reduce(0, (prev, element) => prev + element);
Dynamic reduce(Dynamic initialValue, Dynamic combine(Dynamic previousValue, T element)) { return Collections.reduce(this, initialValue, combine); }
T removeAt(int index) #
Removes the element at position index from the list.
This reduces the length of the list by one and moves all later elements
down by one position.
Returns the removed element.
Throws an ArgumentError
if
index is not an int
.
Throws an IndexOutOfRangeException
if the
index does not point inside
the list.
Throws an UnsupportedOperationException
, and doesn't remove the element,
if the length of the list cannot be changed.
T removeAt(int index) { throw const UnsupportedOperationException('Mutation operations are not supported'); }
T removeLast() #
Pops and returns the last element of the list.
Throws a UnsupportedOperationException
if the length of the
list cannot be changed.
T removeLast() { throw const UnsupportedOperationException('Mutation operations are not supported'); }
void removeRange(int start, int length) #
Removes
length elements from the list, beginning at
start.
Throws an UnsupportedOperationException
if the list is
not extendable.
If
length is 0, this method does not do anything.
Throws an ArgumentError
if
length is negative.
Throws an IndexOutOfRangeException
if
start or
:start + length: - 1
are out of range.
void removeRange(int start, int length) { throw const UnsupportedOperationException('Mutation operations are not supported'); }
Type get runtimeType() #
A representation of the runtime type of the object.
external Type get runtimeType;
Enumerable selectMany(Func1 f) #
Enumerable selectMany(Func1<T, Iterable> f) { requireArgumentNotNull(f, 'f'); return new _FuncEnumerable(this, (s) => new _SelectManyIterator._internal(s, f)); }
NumberEnumerable selectNumbers(Func1 f) #
NumberEnumerable selectNumbers(Func1<T, num> f) { requireArgumentNotNull(f, 'f'); return new _FuncNumEnumerable<T>(this, (s) => new _SelectIterator<T, num>(s, f)); }
void setRange(int start, int length, List<T> from, [int startFrom]) #
Copies
length elements of
from, starting
at
startFrom, into the list, starting at
start.
If
length is 0, this method does not do anything.
Throws an ArgumentError
if
length is negative.
Throws an IndexOutOfRangeException
if
start or
start + length - 1
are out of range for this
, or if
startFrom or startFrom + length - 1
are out of range for
from.
void setRange(int start, int length, List<T> from, [int startFrom]) { throw const UnsupportedOperationException('Mutation operations are not supported'); }
T single([Func1 f = null]) #
T single([Func1<T, bool> f = null]) { if(f == null) { f = (e) => true; } final iter = new _WhereIterator<T>(this.iterator(), f); if(!iter.hasNext()) { throw const InvalidOperationException('The input sequence is empty.'); } final value = iter.next(); if(iter.hasNext()) { throw const InvalidOperationException('The input sequence contains more than one element.'); } return value; }
T singleOrDefault([Func1 f = null, T defaultValue = null]) #
T singleOrDefault([Func1<T, bool> f = null, T defaultValue = null]) { if(f == null) { f = (e) => true; } final iter = new _WhereIterator<T>(this.iterator(), f); if(!iter.hasNext()) { return defaultValue; } final value = iter.next(); if(iter.hasNext()) { throw const InvalidOperationException('The input sequence contains more than one element.'); } return value; }
bool some(bool f(T element)) #
Returns true if one element of this collection satisfies the predicate f. Returns false otherwise.
bool some(bool f(T element)) { for (var i = 0; i < length; i++) { if(f(this[i])) { return true; } } return false; }
void sort(Func2 comparer) #
Sorts the list according to the order specified by the Comparator
.
void sort(Func2<T,T,int> comparer) { throw const UnsupportedOperationException('Mutation operations are not supported'); }
HashMap toHashMap(Func1 valueFunc, [Func1 keyFunc]) #
HashMap toHashMap(Func1<T, Object> valueFunc, [Func1<T, Hashable> keyFunc]) { if(keyFunc == null) { keyFunc = (a) => a; } final map = new HashMap(); for(final e in this) { final k = keyFunc(e); if(map.containsKey(k)) { throw new UnsupportedOperationException("The key '$k' is duplicated"); } map[k] = valueFunc(e); } return map; }
HashSet toHashSet([Func1 f]) #
HashSet toHashSet([Func1<T, Hashable> f]) { if(f == null) { return new HashSet.from(this); } else { return new HashSet.from(this.map(f)); } }
ReadOnlyCollection<T> toReadOnlyCollection() #
ReadOnlyCollection<T> toReadOnlyCollection() => new ReadOnlyCollection<T>(this);
String toString() #
Returns a string representation of this object.
String toString() => "[${this.join()}]";