| | 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; |
| | 14 | | protected readonly object _listLock = new object(); |
| | 15 | |
|
| | 16 | | protected bool _hasBeenModified = false; |
| | 17 | |
|
| | 18 | | public ConcurrentChangeTrackedList() |
| | 19 | | { |
| | 20 | | this._list = new List<T>(); |
| | 21 | | this.IsReadOnly = false; |
| | 22 | | } |
| | 23 | |
|
| | 24 | | public ConcurrentChangeTrackedList(IEnumerable<T> other, bool isReadOnly = false) |
| | 25 | | { |
| | 26 | | this._list = new List<T>(); |
| | 27 | |
|
| | 28 | | foreach (T item in other) |
| | 29 | | { |
| | 30 | | this._list.Add(item); |
| | 31 | | } |
| | 32 | | this.IsReadOnly = isReadOnly; |
| | 33 | | } |
| | 34 | |
|
| | 35 | | #region IPropertyMetadata |
| | 36 | |
|
| | 37 | | public virtual bool HasBeenModified |
| | 38 | | { |
| | 39 | | get |
| | 40 | | { |
| | 41 | | lock (this._listLock) |
| | 42 | | { |
| | 43 | | return this._hasBeenModified; |
| | 44 | | } |
| | 45 | | } |
| | 46 | | } |
| | 47 | |
|
| | 48 | | public virtual bool IsReadOnly { get; set; } |
| | 49 | |
|
| | 50 | | #endregion |
| | 51 | |
|
| | 52 | | #region IEnumerable<T> |
| | 53 | |
|
| | 54 | | public IEnumerator<T> GetEnumerator() |
| | 55 | | { |
| | 56 | | lock (this._listLock) |
| | 57 | | { |
| | 58 | | //Clone the list for enumeration -- this is best for "small" lists. If dealing with larger lists, we sh |
| | 59 | | IReadOnlyList<T> copy = new List<T>(this._list); |
| | 60 | | return copy.GetEnumerator(); |
| | 61 | | } |
| | 62 | | } |
| | 63 | |
|
| | 64 | | #endregion |
| | 65 | |
|
| | 66 | | #region IEnumerable |
| | 67 | |
|
| | 68 | | IEnumerator IEnumerable.GetEnumerator() |
| | 69 | | { |
| | 70 | | return this.GetEnumerator(); |
| | 71 | | } |
| | 72 | |
|
| | 73 | | #endregion |
| | 74 | |
|
| | 75 | | #region IList |
| | 76 | |
|
| | 77 | | public void Add(T item) |
| | 78 | | { |
| | 79 | | this.ThrowOnReadOnly(); |
| | 80 | |
|
| | 81 | | lock (this._listLock) |
| | 82 | | { |
| | 83 | | this._list.Add(item); |
| | 84 | | this._hasBeenModified = true; |
| | 85 | | } |
| | 86 | | } |
| | 87 | |
|
| | 88 | | public void Clear() |
| | 89 | | { |
| | 90 | | this.ThrowOnReadOnly(); |
| | 91 | |
|
| | 92 | | lock (this._listLock) |
| | 93 | | { |
| | 94 | | this._list.Clear(); |
| | 95 | | this._hasBeenModified = true; |
| | 96 | | } |
| | 97 | | } |
| | 98 | |
|
| | 99 | | public bool Contains(T item) |
| | 100 | | { |
| | 101 | | lock (this._listLock) |
| | 102 | | { |
| | 103 | | bool contains = this._list.Contains(item); |
| | 104 | | return contains; |
| | 105 | | } |
| | 106 | | } |
| | 107 | |
|
| | 108 | | public void CopyTo(T[] array, int arrayIndex) |
| | 109 | | { |
| | 110 | | lock (this._listLock) |
| | 111 | | { |
| | 112 | | this._list.CopyTo(array, arrayIndex); |
| | 113 | | } |
| | 114 | | } |
| | 115 | |
|
| | 116 | | public bool Remove(T item) |
| | 117 | | { |
| | 118 | | this.ThrowOnReadOnly(); |
| | 119 | |
|
| | 120 | | lock (this._listLock) |
| | 121 | | { |
| | 122 | | bool result = this._list.Remove(item); |
| | 123 | | this._hasBeenModified = true; |
| | 124 | | return result; |
| | 125 | | } |
| | 126 | | } |
| | 127 | |
|
| | 128 | | public int Count |
| | 129 | | { |
| | 130 | | get |
| | 131 | | { |
| | 132 | | lock (this._listLock) |
| | 133 | | { |
| | 134 | | int count = this._list.Count; |
| | 135 | | return count; |
| | 136 | | } |
| | 137 | | } |
| | 138 | | } |
| | 139 | |
|
| | 140 | | public int IndexOf(T item) |
| | 141 | | { |
| | 142 | | lock (this._listLock) |
| | 143 | | { |
| | 144 | | int index = this._list.IndexOf(item); |
| | 145 | | return index; |
| | 146 | | } |
| | 147 | | } |
| | 148 | |
|
| | 149 | | public void Insert(int index, T item) |
| | 150 | | { |
| | 151 | | this.ThrowOnReadOnly(); |
| | 152 | |
|
| | 153 | | lock (this._listLock) |
| | 154 | | { |
| | 155 | | this._list.Insert(index, item); |
| | 156 | | this._hasBeenModified = true; |
| | 157 | | } |
| | 158 | | } |
| | 159 | |
|
| | 160 | | public void RemoveAt(int index) |
| | 161 | | { |
| | 162 | | this.ThrowOnReadOnly(); |
| | 163 | |
|
| | 164 | | lock (this._listLock) |
| | 165 | | { |
| | 166 | | this._list.RemoveAt(index); |
| | 167 | | this._hasBeenModified = true; |
| | 168 | | } |
| | 169 | | } |
| | 170 | |
|
| | 171 | | public T this[int index] |
| | 172 | | { |
| | 173 | | get |
| | 174 | | { |
| | 175 | | lock (this._listLock) |
| | 176 | | { |
| | 177 | | T result = this._list[index]; |
| | 178 | | return result; |
| | 179 | | } |
| | 180 | | } |
| | 181 | |
|
| | 182 | | set |
| | 183 | | { |
| | 184 | | this.ThrowOnReadOnly(); |
| | 185 | |
|
| | 186 | | lock (this._listLock) |
| | 187 | | { |
| | 188 | | this._list[index] = value; |
| | 189 | | this._hasBeenModified = true; |
| | 190 | | } |
| | 191 | | } |
| | 192 | | } |
| | 193 | |
|
| | 194 | | #endregion |
| | 195 | |
|
| | 196 | | public void AddRange(IEnumerable<T> items) |
| | 197 | | { |
| | 198 | | this.ThrowOnReadOnly(); |
| | 199 | |
|
| | 200 | | lock (this._listLock) |
| | 201 | | { |
| | 202 | | foreach (T item in items) |
| | 203 | | { |
| | 204 | | this._list.Add(item); |
| | 205 | | } |
| | 206 | | } |
| | 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 |
| | 213 | | return new ReadOnlyCollection<T>(this._list); |
| | 214 | | } |
| | 215 | |
|
| | 216 | | public static ConcurrentChangeTrackedList<T> TransformEnumerableToConcurrentList(IEnumerable<T> enumerable) |
| | 217 | | { |
| | 218 | | if (enumerable == null) |
| | 219 | | { |
| | 220 | | return null; |
| | 221 | | } |
| | 222 | |
|
| | 223 | | ConcurrentChangeTrackedList<T> list = new ConcurrentChangeTrackedList<T>(enumerable); |
| | 224 | | return list; |
| | 225 | | } |
| | 226 | |
|
| | 227 | | #region Private helpers |
| | 228 | |
|
| | 229 | | private void ThrowOnReadOnly() |
| | 230 | | { |
| | 231 | | if (this.IsReadOnly) |
| | 232 | | { |
| | 233 | | throw new InvalidOperationException(BatchErrorMessages.GeneralObjectInInvalidState); |
| | 234 | | } |
| | 235 | | } |
| | 236 | |
|
| | 237 | | #endregion |
| | 238 | |
|
| | 239 | | } |
| | 240 | |
|
| | 241 | | internal sealed class ConcurrentChangeTrackedModifiableList<T> : ConcurrentChangeTrackedList<T> where T : IPropertyM |
| | 242 | | { |
| 0 | 243 | | public ConcurrentChangeTrackedModifiableList() |
| | 244 | | { |
| 0 | 245 | | } |
| | 246 | |
|
| 14976 | 247 | | public ConcurrentChangeTrackedModifiableList(IEnumerable<T> other, bool isReadOnly = false) : base(other, isRead |
| | 248 | | { |
| 14976 | 249 | | } |
| | 250 | |
|
| | 251 | | #region IModifiable |
| | 252 | |
|
| | 253 | | public override bool HasBeenModified |
| | 254 | | { |
| | 255 | | get |
| | 256 | | { |
| 14 | 257 | | lock (this._listLock) |
| | 258 | | { |
| | 259 | | //Check to see if this object has been modified |
| 14 | 260 | | if (this._hasBeenModified) |
| | 261 | | { |
| 4 | 262 | | return true; |
| | 263 | | } |
| | 264 | |
|
| | 265 | | //Check to see if the children objects have been modified |
| 39 | 266 | | foreach (T item in this._list) |
| | 267 | | { |
| 10 | 268 | | if (item != null && item.HasBeenModified) |
| | 269 | | { |
| 1 | 270 | | return true; |
| | 271 | | } |
| | 272 | | } |
| | 273 | |
|
| 9 | 274 | | return false; |
| | 275 | | } |
| 14 | 276 | | } |
| | 277 | | } |
| | 278 | |
|
| | 279 | | public override bool IsReadOnly |
| | 280 | | { |
| | 281 | | get |
| | 282 | | { |
| 106 | 283 | | return base.IsReadOnly; |
| | 284 | | } |
| | 285 | | set |
| | 286 | | { |
| 23119 | 287 | | base.IsReadOnly = value; |
| 254584 | 288 | | foreach (T readOnlyItem in this) |
| | 289 | | { |
| 104173 | 290 | | if (readOnlyItem != null) |
| | 291 | | { |
| 104169 | 292 | | readOnlyItem.IsReadOnly = value; |
| | 293 | | } |
| | 294 | | } |
| 23119 | 295 | | } |
| | 296 | | } |
| | 297 | |
|
| | 298 | | #endregion |
| | 299 | |
|
| | 300 | | public static ConcurrentChangeTrackedModifiableList<T> TransformEnumerableToConcurrentModifiableList(IEnumerable |
| | 301 | | { |
| 2842 | 302 | | if (enumerable == null) |
| | 303 | | { |
| 45 | 304 | | return null; |
| | 305 | | } |
| | 306 | |
|
| 2797 | 307 | | ConcurrentChangeTrackedModifiableList<T> list = new ConcurrentChangeTrackedModifiableList<T>(enumerable); |
| 2797 | 308 | | return list; |
| | 309 | | } |
| | 310 | | } |
| | 311 | | } |