< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Collections.Generic.SynchronizedCollection<T>
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Collections/Generic/SynchronizedCollection.cs
Line coverage
42%
Covered lines: 53
Uncovered lines: 73
Coverable lines: 126
Total lines: 312
Line coverage: 42%
Branch coverage
19%
Covered branches: 7
Total branches: 36
Branch coverage: 19.4%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Collections/Generic/SynchronizedCollection.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;
 6using System.Collections.Generic;
 7using System.Reflection;
 8
 9namespace CoreWCF.Collections.Generic
 10{
 11    public class SynchronizedCollection<T> : IList<T>, IList
 12    {
 63113        public SynchronizedCollection()
 14        {
 63115            Items = new List<T>();
 63116            SyncRoot = new object();
 63117        }
 18
 2218219        public SynchronizedCollection(object syncRoot)
 20        {
 2218221            Items = new List<T>();
 2218222            SyncRoot = syncRoot ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(syncRoot));
 2218223        }
 24
 025        public SynchronizedCollection(object syncRoot, IEnumerable<T> list)
 26        {
 027            if (list == null)
 28            {
 029                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(list));
 30            }
 31
 032            Items = new List<T>(list);
 033            SyncRoot = syncRoot ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(syncRoot));
 034        }
 35
 64136        public SynchronizedCollection(object syncRoot, params T[] list)
 37        {
 64138            if (list == null)
 39            {
 040                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(list));
 41            }
 42
 64143            Items = new List<T>(list.Length);
 128244            foreach (T t in list)
 45            {
 046                Items.Add(t);
 47            }
 48
 64149            SyncRoot = syncRoot ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(syncRoot));
 64150        }
 51
 52        public int Count
 53        {
 15771954            get { lock (SyncRoot) { return Items.Count; } }
 55        }
 56
 19656757        protected List<T> Items { get; }
 58
 12064159        public object SyncRoot { get; }
 60
 61        public T this[int index]
 62        {
 63            get
 64            {
 1783665                lock (SyncRoot)
 66                {
 1783667                    return Items[index];
 68                }
 1783669            }
 70            set
 71            {
 072                lock (SyncRoot)
 73                {
 074                    if (index < 0 || index >= Items.Count)
 75                    {
 076                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof
 077                                                    SR.Format(SR.ValueMustBeInRange, 0, Items.Count - 1)));
 78                    }
 79
 080                    SetItem(index, value);
 081                }
 082            }
 83        }
 84
 85        public void Add(T item)
 86        {
 1079487            lock (SyncRoot)
 88            {
 1079489                int index = Items.Count;
 1079490                InsertItem(index, item);
 1079491            }
 1079492        }
 93
 94        public void Clear()
 95        {
 3396            lock (SyncRoot)
 97            {
 3398                ClearItems();
 3399            }
 33100        }
 101
 102        public void CopyTo(T[] array, int index)
 103        {
 1186104            lock (SyncRoot)
 105            {
 1186106                Items.CopyTo(array, index);
 1186107            }
 1186108        }
 109
 110        public bool Contains(T item)
 111        {
 0112            lock (SyncRoot)
 113            {
 0114                return Items.Contains(item);
 115            }
 0116        }
 117
 118        public IEnumerator<T> GetEnumerator()
 119        {
 7296120            lock (SyncRoot)
 121            {
 7296122                return Items.GetEnumerator();
 123            }
 7296124        }
 125
 126        public int IndexOf(T item)
 127        {
 0128            lock (SyncRoot)
 129            {
 0130                return InternalIndexOf(item);
 131            }
 0132        }
 133
 134        public void Insert(int index, T item)
 135        {
 0136            lock (SyncRoot)
 137            {
 0138                if (index < 0 || index > Items.Count)
 139                {
 0140                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(ind
 0141                                                SR.Format(SR.ValueMustBeInRange, 0, Items.Count - 1)));
 142                }
 143
 0144                InsertItem(index, item);
 0145            }
 0146        }
 147
 148        private int InternalIndexOf(T item)
 149        {
 1906150            int count = Items.Count;
 151
 3812152            for (int i = 0; i < count; i++)
 153            {
 1906154                if (Equals(Items[i], item))
 155                {
 1906156                    return i;
 157                }
 158            }
 0159            return -1;
 160        }
 161
 162        public bool Remove(T item)
 163        {
 1906164            lock (SyncRoot)
 165            {
 1906166                int index = InternalIndexOf(item);
 1906167                if (index < 0)
 168                {
 0169                    return false;
 170                }
 171
 1906172                RemoveItem(index);
 1906173                return true;
 174            }
 1906175        }
 176
 177        public void RemoveAt(int index)
 178        {
 0179            lock (SyncRoot)
 180            {
 0181                if (index < 0 || index >= Items.Count)
 182                {
 0183                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(ind
 0184                                                SR.Format(SR.ValueMustBeInRange, 0, Items.Count - 1)));
 185                }
 186
 0187                RemoveItem(index);
 0188            }
 0189        }
 190
 191        protected virtual void ClearItems()
 192        {
 33193            Items.Clear();
 33194        }
 195
 196        protected virtual void InsertItem(int index, T item)
 197        {
 10794198            Items.Insert(index, item);
 10794199        }
 200
 201        protected virtual void RemoveItem(int index)
 202        {
 1906203            Items.RemoveAt(index);
 1906204        }
 205
 206        protected virtual void SetItem(int index, T item)
 207        {
 0208            Items[index] = item;
 0209        }
 210
 211        bool ICollection<T>.IsReadOnly
 212        {
 0213            get { return false; }
 214        }
 215
 216        IEnumerator IEnumerable.GetEnumerator()
 217        {
 6218            return ((IList)Items).GetEnumerator();
 219        }
 220
 221        bool ICollection.IsSynchronized
 222        {
 0223            get { return true; }
 224        }
 225
 226        object ICollection.SyncRoot
 227        {
 0228            get { return SyncRoot; }
 229        }
 230
 231        void ICollection.CopyTo(Array array, int index)
 232        {
 0233            lock (SyncRoot)
 234            {
 0235                ((IList)Items).CopyTo(array, index);
 0236            }
 0237        }
 238
 239        object IList.this[int index]
 240        {
 241            get
 242            {
 0243                return this[index];
 244            }
 245            set
 246            {
 0247                VerifyValueType(value);
 0248                this[index] = (T)value;
 0249            }
 250        }
 251
 252        bool IList.IsReadOnly
 253        {
 0254            get { return false; }
 255        }
 256
 257        bool IList.IsFixedSize
 258        {
 0259            get { return false; }
 260        }
 261
 262        int IList.Add(object value)
 263        {
 0264            VerifyValueType(value);
 265
 0266            lock (SyncRoot)
 267            {
 0268                Add((T)value);
 0269                return Count - 1;
 270            }
 0271        }
 272
 273        bool IList.Contains(object value)
 274        {
 0275            VerifyValueType(value);
 0276            return Contains((T)value);
 277        }
 278
 279        int IList.IndexOf(object value)
 280        {
 0281            VerifyValueType(value);
 0282            return IndexOf((T)value);
 283        }
 284
 285        void IList.Insert(int index, object value)
 286        {
 0287            VerifyValueType(value);
 0288            Insert(index, (T)value);
 0289        }
 290
 291        void IList.Remove(object value)
 292        {
 0293            VerifyValueType(value);
 0294            Remove((T)value);
 0295        }
 296
 297        private static void VerifyValueType(object value)
 298        {
 0299            if (value == null)
 300            {
 0301                if (typeof(T).GetTypeInfo().IsValueType)
 302                {
 0303                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.SynchronizedCollectionWrongTypeNull)
 304                }
 305            }
 0306            else if (!(value is T))
 307            {
 0308                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.Format(SR.SynchronizedCollectionWrongTyp
 309            }
 0310        }
 311    }
 312}