Esempio n. 1
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);
            }
        }
Esempio n. 2
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);
            }
        }
Esempio 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);
            }
        }
Esempio 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);
            }
        }
Esempio 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);
            }
        }
Esempio n. 6
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);
            }
        }