DelegatingMap<K, V> class
Creates a Map that delegates all operations to a base map.
This class can be used hide non-Map
methods of an object that extends
Map
, or it can be extended to add extra functionality on top of an existing
map object.
class DelegatingMap<K, V> implements Map<K, V> { Map<K, V> _base; DelegatingMap(Map<K, V> base) : _base = base; V operator [](Object key) => _base[key]; void operator []=(K key, V value) { _base[key] = value; } void addAll(Map<K, V> other) { _base.addAll(other); } void clear() { _base.clear(); } bool containsKey(Object key) => _base.containsKey(key); bool containsValue(Object value) => _base.containsValue(value); void forEach(void f(K key, V value)) { _base.forEach(f); } bool get isEmpty => _base.isEmpty; bool get isNotEmpty => _base.isNotEmpty; Iterable<K> get keys => _base.keys; int get length => _base.length; V putIfAbsent(K key, V ifAbsent()) => _base.putIfAbsent(key, ifAbsent); V remove(Object key) => _base.remove(key); Iterable<V> get values => _base.values; }
Implements
Constructors
Properties
final Iterable<K> keys #
The keys of this
.
The returned iterable has efficient length
and contains
operations,
based on length and containsKey of the map.
Iterable<K> get keys => _base.keys;
Operators
V operator [](Object key) #
Returns the value for the given key or null if key is not in the map. Because null values are supported, one should either use containsKey to distinguish between an absent key and a null value, or use the putIfAbsent method.
V operator [](Object key) => _base[key];
Methods
void addAll(Map<K, V> other) #
Adds all key-value pairs of other to this map.
If a key of other is already in this map, its value is overwritten.
The operation is equivalent to doing this[key] = value
for each key
and associated value in other. It iterates over
other, which must
therefore not change during the iteration.
void addAll(Map<K, V> other) { _base.addAll(other); }
void clear() #
V putIfAbsent(K key, V ifAbsent()) #
If key is not associated to a value, calls ifAbsent and updates the map by mapping key to the value returned by ifAbsent. Returns the value in the map.
Map<String, int> scores = {'Bob': 36};
for (var key in ['Bob', 'Rohan', 'Sophena']) {
scores.putIfAbsent(key, () => key.length);
}
scores['Bob']; // 36
scores['Rohan']; // 5
scores['Sophena']; // 7
The code that ifAbsent executes must not add or remove keys.
V putIfAbsent(K key, V ifAbsent()) => _base.putIfAbsent(key, ifAbsent);