| | 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 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using System.Collections.ObjectModel; |
| | 8 | |
|
| | 9 | | namespace Microsoft.Azure.Batch |
| | 10 | | { |
| | 11 | | internal class ConcurrentChangeTrackedList<T> : IList<T>, IPropertyMetadata, IReadOnlyList<T> |
| | 12 | | { |
| | 13 | | protected readonly IList<T> _list; |
| 16294 | 14 | | protected readonly object _listLock = new object(); |
| | 15 | |
|
| | 16 | | protected bool _hasBeenModified = false; |
| | 17 | |
|
| 2 | 18 | | public ConcurrentChangeTrackedList() |
| | 19 | | { |
| 2 | 20 | | this._list = new List<T>(); |
| 2 | 21 | | this.IsReadOnly = false; |
| 2 | 22 | | } |
| | 23 | |
|
| 16292 | 24 | | public ConcurrentChangeTrackedList(IEnumerable<T> other, bool isReadOnly = false) |
| | 25 | | { |
| 16292 | 26 | | this._list = new List<T>(); |
| | 27 | |
|
| 179382 | 28 | | foreach (T item in other) |
| | 29 | | { |
| 73399 | 30 | | this._list.Add(item); |
| | 31 | | } |
| 16292 | 32 | | this.IsReadOnly = isReadOnly; |
| 16292 | 33 | | } |
| | 34 | |
|
| | 35 | | #region IPropertyMetadata |
| | 36 | |
|
| | 37 | | public virtual bool HasBeenModified |
| | 38 | | { |
| | 39 | | get |
| | 40 | | { |
| 3 | 41 | | lock (this._listLock) |
| | 42 | | { |
| 3 | 43 | | return this._hasBeenModified; |
| | 44 | | } |
| 3 | 45 | | } |
| | 46 | | } |
| | 47 | |
|
| 24956 | 48 | | public virtual bool IsReadOnly { get; set; } |
| | 49 | |
|
| | 50 | | #endregion |
| | 51 | |
|
| | 52 | | #region IEnumerable<T> |
| | 53 | |
|
| | 54 | | public IEnumerator<T> GetEnumerator() |
| | 55 | | { |
| 42504 | 56 | | lock (this._listLock) |
| | 57 | | { |
| | 58 | | //Clone the list for enumeration -- this is best for "small" lists. If dealing with larger lists, we sh |
| 42504 | 59 | | IReadOnlyList<T> copy = new List<T>(this._list); |
| 42504 | 60 | | return copy.GetEnumerator(); |
| | 61 | | } |
| 42504 | 62 | | } |
| | 63 | |
|
| | 64 | | #endregion |
| | 65 | |
|
| | 66 | | #region IEnumerable |
| | 67 | |
|
| | 68 | | IEnumerator IEnumerable.GetEnumerator() |
| | 69 | | { |
| 2 | 70 | | return this.GetEnumerator(); |
| | 71 | | } |
| | 72 | |
|
| | 73 | | #endregion |
| | 74 | |
|
| | 75 | | #region IList |
| | 76 | |
|
| | 77 | | public void Add(T item) |
| | 78 | | { |
| 61 | 79 | | this.ThrowOnReadOnly(); |
| | 80 | |
|
| 58 | 81 | | lock (this._listLock) |
| | 82 | | { |
| 58 | 83 | | this._list.Add(item); |
| 58 | 84 | | this._hasBeenModified = true; |
| 58 | 85 | | } |
| 58 | 86 | | } |
| | 87 | |
|
| | 88 | | public void Clear() |
| | 89 | | { |
| 1 | 90 | | this.ThrowOnReadOnly(); |
| | 91 | |
|
| 1 | 92 | | lock (this._listLock) |
| | 93 | | { |
| 1 | 94 | | this._list.Clear(); |
| 1 | 95 | | this._hasBeenModified = true; |
| 1 | 96 | | } |
| 1 | 97 | | } |
| | 98 | |
|
| | 99 | | public bool Contains(T item) |
| | 100 | | { |
| 1 | 101 | | lock (this._listLock) |
| | 102 | | { |
| 1 | 103 | | bool contains = this._list.Contains(item); |
| 1 | 104 | | return contains; |
| | 105 | | } |
| 1 | 106 | | } |
| | 107 | |
|
| | 108 | | public void CopyTo(T[] array, int arrayIndex) |
| | 109 | | { |
| 0 | 110 | | lock (this._listLock) |
| | 111 | | { |
| 0 | 112 | | this._list.CopyTo(array, arrayIndex); |
| 0 | 113 | | } |
| 0 | 114 | | } |
| | 115 | |
|
| | 116 | | public bool Remove(T item) |
| | 117 | | { |
| 1 | 118 | | this.ThrowOnReadOnly(); |
| | 119 | |
|
| 1 | 120 | | lock (this._listLock) |
| | 121 | | { |
| 1 | 122 | | bool result = this._list.Remove(item); |
| 1 | 123 | | this._hasBeenModified = true; |
| 1 | 124 | | return result; |
| | 125 | | } |
| 1 | 126 | | } |
| | 127 | |
|
| | 128 | | public int Count |
| | 129 | | { |
| | 130 | | get |
| | 131 | | { |
| 33 | 132 | | lock (this._listLock) |
| | 133 | | { |
| 33 | 134 | | int count = this._list.Count; |
| 33 | 135 | | return count; |
| | 136 | | } |
| 33 | 137 | | } |
| | 138 | | } |
| | 139 | |
|
| | 140 | | public int IndexOf(T item) |
| | 141 | | { |
| 1 | 142 | | lock (this._listLock) |
| | 143 | | { |
| 1 | 144 | | int index = this._list.IndexOf(item); |
| 1 | 145 | | return index; |
| | 146 | | } |
| 1 | 147 | | } |
| | 148 | |
|
| | 149 | | public void Insert(int index, T item) |
| | 150 | | { |
| 0 | 151 | | this.ThrowOnReadOnly(); |
| | 152 | |
|
| 0 | 153 | | lock (this._listLock) |
| | 154 | | { |
| 0 | 155 | | this._list.Insert(index, item); |
| 0 | 156 | | this._hasBeenModified = true; |
| 0 | 157 | | } |
| 0 | 158 | | } |
| | 159 | |
|
| | 160 | | public void RemoveAt(int index) |
| | 161 | | { |
| 48 | 162 | | this.ThrowOnReadOnly(); |
| | 163 | |
|
| 48 | 164 | | lock (this._listLock) |
| | 165 | | { |
| 48 | 166 | | this._list.RemoveAt(index); |
| 41 | 167 | | this._hasBeenModified = true; |
| 41 | 168 | | } |
| 41 | 169 | | } |
| | 170 | |
|
| | 171 | | public T this[int index] |
| | 172 | | { |
| | 173 | | get |
| | 174 | | { |
| 37 | 175 | | lock (this._listLock) |
| | 176 | | { |
| 37 | 177 | | T result = this._list[index]; |
| 37 | 178 | | return result; |
| | 179 | | } |
| 37 | 180 | | } |
| | 181 | |
|
| | 182 | | set |
| | 183 | | { |
| 0 | 184 | | this.ThrowOnReadOnly(); |
| | 185 | |
|
| 0 | 186 | | lock (this._listLock) |
| | 187 | | { |
| 0 | 188 | | this._list[index] = value; |
| 0 | 189 | | this._hasBeenModified = true; |
| 0 | 190 | | } |
| 0 | 191 | | } |
| | 192 | | } |
| | 193 | |
|
| | 194 | | #endregion |
| | 195 | |
|
| | 196 | | public void AddRange(IEnumerable<T> items) |
| | 197 | | { |
| 0 | 198 | | this.ThrowOnReadOnly(); |
| | 199 | |
|
| 0 | 200 | | lock (this._listLock) |
| | 201 | | { |
| 0 | 202 | | foreach (T item in items) |
| | 203 | | { |
| 0 | 204 | | this._list.Add(item); |
| | 205 | | } |
| | 206 | | } |
| 0 | 207 | | } |
| | 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 |
| 1 | 213 | | return new ReadOnlyCollection<T>(this._list); |
| | 214 | | } |
| | 215 | |
|
| | 216 | | public static ConcurrentChangeTrackedList<T> TransformEnumerableToConcurrentList(IEnumerable<T> enumerable) |
| | 217 | | { |
| 428 | 218 | | if (enumerable == null) |
| | 219 | | { |
| 5 | 220 | | return null; |
| | 221 | | } |
| | 222 | |
|
| 423 | 223 | | ConcurrentChangeTrackedList<T> list = new ConcurrentChangeTrackedList<T>(enumerable); |
| 423 | 224 | | return list; |
| | 225 | | } |
| | 226 | |
|
| | 227 | | #region Private helpers |
| | 228 | |
|
| | 229 | | private void ThrowOnReadOnly() |
| | 230 | | { |
| 111 | 231 | | if (this.IsReadOnly) |
| | 232 | | { |
| 3 | 233 | | throw new InvalidOperationException(BatchErrorMessages.GeneralObjectInInvalidState); |
| | 234 | | } |
| 108 | 235 | | } |
| | 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 | | } |