// ----------------------------------------------------------------------- // // Copyright (c) James South. // Licensed under the Apache License, Version 2.0. // // ----------------------------------------------------------------------- namespace ImageProcessor.Web.Helpers { #region Using using System.Collections.Generic; using System.Linq; #endregion /// /// Represents a collection of keys and values that are thread safe. /// /// /// The type of the keys in the dictionary. /// /// /// The type of the values in the dictionary. /// internal class LockedDictionary : IDictionary { /// /// The _inner. /// private readonly Dictionary innerDictionary = new Dictionary(); #region Constructors /// /// Initializes a new instance of the class. /// /// /// The value to initialize the LockedDictionary with. /// public LockedDictionary(IEnumerable> val = null) { if (val != null) { this.innerDictionary = val.ToDictionary(x => x.Key, x => x.Value); } } #endregion #region Properties /// /// Gets a collection containing the keys in the . /// public ICollection Keys { get { lock (this.innerDictionary) { return this.innerDictionary.Keys.ToArray(); } } } /// /// Gets a collection containing the values in the . /// public ICollection Values { get { lock (this.innerDictionary) { return this.innerDictionary.Values.ToArray(); } } } /// /// Gets the number of key/value pairs contained in the . /// public int Count { get { lock (this.innerDictionary) { return this.innerDictionary.Count; } } } /// /// Gets a value indicating whether the is read only. /// public bool IsReadOnly { get { return false; } } /// /// Gets or sets the value associated with the specified key. /// /// /// The key of the value to get or set. /// /// /// TThe value associated with the specified key. If the specified key is not found, /// a get operation throws a , /// and a set operation creates a new element with the specified key. /// public TVal this[TKey key] { get { lock (this.innerDictionary) { return this.innerDictionary[key]; } } set { lock (this.innerDictionary) { this.innerDictionary[key] = value; } } } #endregion #region Methods /// /// Adds the specified key and value to the dictionary. /// /// /// The key of the element to add. /// /// /// The value of the element to add. The value can be null for reference types. /// public void Add(TKey key, TVal value) { lock (this.innerDictionary) { this.innerDictionary.Add(key, value); } } /// /// Determines whether the LockedDictionary contains the specified key. /// /// /// The key to locate in the LockedDictionary. /// /// /// true if the LockedDictionary contains the key; otherwise, false. /// public bool ContainsKey(TKey key) { lock (this.innerDictionary) { return this.innerDictionary.ContainsKey(key); } } /// /// Removes the value with the specified key from the . /// /// /// The key of the element to remove. /// /// /// true if the element is successfully found and removed; otherwise, false. /// This method returns false if key is not found in the . /// public bool Remove(TKey key) { lock (this.innerDictionary) { return this.innerDictionary.Remove(key); } } /// /// Gets the value associated with the specified key. /// /// /// The key of the value to get. /// /// /// When this method returns, contains the value associated with the specified key, if the key is found; /// otherwise, the default value for the type of the value parameter. This parameter is passed uninitialized. /// /// /// true if the contains an element with /// the specified key; otherwise, false. /// public bool TryGetValue(TKey key, out TVal value) { lock (this.innerDictionary) { return this.innerDictionary.TryGetValue(key, out value); } } /// /// Adds the specified key and value to the dictionary. /// /// /// The representing /// the item to add. /// public void Add(KeyValuePair item) { lock (this.innerDictionary) { this.innerDictionary.Add(item.Key, item.Value); } } /// /// Removes all keys and values from the . /// public void Clear() { lock (this.innerDictionary) { this.innerDictionary.Clear(); } } /// /// Determines whether the contains the specified key. /// /// /// The representing /// the item to locate. /// /// /// true if the contains an element /// with the specified key; otherwise, false. /// public bool Contains(KeyValuePair item) { lock (this.innerDictionary) { var inner = this.innerDictionary as IDictionary; return inner.Contains(item); } } /// /// Copies the elements of an to a one-dimensional /// starting at a particular index. /// /// /// The one-dimensional that is the destination of the elements copied /// from .KeyCollection. /// The must have zero-based indexing. /// /// /// The zero-based index in array at which copying begins. /// public void CopyTo(KeyValuePair[] array, int arrayIndex) { lock (this.innerDictionary) { var inner = this.innerDictionary as IDictionary; inner.CopyTo(array, arrayIndex); } } /// /// Removes the item with the specified /// from the /// /// /// The representing the item to remove. /// /// /// This method returns false if item is not found in the . /// public bool Remove(KeyValuePair item) { lock (this.innerDictionary) { var inner = this.innerDictionary as IDictionary; return inner.Remove(item); } } /// /// Returns an enumerator that iterates through the .KeyCollection. /// /// /// A /// for the . /// public IEnumerator> GetEnumerator() { lock (this.innerDictionary) { return this.innerDictionary.ToList().GetEnumerator(); } } /// /// Returns an enumerator that iterates through the .KeyCollection. /// /// /// A /// for the . /// System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { lock (this.innerDictionary) { return this.innerDictionary.ToArray().GetEnumerator(); } } #endregion } }