From cbd90b116389e4efcaf48d9afca9818ac965892e Mon Sep 17 00:00:00 2001 From: "Alexander \"PapaTutuWawa" Date: Wed, 7 Jun 2023 14:13:46 +0200 Subject: [PATCH] feat(core): Add a list constructor to TypedMap --- packages/moxxmpp/lib/src/util/typed_map.dart | 10 ++++++++++ packages/moxxmpp/test/type_map_test.dart | 12 ++++++++++++ 2 files changed, 22 insertions(+) diff --git a/packages/moxxmpp/lib/src/util/typed_map.dart b/packages/moxxmpp/lib/src/util/typed_map.dart index e19b4c1..f6a096e 100644 --- a/packages/moxxmpp/lib/src/util/typed_map.dart +++ b/packages/moxxmpp/lib/src/util/typed_map.dart @@ -1,5 +1,15 @@ /// A map, similar to Map, but always uses the type of the value as the key. class TypedMap { + /// Create an empty typed map. + TypedMap(); + + /// Create a typed map from a list of values. + TypedMap.fromList(List items) { + for (final item in items) { + _data[item.runtimeType] = item; + } + } + /// The internal mapping of type -> data final Map _data = {}; diff --git a/packages/moxxmpp/test/type_map_test.dart b/packages/moxxmpp/test/type_map_test.dart index 7d9e7b9..e214e56 100644 --- a/packages/moxxmpp/test/type_map_test.dart +++ b/packages/moxxmpp/test/type_map_test.dart @@ -24,4 +24,16 @@ void main() { expect(map.get()?.i, 1); expect(map.get()?.j, false); }); + + test('Test storing data in the type map using a list', () { + // Set + final map = TypedMap.fromList([ + const TestType1(1), + const TestType2(false), + ]); + + // And access + expect(map.get()?.i, 1); + expect(map.get()?.j, false); + }); }