Exemple #1
0
        public virtual TValue GetValue <TValue>(object item, string path, bool throwOnError)
        {
            Should.NotBeNull(item, "item");
            Should.NotBeNull(path, "path");
            LightDictionaryBase <string, object> dictionary = GetOrAddAttachedDictionary(item, false);

            if (dictionary == null)
            {
                if (throwOnError)
                {
                    throw new KeyNotFoundException();
                }
                return(default(TValue));
            }
            lock (dictionary)
            {
                object value;
                if (!dictionary.TryGetValue(path, out value))
                {
                    if (throwOnError)
                    {
                        throw new KeyNotFoundException();
                    }
                    return(default(TValue));
                }
                return((TValue)value);
            }
        }
Exemple #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);
            }
        }
Exemple #3
0
        public virtual IList <KeyValuePair <string, object> > GetValues(object item, Func <string, object, bool> predicate)
        {
            Should.NotBeNull(item, "item");
            LightDictionaryBase <string, object> dictionary = GetOrAddAttachedDictionary(item, false);

            if (dictionary == null)
            {
                return(Empty.Array <KeyValuePair <string, object> >());
            }
            lock (dictionary)
            {
                if (predicate == null)
                {
                    return(new List <KeyValuePair <string, object> >(dictionary));
                }
                var list = new List <KeyValuePair <string, object> >();
                foreach (var keyValue in dictionary)
                {
                    if (predicate(keyValue.Key, keyValue.Value))
                    {
                        list.Add(keyValue);
                    }
                }
                return(list);
            }
        }
Exemple #4
0
        public virtual void SetValue(object item, string path, object value)
        {
            Should.NotBeNull(item, "item");
            Should.NotBeNull(path, "path");
            LightDictionaryBase <string, object> dictionary = GetOrAddAttachedDictionary(item, true);

            lock (dictionary)
                dictionary[path] = value;
        }
Exemple #5
0
        public virtual bool Contains(object item, string path)
        {
            Should.NotBeNull(item, "item");
            LightDictionaryBase <string, object> dictionary = GetOrAddAttachedDictionary(item, false);

            if (dictionary == null)
            {
                return(false);
            }
            lock (dictionary)
                return(dictionary.ContainsKey(path));
        }
        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();
        }
        public virtual void ComparerTest()
        {
            LightDictionaryBase <string, string> dict = Create <string, string>(StringComparer.Ordinal);

            dict["a"] = "a";
            dict["A"] = "A";
            dict.Count.ShouldEqual(2);
            dict["a"].ShouldEqual("a");
            dict["A"].ShouldEqual("A");

            dict      = Create <string, string>(StringComparer.OrdinalIgnoreCase);
            dict["a"] = "a";
            dict["A"] = "A";
            dict.Count.ShouldEqual(1);
            dict["a"].ShouldEqual("A");
            dict["A"].ShouldEqual("A");
        }
Exemple #8
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);
            }
        }
Exemple #9
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);
            }
        }
Exemple #10
0
        public virtual bool Clear(object item, string path)
        {
            LightDictionaryBase <string, object> dictionary = GetOrAddAttachedDictionary(item, false);

            if (dictionary == null)
            {
                return(false);
            }
            lock (dictionary)
            {
                if (dictionary.Remove(path))
                {
                    if (dictionary.Count == 0)
                    {
                        ClearInternal(item);
                    }
                    return(true);
                }
            }
            return(false);
        }
Exemple #11
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);
            }
        }
Exemple #12
0
        public virtual bool TryGetValue <TValue>(object item, string path, out TValue value)
        {
            Should.NotBeNull(item, "item");
            Should.NotBeNull(path, "path");
            LightDictionaryBase <string, object> dictionary = GetOrAddAttachedDictionary(item, false);

            if (dictionary == null)
            {
                value = default(TValue);
                return(false);
            }
            lock (dictionary)
            {
                object result;
                if (dictionary.TryGetValue(path, out result))
                {
                    value = (TValue)result;
                    return(true);
                }
                value = default(TValue);
                return(false);
            }
        }