diff --git a/lib/src/collection/impl/map_mutable.dart b/lib/src/collection/impl/map_mutable.dart index e1192b4..28b7630 100644 --- a/lib/src/collection/impl/map_mutable.dart +++ b/lib/src/collection/impl/map_mutable.dart @@ -19,7 +19,7 @@ class DartMutableMap extends Object implements KtMutableMap { @override Iterable> get iter => - _map.entries.map((entry) => _MutableEntry.from(entry)); + _map.entries.map((entry) => _MutableEntry.from(entry, this)); @override Map asMap() => _map; @@ -31,8 +31,8 @@ class DartMutableMap extends Object implements KtMutableMap { bool containsValue(V value) => _map.containsValue(value); @override - KtMutableSet> get entries => - linkedSetFrom(_map.entries.map((entry) => _MutableEntry.from(entry))); + KtMutableSet> get entries => linkedSetFrom( + _map.entries.map((entry) => _MutableEntry.from(entry, this))); @override V? get(K key) => _map[key]; @@ -121,14 +121,21 @@ class DartMutableMap extends Object implements KtMutableMap { } class _MutableEntry implements KtMutableMapEntry { - _MutableEntry(this._key, this._value); + _MutableEntry(this._key, this._value, this._parent); - factory _MutableEntry.from(MapEntry entry) => - _MutableEntry(entry.key, entry.value); + factory _MutableEntry.from( + MapEntry entry, + DartMutableMap parent, + ) => + _MutableEntry(entry.key, entry.value, parent); K _key; V _value; + /// Object reference for the [DartMutableMap] which contains this + /// [_MutableEntry]. + DartMutableMap _parent; + @override K get key => _key; @@ -137,12 +144,9 @@ class _MutableEntry implements KtMutableMapEntry { @override V setValue(V newValue) { - // setting _value here is wrong because is is a copy of the original value. - // setValue should modify the underlying list, not the copy - // see how kotlin solved this: - // https://github.com/JetBrains/kotlin/blob/ba6da7c40a6cc502508faf6e04fa105b96bc7777/libraries/stdlib/js/src/kotlin/collections/InternalHashCodeMap.kt - throw UnimplementedError( - "setValue() in not yet implemented. Please vote for https://github.com/passsy/dart_kollection/issues/55 for prioritization"); + final oldValue = _value; + _parent._map.update(key, (value) => value = newValue); + return oldValue; } @override diff --git a/test/collection/map_mutable_test.dart b/test/collection/map_mutable_test.dart index fae0142..6fe055c 100644 --- a/test/collection/map_mutable_test.dart +++ b/test/collection/map_mutable_test.dart @@ -1,8 +1,6 @@ import "package:kt_dart/collection.dart"; import "package:test/test.dart"; -import "../test/assert_dart.dart"; - void main() { group("KtMutableMap", () { group("mutableMapFrom", () { @@ -71,25 +69,16 @@ void testMutableMap( }); test("set value for mutable entry", () { - final pokemon = mutableMapFrom({ - 1: "Bulbasaur", - 2: "Ivysaur", - }); + final pokemon = mutableMapFrom({1: "Bulbasaur", 2: "Ivysaur"}); + + pokemon.entries.forEach((entry) { + final oldValue = entry.value; + final newValue = entry.value.toUpperCase(); - final e = catchException(() { - pokemon.entries.forEach((entry) { - entry.setValue(entry.value.toUpperCase()); - }); + expect(entry.setValue(newValue), oldValue); }); - // TODO exchange error check with assertion once https://github.com/passsy/dart_kollection/issues/55 has been fixed - expect(e, const TypeMatcher()); - // expect( - // pokemon, - // mapFrom({ - // 1: "BULBASAUR", - // 2: "IVYSAUR", - // })); + expect(pokemon, mapFrom({1: "BULBASAUR", 2: "IVYSAUR"})); }); }); }