public virtual void ClearItemsTest()
        {
            var item1 = new Item();
            var item2 = new Item();
            LightDictionaryBase <Item, Item> dict = Create <Item, Item>();

            dict.Add(item1, item1);
            dict.Add(item2, item2);
            dict.Count.ShouldEqual(2);
            dict.Clear();
            dict.Count.ShouldEqual(0);
            dict.ContainsKey(item1).ShouldBeFalse();
            dict.ContainsKey(item2).ShouldBeFalse();
        }
Ejemplo n.º 2
0
        public virtual TValue AddOrUpdate <TItem, TValue>(TItem item, string path,
                                                          Func <TItem, object, TValue> addValueFactory,
                                                          UpdateValueDelegate <TItem, Func <TItem, object, TValue>, TValue, object> updateValueFactory,
                                                          object state = null)
        {
            Should.NotBeNull(item, "item");
            Should.NotBeNull(path, "path");
            Should.NotBeNull(addValueFactory, "addValueFactory");
            Should.NotBeNull(updateValueFactory, "updateValueFactory");
            LightDictionaryBase <string, object> dictionary = GetOrAddAttachedDictionary(item, true);

            lock (dictionary)
            {
                object value;
                if (dictionary.TryGetValue(path, out value))
                {
                    value            = updateValueFactory(item, addValueFactory, (TValue)value, state);
                    dictionary[path] = value;
                    return((TValue)value);
                }
                value = addValueFactory(item, state);
                dictionary.Add(path, value);
                return((TValue)value);
            }
        }
Ejemplo n.º 3
0
        public virtual TValue GetOrAdd <TValue>(object item, string path, TValue value)
        {
            Should.NotBeNull(item, "item");
            Should.NotBeNull(path, "path");
            LightDictionaryBase <string, object> dictionary = GetOrAddAttachedDictionary(item, true);

            lock (dictionary)
            {
                object oldValue;
                if (dictionary.TryGetValue(path, out oldValue))
                {
                    return((TValue)oldValue);
                }
                dictionary.Add(path, value);
                return(value);
            }
        }
Ejemplo n.º 4
0
        public virtual TValue GetOrAdd <TItem, TValue>(TItem item, string path, Func <TItem, object, TValue> valueFactory,
                                                       object state = null)
        {
            Should.NotBeNull(item, "item");
            Should.NotBeNull(path, "path");
            Should.NotBeNull(valueFactory, "valueFactory");
            LightDictionaryBase <string, object> dictionary = GetOrAddAttachedDictionary(item, true);

            lock (dictionary)
            {
                object oldValue;
                if (dictionary.TryGetValue(path, out oldValue))
                {
                    return((TValue)oldValue);
                }
                oldValue = valueFactory(item, state);
                dictionary.Add(path, oldValue);
                return((TValue)oldValue);
            }
        }
Ejemplo n.º 5
0
        public virtual TValue AddOrUpdate <TItem, TValue>(TItem item, string path, TValue addValue,
                                                          UpdateValueDelegate <TItem, TValue, TValue, object> updateValueFactory,
                                                          object state = null)
        {
            Should.NotBeNull(item, nameof(item));
            Should.NotBeNull(path, nameof(path));
            Should.NotBeNull(updateValueFactory, nameof(updateValueFactory));
            LightDictionaryBase <string, object> dictionary = GetOrAddAttachedDictionary(item, true);

            lock (dictionary)
            {
                object value;
                if (dictionary.TryGetValue(path, out value))
                {
                    value            = updateValueFactory(item, addValue, (TValue)value, state);
                    dictionary[path] = value;
                    return((TValue)value);
                }
                dictionary.Add(path, addValue);
                return(addValue);
            }
        }