LCOV - code coverage report

Current view
top level - src/converters - lazy_in_place_map.dart
Test
lcov.info
Date
2025-03-26
Legend
Lines
hit
not hit
Branches
taken
not taken
# not executed
HitTotalCoverage
Lines6767100.0%
Functions00-
Branches00-
Each row represents a line of source code
LineBranchHitsSource code
1import 'package:meta/meta.dart';
2
3import '../typedefs.dart';
4
5/// Wraps a `Map<dynamic, dynamic>` and a `Cast<V>` converter. Keys in the map
6/// must be castable to [K] (`key as K`). Values in the map are converted to [V]
7/// on demand i.e. when they are read by the program. Conversion occurs only
8/// once for each value and the original `dynamic` value is replaced with the
9/// conversion result. If some keys cannot be safely cast to [K], exceptions
10/// might occur at runtime, e.g. when reading [keys] or [entries], or calling
11/// [map].
12@internal
13class LazyInPlaceMap<K, V> implements Map<K, V> {
141 LazyInPlaceMap(this._data, this._vcast);
15
16 final Map<dynamic, dynamic> _data;
17 final Cast<V> _vcast;
18
191 @override
20 Iterable<MapEntry<K, V>> get entries =>
215 keys.map((k) => MapEntry(k, _get(k) as V));
22
231 @override
242 bool get isEmpty => _data.isEmpty;
25
261 @override
272 bool get isNotEmpty => _data.isNotEmpty;
28
291 @override
303 Iterable<K> get keys => _data.keys.cast<K>();
31
321 @override
332 int get length => _data.length;
34
351 @override
365 Iterable<V> get values => _data.keys.map((k) => _get(k) as V);
37
381 @override
391 V? operator [](Object? key) => _get(key);
40
411 @override
422 void operator []=(K key, V value) => _data[key] = value;
43
441 @override
452 void addAll(Map<K, V> other) => _data.addAll(other);
46
471 @override
48 void addEntries(Iterable<MapEntry<K, V>> newEntries) =>
492 _data.addEntries(newEntries);
50
511 @override
522 Map<RK, RV> cast<RK, RV>() => _forceCast().cast<RK, RV>();
53
541 @override
552 void clear() => _data.clear();
56
571 @override
582 bool containsKey(Object? key) => _data.containsKey(key);
59
601 @override
616 bool containsValue(Object? value) => _data.keys.any((k) => value == _get(k));
62
631 @override
64 void forEach(void Function(K key, V value) action) {
653 for (var k in _data.keys) {
662 action(k as K, _get(k) as V);
67 }
68 }
69
701 @override
71 Map<K2, V2> map<K2, V2>(MapEntry<K2, V2> Function(K key, V value) convert) {
724 final r = <K2, V2>{}, keys = _data.keys.toList();
734 for (var i = keys.length - 1; i >= 0; i--) {
743 final k = keys[i], e = convert(k as K, _get(k) as V);
753 r[e.key] = e.value;
76 }
77 return r;
78 }
79
801 @override
81 V putIfAbsent(K key, V Function() ifAbsent) =>
822 _data.putIfAbsent(key, ifAbsent);
83
841 @override
852 V? remove(Object? key) => _data.remove(key);
86
871 @override
88 void removeWhere(bool Function(K key, V value) test) {
893 final keys = _data.keys.toList();
904 for (var i = keys.length - 1; i >= 0; i--) {
912 final k = keys[i], v = _get(k);
921 if (test(k as K, v as V)) {
932 _data.remove(k);
94 }
95 }
96 }
97
981 @override
99 String toString() {
1001 _forceCast();
1012 return _data.toString();
102 }
103
1041 @override
105 V update(K key, V Function(V value) update, {V Function()? ifAbsent}) =>
1064 _data.update(key, (v) => (v != null && v is! V) ? _vcast(v) : v,
107 ifAbsent: ifAbsent);
108
1091 @override
110 void updateAll(V Function(K key, V value) update) {
1113 final keys = _data.keys.toList();
1124 for (var i = keys.length - 1; i >= 0; i--) {
1132 final k = keys[i], v = _get(k);
1143 _data[k] = update(k as K, v as V);
115 }
116 }
117
1181 Map<dynamic, dynamic> _forceCast() {
1193 var keys = _data.keys.toList();
1204 for (var i = keys.length - 1; i >= 0; i--) {
1212 _get(keys[i]);
122 }
1231 return _data;
124 }
125
1261 V? _get(Object? key) {
1272 dynamic v = _data[key];
1281 if (v != null && v is! V) {
1292 v = _vcast(v);
1302 _data[key] = v;
131 }
132 return v;
133 }
134}
Choose Features