< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Collections.Generic.SynchronizedKeyedCollection<T1, T2>
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Collections/Generic/SynchronizedKeyedCollection.cs
Line coverage
33%
Covered lines: 40
Uncovered lines: 79
Coverable lines: 119
Total lines: 315
Line coverage: 33.6%
Branch coverage
23%
Covered branches: 19
Total branches: 80
Branch coverage: 23.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor()100%11100%
.ctor(...)100%11100%
.ctor(...)0%220%
.ctor(...)0%660%
AddKey(...)50%6655.55%
ChangeItemKey(...)0%880%
ClearItems()50%2280%
Contains(...)80%101083.33%
ContainsItem(...)0%660%
CreateDictionary()0%440%
InsertItem(...)100%22100%
Remove(...)0%10100%
RemoveItem(...)0%220%
RemoveKey(...)0%440%
SetItem(...)0%10100%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Collections/Generic/SynchronizedKeyedCollection.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System;
 5using System.Collections.Generic;
 6using CoreWCF.Runtime;
 7
 8namespace CoreWCF.Collections.Generic
 9{
 10    public abstract class SynchronizedKeyedCollection<K, T> : SynchronizedCollection<T>
 11    {
 12        private const int DefaultThreshold = 0;
 13        private readonly IEqualityComparer<K> _comparer;
 14        private Dictionary<K, T> _dictionary;
 15        private int _keyCount;
 16        private readonly int _threshold;
 17
 58718        protected SynchronizedKeyedCollection()
 19        {
 58720            _comparer = EqualityComparer<K>.Default;
 58721            _threshold = int.MaxValue;
 58722        }
 23
 24        protected SynchronizedKeyedCollection(object syncRoot)
 176025            : base(syncRoot)
 26        {
 176027            _comparer = EqualityComparer<K>.Default;
 176028            _threshold = int.MaxValue;
 176029        }
 30
 31        protected SynchronizedKeyedCollection(object syncRoot, IEqualityComparer<K> comparer)
 032            : base(syncRoot)
 33        {
 034            _comparer = comparer ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(comparer));
 035            _threshold = int.MaxValue;
 036        }
 37
 38        protected SynchronizedKeyedCollection(object syncRoot, IEqualityComparer<K> comparer, int dictionaryCreationThre
 039            : base(syncRoot)
 40        {
 041            if (dictionaryCreationThreshold < -1)
 42            {
 043                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(diction
 044                                                    SR.Format(SR.ValueMustBeInRange, -1, int.MaxValue)));
 45            }
 046            else if (dictionaryCreationThreshold == -1)
 47            {
 048                _threshold = int.MaxValue;
 49            }
 50            else
 51            {
 052                _threshold = dictionaryCreationThreshold;
 53            }
 54
 055            _comparer = comparer ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(comparer));
 056        }
 57
 58        public T this[K key]
 59        {
 60            get
 61            {
 318162                if (key == null)
 63                {
 064                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(key));
 65                }
 66
 318167                lock (SyncRoot)
 68                {
 318169                    if (_dictionary != null)
 70                    {
 071                        return _dictionary[key];
 72                    }
 73
 2882674                    for (int i = 0; i < Items.Count; i++)
 75                    {
 1441376                        T item = Items[i];
 1441377                        if (_comparer.Equals(key, GetKeyForItem(item)))
 78                        {
 318179                            return item;
 80                        }
 81                    }
 82
 083                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new KeyNotFoundException());
 84                }
 318185            }
 86        }
 87
 88        protected IDictionary<K, T> Dictionary
 89        {
 090            get { return _dictionary; }
 91        }
 92
 93        private void AddKey(K key, T item)
 94        {
 370995            if (_dictionary != null)
 96            {
 097                _dictionary.Add(key, item);
 98            }
 370999            else if (_keyCount == _threshold)
 100            {
 0101                CreateDictionary();
 0102                _dictionary.Add(key, item);
 103            }
 104            else
 105            {
 3709106                if (Contains(key))
 107                {
 0108                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.CannotAddTwoItemsWithTheSameKeyToSyn
 109                }
 110
 3709111                _keyCount++;
 112            }
 3709113        }
 114
 115        protected void ChangeItemKey(T item, K newKey)
 116        {
 117            // check if the item exists in the collection
 0118            if (!ContainsItem(item))
 119            {
 0120                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.ItemDoesNotExistInSynchronizedKeyedColle
 121            }
 122
 0123            K oldKey = GetKeyForItem(item);
 0124            if (!_comparer.Equals(newKey, oldKey))
 125            {
 0126                if (newKey != null)
 127                {
 0128                    AddKey(newKey, item);
 129                }
 130
 0131                if (oldKey != null)
 132                {
 0133                    RemoveKey(oldKey);
 134                }
 135            }
 0136        }
 137
 138        protected override void ClearItems()
 139        {
 33140            base.ClearItems();
 141
 33142            if (_dictionary != null)
 143            {
 0144                _dictionary.Clear();
 145            }
 146
 33147            _keyCount = 0;
 33148        }
 149
 150        public bool Contains(K key)
 151        {
 7589152            if (key == null)
 153            {
 0154                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(key));
 155            }
 156
 7589157            lock (SyncRoot)
 158            {
 7589159                if (_dictionary != null)
 160                {
 0161                    return _dictionary.ContainsKey(key);
 162                }
 163
 7589164                if (key != null)
 165                {
 59600166                    for (int i = 0; i < Items.Count; i++)
 167                    {
 25390168                        T item = Items[i];
 25390169                        if (_comparer.Equals(key, GetKeyForItem(item)))
 170                        {
 3179171                            return true;
 172                        }
 173                    }
 174                }
 4410175                return false;
 176            }
 7589177        }
 178
 179        private bool ContainsItem(T item)
 180        {
 181            K key;
 0182            if ((_dictionary == null) || ((key = GetKeyForItem(item)) == null))
 183            {
 0184                return Items.Contains(item);
 185            }
 186
 187
 0188            if (_dictionary.TryGetValue(key, out T itemInDict))
 189            {
 0190                return EqualityComparer<T>.Default.Equals(item, itemInDict);
 191            }
 192
 0193            return false;
 194        }
 195
 196        private void CreateDictionary()
 197        {
 0198            _dictionary = new Dictionary<K, T>(_comparer);
 199
 0200            foreach (T item in Items)
 201            {
 0202                K key = GetKeyForItem(item);
 0203                if (key != null)
 204                {
 0205                    _dictionary.Add(key, item);
 206                }
 207            }
 0208        }
 209
 210        protected abstract K GetKeyForItem(T item);
 211
 212        protected override void InsertItem(int index, T item)
 213        {
 3709214            K key = GetKeyForItem(item);
 215
 3709216            if (key != null)
 217            {
 3709218                AddKey(key, item);
 219            }
 220
 3709221            base.InsertItem(index, item);
 3709222        }
 223
 224        public bool Remove(K key)
 225        {
 0226            if (key == null)
 227            {
 0228                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(key));
 229            }
 230
 0231            lock (SyncRoot)
 232            {
 0233                if (_dictionary != null)
 234                {
 0235                    if (_dictionary.ContainsKey(key))
 236                    {
 0237                        return Remove(_dictionary[key]);
 238                    }
 239                    else
 240                    {
 0241                        return false;
 242                    }
 243                }
 244                else
 245                {
 0246                    for (int i = 0; i < Items.Count; i++)
 247                    {
 0248                        if (_comparer.Equals(key, GetKeyForItem(Items[i])))
 249                        {
 0250                            RemoveItem(i);
 0251                            return true;
 252                        }
 253                    }
 0254                    return false;
 255                }
 256            }
 0257        }
 258
 259        protected override void RemoveItem(int index)
 260        {
 0261            K key = GetKeyForItem(Items[index]);
 262
 0263            if (key != null)
 264            {
 0265                RemoveKey(key);
 266            }
 267
 0268            base.RemoveItem(index);
 0269        }
 270
 271        private void RemoveKey(K key)
 272        {
 0273            if (!(key != null))
 274            {
 275                Fx.Assert(false, "key shouldn't be null!");
 0276                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(key));
 277            }
 0278            if (_dictionary != null)
 279            {
 0280                _dictionary.Remove(key);
 281            }
 282            else
 283            {
 0284                _keyCount--;
 285            }
 0286        }
 287
 288        protected override void SetItem(int index, T item)
 289        {
 0290            K newKey = GetKeyForItem(item);
 0291            K oldKey = GetKeyForItem(Items[index]);
 292
 0293            if (_comparer.Equals(newKey, oldKey))
 294            {
 0295                if ((newKey != null) && (_dictionary != null))
 296                {
 0297                    _dictionary[newKey] = item;
 298                }
 299            }
 300            else
 301            {
 0302                if (newKey != null)
 303                {
 0304                    AddKey(newKey, item);
 305                }
 306
 0307                if (oldKey != null)
 308                {
 0309                    RemoveKey(oldKey);
 310                }
 311            }
 0312            base.SetItem(index, item);
 0313        }
 314    }
 315}