< Summary

Class:Microsoft.Azure.Batch.ConcurrentChangeTrackedList`1
Assembly:Microsoft.Azure.Batch
File(s):C:\Git\azure-sdk-for-net\sdk\batch\Microsoft.Azure.Batch\src\ConcurrentChangeTrackedList.cs
Covered lines:68
Uncovered lines:21
Coverable lines:89
Total lines:311
Line coverage:76.4% (68 of 89)
Covered branches:6
Total branches:8
Branch coverage:75% (6 of 8)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor()-100%100%
.ctor(...)-100%100%
get_HasBeenModified()-100%100%
get_IsReadOnly()-100%100%
GetEnumerator()-100%100%
System.Collections.IEnumerable.GetEnumerator()-100%100%
Add(...)-100%100%
Clear()-100%100%
Contains(...)-100%100%
CopyTo(...)-0%100%
Remove(...)-100%100%
get_Count()-100%100%
IndexOf(...)-100%100%
Insert(...)-0%100%
RemoveAt(...)-100%100%
get_Item(...)-100%100%
set_Item(...)-0%100%
AddRange(...)-0%0%
AsReadOnly()-100%100%
TransformEnumerableToConcurrentList(...)-100%100%
ThrowOnReadOnly()-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\batch\Microsoft.Azure.Batch\src\ConcurrentChangeTrackedList.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License. See License.txt in the project root for license information.
 3
 4using System;
 5using System.Collections;
 6using System.Collections.Generic;
 7using System.Collections.ObjectModel;
 8
 9namespace Microsoft.Azure.Batch
 10{
 11    internal class ConcurrentChangeTrackedList<T> : IList<T>, IPropertyMetadata, IReadOnlyList<T>
 12    {
 13        protected readonly IList<T> _list;
 1629414        protected readonly object _listLock = new object();
 15
 16        protected bool _hasBeenModified = false;
 17
 218        public ConcurrentChangeTrackedList()
 19        {
 220            this._list = new List<T>();
 221            this.IsReadOnly = false;
 222        }
 23
 1629224        public ConcurrentChangeTrackedList(IEnumerable<T> other, bool isReadOnly = false)
 25        {
 1629226            this._list = new List<T>();
 27
 17938228            foreach (T item in other)
 29            {
 7339930                this._list.Add(item);
 31            }
 1629232            this.IsReadOnly = isReadOnly;
 1629233        }
 34
 35        #region IPropertyMetadata
 36
 37        public virtual bool HasBeenModified
 38        {
 39            get
 40            {
 341                lock (this._listLock)
 42                {
 343                    return this._hasBeenModified;
 44                }
 345            }
 46        }
 47
 2495648        public virtual bool IsReadOnly { get; set; }
 49
 50        #endregion
 51
 52        #region IEnumerable<T>
 53
 54        public IEnumerator<T> GetEnumerator()
 55        {
 4250456            lock (this._listLock)
 57            {
 58                //Clone the list for enumeration -- this is best for "small" lists.  If dealing with larger lists, we sh
 4250459                IReadOnlyList<T> copy = new List<T>(this._list);
 4250460                return copy.GetEnumerator();
 61            }
 4250462        }
 63
 64        #endregion
 65
 66        #region IEnumerable
 67
 68        IEnumerator IEnumerable.GetEnumerator()
 69        {
 270            return this.GetEnumerator();
 71        }
 72
 73        #endregion
 74
 75        #region IList
 76
 77        public void Add(T item)
 78        {
 6179            this.ThrowOnReadOnly();
 80
 5881            lock (this._listLock)
 82            {
 5883                this._list.Add(item);
 5884                this._hasBeenModified = true;
 5885            }
 5886        }
 87
 88        public void Clear()
 89        {
 190            this.ThrowOnReadOnly();
 91
 192            lock (this._listLock)
 93            {
 194                this._list.Clear();
 195                this._hasBeenModified = true;
 196            }
 197        }
 98
 99        public bool Contains(T item)
 100        {
 1101            lock (this._listLock)
 102            {
 1103                bool contains = this._list.Contains(item);
 1104                return contains;
 105            }
 1106        }
 107
 108        public void CopyTo(T[] array, int arrayIndex)
 109        {
 0110            lock (this._listLock)
 111            {
 0112                this._list.CopyTo(array, arrayIndex);
 0113            }
 0114        }
 115
 116        public bool Remove(T item)
 117        {
 1118            this.ThrowOnReadOnly();
 119
 1120            lock (this._listLock)
 121            {
 1122                bool result = this._list.Remove(item);
 1123                this._hasBeenModified = true;
 1124                return result;
 125            }
 1126        }
 127
 128        public int Count
 129        {
 130            get
 131            {
 33132                lock (this._listLock)
 133                {
 33134                    int count = this._list.Count;
 33135                    return count;
 136                }
 33137            }
 138        }
 139
 140        public int IndexOf(T item)
 141        {
 1142            lock (this._listLock)
 143            {
 1144                int index = this._list.IndexOf(item);
 1145                return index;
 146            }
 1147        }
 148
 149        public void Insert(int index, T item)
 150        {
 0151            this.ThrowOnReadOnly();
 152
 0153            lock (this._listLock)
 154            {
 0155                this._list.Insert(index, item);
 0156                this._hasBeenModified = true;
 0157            }
 0158        }
 159
 160        public void RemoveAt(int index)
 161        {
 48162            this.ThrowOnReadOnly();
 163
 48164            lock (this._listLock)
 165            {
 48166                this._list.RemoveAt(index);
 41167                this._hasBeenModified = true;
 41168            }
 41169        }
 170
 171        public T this[int index]
 172        {
 173            get
 174            {
 37175                lock (this._listLock)
 176                {
 37177                    T result = this._list[index];
 37178                    return result;
 179                }
 37180            }
 181
 182            set
 183            {
 0184                this.ThrowOnReadOnly();
 185
 0186                lock (this._listLock)
 187                {
 0188                    this._list[index] = value;
 0189                    this._hasBeenModified = true;
 0190                }
 0191            }
 192        }
 193
 194        #endregion
 195
 196        public void AddRange(IEnumerable<T> items)
 197        {
 0198            this.ThrowOnReadOnly();
 199
 0200            lock (this._listLock)
 201            {
 0202                foreach (T item in items)
 203                {
 0204                    this._list.Add(item);
 205                }
 206            }
 0207        }
 208
 209        public IReadOnlyList<T> AsReadOnly()
 210        {
 211            //As per http://msdn.microsoft.com/en-us/library/ms132474%28v=vs.110%29.aspx this is a wrapper around the co
 212            //will reflect any changes made to the underlying list
 1213            return new ReadOnlyCollection<T>(this._list);
 214        }
 215
 216        public static ConcurrentChangeTrackedList<T> TransformEnumerableToConcurrentList(IEnumerable<T> enumerable)
 217        {
 428218            if (enumerable == null)
 219            {
 5220                return null;
 221            }
 222
 423223            ConcurrentChangeTrackedList<T> list = new ConcurrentChangeTrackedList<T>(enumerable);
 423224            return list;
 225        }
 226
 227        #region Private helpers
 228
 229        private void ThrowOnReadOnly()
 230        {
 111231            if (this.IsReadOnly)
 232            {
 3233                throw new InvalidOperationException(BatchErrorMessages.GeneralObjectInInvalidState);
 234            }
 108235        }
 236
 237        #endregion
 238
 239    }
 240
 241    internal sealed class ConcurrentChangeTrackedModifiableList<T> : ConcurrentChangeTrackedList<T> where T : IPropertyM
 242    {
 243        public ConcurrentChangeTrackedModifiableList()
 244        {
 245        }
 246
 247        public ConcurrentChangeTrackedModifiableList(IEnumerable<T> other, bool isReadOnly = false) : base(other, isRead
 248        {
 249        }
 250
 251        #region IModifiable
 252
 253        public override bool HasBeenModified
 254        {
 255            get
 256            {
 257                lock (this._listLock)
 258                {
 259                    //Check to see if this object has been modified
 260                    if (this._hasBeenModified)
 261                    {
 262                        return true;
 263                    }
 264
 265                    //Check to see if the children objects have been modified
 266                    foreach (T item in this._list)
 267                    {
 268                        if (item != null && item.HasBeenModified)
 269                        {
 270                            return true;
 271                        }
 272                    }
 273
 274                    return false;
 275                }
 276            }
 277        }
 278
 279        public override bool IsReadOnly
 280        {
 281            get
 282            {
 283                return base.IsReadOnly;
 284            }
 285            set
 286            {
 287                base.IsReadOnly = value;
 288                foreach (T readOnlyItem in this)
 289                {
 290                    if (readOnlyItem != null)
 291                    {
 292                        readOnlyItem.IsReadOnly = value;
 293                    }
 294                }
 295            }
 296        }
 297
 298        #endregion
 299
 300        public static ConcurrentChangeTrackedModifiableList<T> TransformEnumerableToConcurrentModifiableList(IEnumerable
 301        {
 302            if (enumerable == null)
 303            {
 304                return null;
 305            }
 306
 307            ConcurrentChangeTrackedModifiableList<T> list = new ConcurrentChangeTrackedModifiableList<T>(enumerable);
 308            return list;
 309        }
 310    }
 311}