コード例 #1
0
        public static TValue GetOrAddDA <TKey, TValue>(this Disposable owner, ref ImmutableDictionary <TKey, TValue> location, TKey key, Func <TKey, TValue> factory, string locationName = default, Action <TKey, TValue> unclaimedValue = default)
        {
            owner.EnsureNotNull(nameof(owner));
            key.EnsureNotNull(nameof(key));
            factory.EnsureNotNull(nameof(factory));
            //
            var factoriedValue     = default(TValue);
            var valueFactoryCalled = false;
            var existingValue      = default(TValue);
            var updateResult       = default(UpdateResult <ImmutableDictionary <TKey, TValue> >);

            try {
                owner
                .UpdDA(
                    location: ref location,
                    transform:
                    locCurrent => {
                    if (locCurrent is null)
                    {
                        throw ExceptionUtilities.NewNullReferenceException(varName: locationName ?? nameof(location), component: owner);
                    }
                    else if (locCurrent.TryGetValue(key: key, value: out var locValue))
                    {
                        existingValue = locValue;
                        return(locCurrent);
                    }
                    else
                    {
                        factoriedValue     = valueFactoryCalled ? factoriedValue : factory(arg: key);
                        valueFactoryCalled = true;
                        return(locCurrent.Add(key: key, value: factoriedValue));
                    }
                },