diff --git a/ICD.Common.Utils/Collections/BiDictionary.cs b/ICD.Common.Utils/Collections/BiDictionary.cs index 3467d01..60e3823 100644 --- a/ICD.Common.Utils/Collections/BiDictionary.cs +++ b/ICD.Common.Utils/Collections/BiDictionary.cs @@ -1,6 +1,7 @@ using System; using System.Collections; using System.Collections.Generic; +using ICD.Common.Properties; namespace ICD.Common.Utils.Collections { @@ -20,8 +21,10 @@ namespace ICD.Common.Utils.Collections public bool IsReadOnly { get { return false; } } + [NotNull] public ICollection Keys { get { return m_KeyToValue.Keys; } } + [NotNull] public ICollection Values { get { return m_ValueToKey.Keys; } } #endregion @@ -30,21 +33,20 @@ namespace ICD.Common.Utils.Collections /// Constructor. /// public BiDictionary() - : this(null) { + m_KeyToValue = new Dictionary(); + m_ValueToKey = new Dictionary(); } /// /// Constructor. /// /// - public BiDictionary(Dictionary dict) + public BiDictionary([NotNull] Dictionary dict) + : this() { - m_KeyToValue = new Dictionary(); - m_ValueToKey = new Dictionary(); - if (dict == null) - return; + throw new ArgumentNullException("dict"); foreach (KeyValuePair kvp in dict) Add(kvp.Key, kvp.Value); @@ -58,24 +60,31 @@ namespace ICD.Common.Utils.Collections m_ValueToKey.Clear(); } - public bool ContainsKey(TKey key) + public bool ContainsKey([NotNull] TKey key) { +// ReSharper disable CompareNonConstrainedGenericWithNull + if (key == null) +// ReSharper restore CompareNonConstrainedGenericWithNull + throw new ArgumentNullException("key"); + return m_KeyToValue.ContainsKey(key); } - public bool ContainsValue(TValue value) + public bool ContainsValue([NotNull] TValue value) { return m_ValueToKey.ContainsKey(value); } - public void Add(TKey key, TValue value) + public void Add([NotNull] TKey key, [NotNull] TValue value) { -// ReSharper disable once CompareNonConstrainedGenericWithNull +// ReSharper disable CompareNonConstrainedGenericWithNull if (key == null) +// ReSharper restore CompareNonConstrainedGenericWithNull throw new ArgumentNullException("key"); -// ReSharper disable once CompareNonConstrainedGenericWithNull +// ReSharper disable CompareNonConstrainedGenericWithNull if (value == null) +// ReSharper restore CompareNonConstrainedGenericWithNull throw new ArgumentNullException("value"); if (ContainsKey(key)) @@ -88,14 +97,16 @@ namespace ICD.Common.Utils.Collections m_ValueToKey.Add(value, key); } - public void Set(TKey key, TValue value) + public void Set([NotNull] TKey key, [NotNull] TValue value) { -// ReSharper disable once CompareNonConstrainedGenericWithNull +// ReSharper disable CompareNonConstrainedGenericWithNull if (key == null) +// ReSharper restore CompareNonConstrainedGenericWithNull throw new ArgumentNullException("key"); -// ReSharper disable once CompareNonConstrainedGenericWithNull +// ReSharper disable CompareNonConstrainedGenericWithNull if (value == null) +// ReSharper restore CompareNonConstrainedGenericWithNull throw new ArgumentNullException("value"); RemoveKey(key); @@ -104,18 +115,35 @@ namespace ICD.Common.Utils.Collections Add(key, value); } - public TKey GetKey(TValue value) + [NotNull] + public TKey GetKey([NotNull] TValue value) { +// ReSharper disable CompareNonConstrainedGenericWithNull + if (value == null) +// ReSharper restore CompareNonConstrainedGenericWithNull + throw new ArgumentNullException("value"); + return m_ValueToKey[value]; } - public TValue GetValue(TKey key) + [NotNull] + public TValue GetValue([NotNull] TKey key) { +// ReSharper disable CompareNonConstrainedGenericWithNull + if (key == null) +// ReSharper restore CompareNonConstrainedGenericWithNull + throw new ArgumentNullException("key"); + return m_KeyToValue[key]; } - public bool RemoveKey(TKey key) + public bool RemoveKey([NotNull] TKey key) { +// ReSharper disable CompareNonConstrainedGenericWithNull + if (key == null) +// ReSharper restore CompareNonConstrainedGenericWithNull + throw new ArgumentNullException("key"); + if (!ContainsKey(key)) return false; @@ -127,8 +155,13 @@ namespace ICD.Common.Utils.Collections return true; } - public bool RemoveValue(TValue value) + public bool RemoveValue([NotNull] TValue value) { +// ReSharper disable CompareNonConstrainedGenericWithNull + if (value == null) +// ReSharper restore CompareNonConstrainedGenericWithNull + throw new ArgumentNullException("value"); + if (!ContainsValue(value)) return false; @@ -137,13 +170,23 @@ namespace ICD.Common.Utils.Collections return RemoveKey(key); } - public bool TryGetValue(TKey key, out TValue value) + public bool TryGetValue([NotNull] TKey key, out TValue value) { +// ReSharper disable CompareNonConstrainedGenericWithNull + if (key == null) +// ReSharper restore CompareNonConstrainedGenericWithNull + throw new ArgumentNullException("key"); + return m_KeyToValue.TryGetValue(key, out value); } - public bool TryGetKey(TValue value, out TKey key) + public bool TryGetKey([NotNull] TValue value, out TKey key) { +// ReSharper disable CompareNonConstrainedGenericWithNull + if (value == null) +// ReSharper restore CompareNonConstrainedGenericWithNull + throw new ArgumentNullException("value"); + return m_ValueToKey.TryGetValue(value, out key); } @@ -151,9 +194,10 @@ namespace ICD.Common.Utils.Collections #region IDictionary - TValue IDictionary.this[TKey key] { get { return GetValue(key); } set { Set(key, value); } } + [NotNull] + TValue IDictionary.this[[NotNull] TKey key] { get { return GetValue(key); } set { Set(key, value); } } - bool IDictionary.Remove(TKey key) + bool IDictionary.Remove([NotNull] TKey key) { return RemoveKey(key); } @@ -177,7 +221,7 @@ namespace ICD.Common.Utils.Collections return RemoveKey(item.Key); } - void ICollection>.CopyTo(KeyValuePair[] array, int arrayIndex) + void ICollection>.CopyTo([NotNull] KeyValuePair[] array, int arrayIndex) { (m_KeyToValue as IDictionary).CopyTo(array, arrayIndex); } @@ -186,11 +230,13 @@ namespace ICD.Common.Utils.Collections #region IEnumerable + [NotNull] public IEnumerator> GetEnumerator() { return m_KeyToValue.GetEnumerator(); } + [NotNull] IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); diff --git a/ICD.Common.Utils/Collections/IcdHashSet.cs b/ICD.Common.Utils/Collections/IcdHashSet.cs index 8f46f92..af85962 100644 --- a/ICD.Common.Utils/Collections/IcdHashSet.cs +++ b/ICD.Common.Utils/Collections/IcdHashSet.cs @@ -49,7 +49,7 @@ namespace ICD.Common.Utils.Collections /// Constructor. /// /// - public IcdHashSet(IEnumerable items) + public IcdHashSet([NotNull] IEnumerable items) : this(EqualityComparer.Default, items) { } @@ -58,7 +58,7 @@ namespace ICD.Common.Utils.Collections /// Constructor. /// /// - public IcdHashSet(IEqualityComparer comparer) + public IcdHashSet([NotNull] IEqualityComparer comparer) : this(comparer, Enumerable.Empty()) { } @@ -68,7 +68,7 @@ namespace ICD.Common.Utils.Collections /// /// /// - public IcdHashSet(IEqualityComparer comparer, IEnumerable items) + public IcdHashSet([NotNull] IEqualityComparer comparer, [NotNull] IEnumerable items) { if (comparer == null) throw new ArgumentNullException("comparer"); @@ -91,7 +91,8 @@ namespace ICD.Common.Utils.Collections /// /// [PublicAPI] - public IcdHashSet Union(IEnumerable set) + [NotNull] + public IcdHashSet Union([NotNull] IEnumerable set) { if (set == null) throw new ArgumentNullException("set"); @@ -108,7 +109,8 @@ namespace ICD.Common.Utils.Collections /// /// [PublicAPI] - public IcdHashSet Subtract(IEnumerable set) + [NotNull] + public IcdHashSet Subtract([NotNull] IEnumerable set) { if (set == null) throw new ArgumentNullException("set"); @@ -127,7 +129,8 @@ namespace ICD.Common.Utils.Collections /// /// [PublicAPI] - public IcdHashSet Intersection(IEnumerable set) + [NotNull] + public IcdHashSet Intersection([NotNull] IEnumerable set) { if (set == null) throw new ArgumentNullException("set"); @@ -146,7 +149,8 @@ namespace ICD.Common.Utils.Collections /// /// [PublicAPI] - public IcdHashSet NonIntersection(IEnumerable set) + [NotNull] + public IcdHashSet NonIntersection([NotNull] IEnumerable set) { if (set == null) throw new ArgumentNullException("set"); @@ -170,7 +174,7 @@ namespace ICD.Common.Utils.Collections /// /// [PublicAPI] - public bool IsSubsetOf(IcdHashSet set) + public bool IsSubsetOf([NotNull] IcdHashSet set) { if (set == null) throw new ArgumentNullException("set"); @@ -184,7 +188,7 @@ namespace ICD.Common.Utils.Collections /// /// [PublicAPI] - public bool IsProperSubsetOf(IcdHashSet set) + public bool IsProperSubsetOf([NotNull] IcdHashSet set) { if (set == null) throw new ArgumentNullException("set"); @@ -198,7 +202,7 @@ namespace ICD.Common.Utils.Collections /// /// [PublicAPI] - public bool IsSupersetOf(IcdHashSet set) + public bool IsSupersetOf([NotNull] IcdHashSet set) { if (set == null) throw new ArgumentNullException("set"); @@ -212,7 +216,7 @@ namespace ICD.Common.Utils.Collections /// /// [PublicAPI] - public bool IsProperSupersetOf(IcdHashSet set) + public bool IsProperSupersetOf([NotNull] IcdHashSet set) { if (set == null) throw new ArgumentNullException("set"); @@ -226,7 +230,7 @@ namespace ICD.Common.Utils.Collections /// /// [PublicAPI] - public bool SetEquals(IcdHashSet set) + public bool SetEquals([NotNull] IcdHashSet set) { if (set == null) throw new ArgumentNullException("set"); @@ -243,7 +247,7 @@ namespace ICD.Common.Utils.Collections /// /// /// - public bool Add(T item) + public bool Add([NotNull] T item) { // ReSharper disable CompareNonConstrainedGenericWithNull if (item == null) @@ -261,7 +265,7 @@ namespace ICD.Common.Utils.Collections /// Adds the item to the collection. /// /// - void ICollection.Add(T item) + void ICollection.Add([NotNull] T item) { Add(item); } @@ -270,7 +274,7 @@ namespace ICD.Common.Utils.Collections /// Adds each of the items in the sequence to the collection. /// /// - public void AddRange(IEnumerable items) + public void AddRange([NotNull] IEnumerable items) { if (items == null) throw new ArgumentNullException("items"); @@ -293,7 +297,7 @@ namespace ICD.Common.Utils.Collections /// /// /// - public bool Contains(T item) + public bool Contains([NotNull] T item) { // ReSharper disable CompareNonConstrainedGenericWithNull if (item == null) @@ -307,8 +311,11 @@ namespace ICD.Common.Utils.Collections /// Copies the items of the to an , starting at a particular index. /// /// The one-dimensional that is the destination of the items copied from . The must have zero-based indexing.The zero-based index in at which copying begins. is null. is less than 0. is multidimensional.-or- is equal to or greater than the length of .-or-The number of items in the source is greater than the available space from to the end of the destination .-or-Type T cannot be cast automatically to the type of the destination . - public void CopyTo(T[] array, int arrayIndex) + public void CopyTo([NotNull] T[] array, int arrayIndex) { + if (array == null) + throw new ArgumentNullException("array"); + m_Dict.Keys.CopyTo(array, arrayIndex); } @@ -319,7 +326,7 @@ namespace ICD.Common.Utils.Collections /// true if was successfully removed from the ; otherwise, false. This method also returns false if is not found in the original . /// /// The object to remove from the .The is read-only. - public bool Remove(T item) + public bool Remove([NotNull] T item) { // ReSharper disable CompareNonConstrainedGenericWithNull if (item == null) @@ -333,7 +340,7 @@ namespace ICD.Common.Utils.Collections /// Removes each of the items in the sequence from the collection. /// /// - public void RemoveRange(IEnumerable items) + public void RemoveRange([NotNull] IEnumerable items) { if (items == null) throw new ArgumentNullException("items"); @@ -352,6 +359,7 @@ namespace ICD.Common.Utils.Collections /// A that can be used to iterate through the collection. /// /// 1 + [NotNull] public IEnumerator GetEnumerator() { return m_Dict.Keys.GetEnumerator(); @@ -364,6 +372,7 @@ namespace ICD.Common.Utils.Collections /// An object that can be used to iterate through the collection. /// /// 2 + [NotNull] IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); diff --git a/ICD.Common.Utils/Collections/IcdOrderedDictionary.cs b/ICD.Common.Utils/Collections/IcdOrderedDictionary.cs index 01c88b0..f02f894 100644 --- a/ICD.Common.Utils/Collections/IcdOrderedDictionary.cs +++ b/ICD.Common.Utils/Collections/IcdOrderedDictionary.cs @@ -2,6 +2,7 @@ using System.Collections; using System.Collections.Generic; using System.Linq; +using ICD.Common.Properties; using ICD.Common.Utils.Extensions; namespace ICD.Common.Utils.Collections @@ -19,11 +20,14 @@ namespace ICD.Common.Utils.Collections public bool IsReadOnly { get { return false; } } + [NotNull] public ICollection Keys { get { return m_OrderedKeys; } } + [NotNull] public ICollection Values { get { return m_ValuesOrderedByKey; } } - public TValue this[TKey key] + [CanBeNull] + public TValue this[[NotNull] TKey key] { get { return m_Dictionary[key]; } set @@ -51,7 +55,7 @@ namespace ICD.Common.Utils.Collections /// Constructor. /// /// - public IcdOrderedDictionary(IComparer comparer) + public IcdOrderedDictionary([NotNull] IComparer comparer) : this(comparer, EqualityComparer.Default) { } @@ -61,7 +65,7 @@ namespace ICD.Common.Utils.Collections /// /// /// - public IcdOrderedDictionary(IComparer comparer, IEqualityComparer equalityComparer) + public IcdOrderedDictionary([NotNull] IComparer comparer, [NotNull] IEqualityComparer equalityComparer) { if (comparer == null) throw new ArgumentNullException("comparer"); @@ -79,7 +83,7 @@ namespace ICD.Common.Utils.Collections /// Constructor. /// /// - public IcdOrderedDictionary(IEnumerable> dictionary) + public IcdOrderedDictionary([NotNull] IEnumerable> dictionary) : this() { if (dictionary == null) @@ -91,16 +95,18 @@ namespace ICD.Common.Utils.Collections #region Methods + [NotNull] public IEnumerator> GetEnumerator() { return m_OrderedKeys.Select(k => new KeyValuePair(k, m_Dictionary[k])) .GetEnumerator(); } - public void Add(TKey key, TValue value) + public void Add([NotNull] TKey key, [CanBeNull] TValue value) { -// ReSharper disable once CompareNonConstrainedGenericWithNull +// ReSharper disable CompareNonConstrainedGenericWithNull if (key == null) +// ReSharper restore CompareNonConstrainedGenericWithNull throw new ArgumentNullException("key"); if (m_Dictionary.ContainsKey(key)) @@ -119,15 +125,21 @@ namespace ICD.Common.Utils.Collections m_Dictionary.Clear(); } - public bool ContainsKey(TKey key) + public bool ContainsKey([NotNull] TKey key) { +// ReSharper disable CompareNonConstrainedGenericWithNull + if (key == null) +// ReSharper restore CompareNonConstrainedGenericWithNull + throw new ArgumentNullException("key"); + return m_Dictionary.ContainsKey(key); } - public bool Remove(TKey key) + public bool Remove([NotNull] TKey key) { -// ReSharper disable once CompareNonConstrainedGenericWithNull +// ReSharper disable CompareNonConstrainedGenericWithNull if (key == null) +// ReSharper restore CompareNonConstrainedGenericWithNull throw new ArgumentNullException("key"); if (!m_Dictionary.Remove(key)) @@ -141,8 +153,13 @@ namespace ICD.Common.Utils.Collections return true; } - public bool TryGetValue(TKey key, out TValue value) + public bool TryGetValue([NotNull] TKey key, out TValue value) { +// ReSharper disable CompareNonConstrainedGenericWithNull + if (key == null) +// ReSharper restore CompareNonConstrainedGenericWithNull + throw new ArgumentNullException("key"); + return m_Dictionary.TryGetValue(key, out value); } @@ -150,6 +167,7 @@ namespace ICD.Common.Utils.Collections #region Private Methods + [NotNull] IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); @@ -167,8 +185,11 @@ namespace ICD.Common.Utils.Collections EqualityComparer.Default.Equals(value, item.Value); } - void ICollection>.CopyTo(KeyValuePair[] array, int index) + void ICollection>.CopyTo([NotNull] KeyValuePair[] array, int index) { + if (array == null) + throw new ArgumentNullException("array"); + foreach (KeyValuePair kvp in this) { array.SetValue(kvp, index); diff --git a/ICD.Common.Utils/Collections/PriorityQueue.cs b/ICD.Common.Utils/Collections/PriorityQueue.cs index 8c07ef3..a772417 100644 --- a/ICD.Common.Utils/Collections/PriorityQueue.cs +++ b/ICD.Common.Utils/Collections/PriorityQueue.cs @@ -30,6 +30,7 @@ namespace ICD.Common.Utils.Collections /// /// Gets a reference for locking. /// + [NotNull] public object SyncRoot { get { return this; } } #endregion @@ -59,7 +60,7 @@ namespace ICD.Common.Utils.Collections /// /// [PublicAPI] - public void Enqueue(T item) + public void Enqueue([CanBeNull] T item) { Enqueue(item, int.MaxValue); } @@ -71,16 +72,10 @@ namespace ICD.Common.Utils.Collections /// /// [PublicAPI] - public void Enqueue(T item, int priority) + public void Enqueue([CanBeNull] T item, int priority) { - List queue; - if (!m_PriorityToQueue.TryGetValue(priority, out queue)) - { - queue = new List(); - m_PriorityToQueue.Add(priority, queue); - } - - queue.Add(item); + m_PriorityToQueue.GetOrAddNew(priority, () => new List()) + .Add(item); m_Count++; } @@ -89,18 +84,10 @@ namespace ICD.Common.Utils.Collections /// /// [PublicAPI] - public void EnqueueFirst(T item) + public void EnqueueFirst([CanBeNull] T item) { - const int priority = int.MinValue; - - List queue; - if (!m_PriorityToQueue.TryGetValue(priority, out queue)) - { - queue = new List(); - m_PriorityToQueue.Add(priority, queue); - } - - queue.Insert(0, item); + m_PriorityToQueue.GetOrAddNew(int.MinValue, () => new List()) + .Insert(0, item); m_Count++; } @@ -112,7 +99,7 @@ namespace ICD.Common.Utils.Collections /// /// [PublicAPI] - public void EnqueueRemove(T item, Func remove) + public void EnqueueRemove([CanBeNull] T item, [NotNull] Func remove) { if (remove == null) throw new ArgumentNullException("remove"); @@ -129,7 +116,7 @@ namespace ICD.Common.Utils.Collections /// /// [PublicAPI] - public void EnqueueRemove(T item, Func remove, int priority) + public void EnqueueRemove([CanBeNull] T item, [NotNull] Func remove, int priority) { if (remove == null) throw new ArgumentNullException("remove"); @@ -180,6 +167,7 @@ namespace ICD.Common.Utils.Collections /// /// [PublicAPI] + [CanBeNull] public T Dequeue() { T output; @@ -227,6 +215,7 @@ namespace ICD.Common.Utils.Collections /// Gets an enumerator for the items. /// /// + [NotNull] public IEnumerator GetEnumerator() { return m_PriorityToQueue.Values @@ -239,8 +228,11 @@ namespace ICD.Common.Utils.Collections /// /// /// - public void CopyTo(Array array, int index) + public void CopyTo([NotNull] Array array, int index) { + if (array == null) + throw new ArgumentNullException("array"); + foreach (T item in this) array.SetValue(item, index++); } @@ -253,6 +245,7 @@ namespace ICD.Common.Utils.Collections /// Gets an enumerator for the items. /// /// + [NotNull] IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); diff --git a/ICD.Common.Utils/Collections/RateLimitedEventQueue.cs b/ICD.Common.Utils/Collections/RateLimitedEventQueue.cs index dc4708e..b3cc5bb 100644 --- a/ICD.Common.Utils/Collections/RateLimitedEventQueue.cs +++ b/ICD.Common.Utils/Collections/RateLimitedEventQueue.cs @@ -1,6 +1,7 @@ using System; using System.Collections; using System.Collections.Generic; +using ICD.Common.Properties; using ICD.Common.Utils.EventArguments; using ICD.Common.Utils.Extensions; using ICD.Common.Utils.Timers; @@ -32,6 +33,7 @@ namespace ICD.Common.Utils.Collections bool ICollection.IsSynchronized { get { return true; } } + [NotNull] object ICollection.SyncRoot { get { return this; } } #endregion @@ -72,7 +74,7 @@ namespace ICD.Common.Utils.Collections /// Enqueues the given item. /// /// - public void Enqueue(T item) + public void Enqueue([CanBeNull] T item) { m_QueueSection.Enter(); @@ -158,18 +160,23 @@ namespace ICD.Common.Utils.Collections #region IEnumerable/ICollection + [NotNull] public IEnumerator GetEnumerator() { return m_QueueSection.Execute(() => m_Queue.ToList(m_Queue.Count).GetEnumerator()); } + [NotNull] IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } - void ICollection.CopyTo(Array array, int index) + void ICollection.CopyTo([NotNull] Array array, int index) { + if (array == null) + throw new ArgumentNullException("array"); + m_QueueSection.Enter(); try