| | | 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 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections; |
| | | 6 | | using System.Collections.Generic; |
| | | 7 | | using System.Reflection; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF.Collections.Generic |
| | | 10 | | { |
| | | 11 | | public class SynchronizedCollection<T> : IList<T>, IList |
| | | 12 | | { |
| | 631 | 13 | | public SynchronizedCollection() |
| | | 14 | | { |
| | 631 | 15 | | Items = new List<T>(); |
| | 631 | 16 | | SyncRoot = new object(); |
| | 631 | 17 | | } |
| | | 18 | | |
| | 22182 | 19 | | public SynchronizedCollection(object syncRoot) |
| | | 20 | | { |
| | 22182 | 21 | | Items = new List<T>(); |
| | 22182 | 22 | | SyncRoot = syncRoot ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(syncRoot)); |
| | 22182 | 23 | | } |
| | | 24 | | |
| | 0 | 25 | | public SynchronizedCollection(object syncRoot, IEnumerable<T> list) |
| | | 26 | | { |
| | 0 | 27 | | if (list == null) |
| | | 28 | | { |
| | 0 | 29 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(list)); |
| | | 30 | | } |
| | | 31 | | |
| | 0 | 32 | | Items = new List<T>(list); |
| | 0 | 33 | | SyncRoot = syncRoot ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(syncRoot)); |
| | 0 | 34 | | } |
| | | 35 | | |
| | 641 | 36 | | public SynchronizedCollection(object syncRoot, params T[] list) |
| | | 37 | | { |
| | 641 | 38 | | if (list == null) |
| | | 39 | | { |
| | 0 | 40 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(list)); |
| | | 41 | | } |
| | | 42 | | |
| | 641 | 43 | | Items = new List<T>(list.Length); |
| | 1282 | 44 | | foreach (T t in list) |
| | | 45 | | { |
| | 0 | 46 | | Items.Add(t); |
| | | 47 | | } |
| | | 48 | | |
| | 641 | 49 | | SyncRoot = syncRoot ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(syncRoot)); |
| | 641 | 50 | | } |
| | | 51 | | |
| | | 52 | | public int Count |
| | | 53 | | { |
| | 157719 | 54 | | get { lock (SyncRoot) { return Items.Count; } } |
| | | 55 | | } |
| | | 56 | | |
| | 196567 | 57 | | protected List<T> Items { get; } |
| | | 58 | | |
| | 120641 | 59 | | public object SyncRoot { get; } |
| | | 60 | | |
| | | 61 | | public T this[int index] |
| | | 62 | | { |
| | | 63 | | get |
| | | 64 | | { |
| | 17836 | 65 | | lock (SyncRoot) |
| | | 66 | | { |
| | 17836 | 67 | | return Items[index]; |
| | | 68 | | } |
| | 17836 | 69 | | } |
| | | 70 | | set |
| | | 71 | | { |
| | 0 | 72 | | lock (SyncRoot) |
| | | 73 | | { |
| | 0 | 74 | | if (index < 0 || index >= Items.Count) |
| | | 75 | | { |
| | 0 | 76 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof |
| | 0 | 77 | | SR.Format(SR.ValueMustBeInRange, 0, Items.Count - 1))); |
| | | 78 | | } |
| | | 79 | | |
| | 0 | 80 | | SetItem(index, value); |
| | 0 | 81 | | } |
| | 0 | 82 | | } |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | public void Add(T item) |
| | | 86 | | { |
| | 10794 | 87 | | lock (SyncRoot) |
| | | 88 | | { |
| | 10794 | 89 | | int index = Items.Count; |
| | 10794 | 90 | | InsertItem(index, item); |
| | 10794 | 91 | | } |
| | 10794 | 92 | | } |
| | | 93 | | |
| | | 94 | | public void Clear() |
| | | 95 | | { |
| | 33 | 96 | | lock (SyncRoot) |
| | | 97 | | { |
| | 33 | 98 | | ClearItems(); |
| | 33 | 99 | | } |
| | 33 | 100 | | } |
| | | 101 | | |
| | | 102 | | public void CopyTo(T[] array, int index) |
| | | 103 | | { |
| | 1186 | 104 | | lock (SyncRoot) |
| | | 105 | | { |
| | 1186 | 106 | | Items.CopyTo(array, index); |
| | 1186 | 107 | | } |
| | 1186 | 108 | | } |
| | | 109 | | |
| | | 110 | | public bool Contains(T item) |
| | | 111 | | { |
| | 0 | 112 | | lock (SyncRoot) |
| | | 113 | | { |
| | 0 | 114 | | return Items.Contains(item); |
| | | 115 | | } |
| | 0 | 116 | | } |
| | | 117 | | |
| | | 118 | | public IEnumerator<T> GetEnumerator() |
| | | 119 | | { |
| | 7296 | 120 | | lock (SyncRoot) |
| | | 121 | | { |
| | 7296 | 122 | | return Items.GetEnumerator(); |
| | | 123 | | } |
| | 7296 | 124 | | } |
| | | 125 | | |
| | | 126 | | public int IndexOf(T item) |
| | | 127 | | { |
| | 0 | 128 | | lock (SyncRoot) |
| | | 129 | | { |
| | 0 | 130 | | return InternalIndexOf(item); |
| | | 131 | | } |
| | 0 | 132 | | } |
| | | 133 | | |
| | | 134 | | public void Insert(int index, T item) |
| | | 135 | | { |
| | 0 | 136 | | lock (SyncRoot) |
| | | 137 | | { |
| | 0 | 138 | | if (index < 0 || index > Items.Count) |
| | | 139 | | { |
| | 0 | 140 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(ind |
| | 0 | 141 | | SR.Format(SR.ValueMustBeInRange, 0, Items.Count - 1))); |
| | | 142 | | } |
| | | 143 | | |
| | 0 | 144 | | InsertItem(index, item); |
| | 0 | 145 | | } |
| | 0 | 146 | | } |
| | | 147 | | |
| | | 148 | | private int InternalIndexOf(T item) |
| | | 149 | | { |
| | 1906 | 150 | | int count = Items.Count; |
| | | 151 | | |
| | 3812 | 152 | | for (int i = 0; i < count; i++) |
| | | 153 | | { |
| | 1906 | 154 | | if (Equals(Items[i], item)) |
| | | 155 | | { |
| | 1906 | 156 | | return i; |
| | | 157 | | } |
| | | 158 | | } |
| | 0 | 159 | | return -1; |
| | | 160 | | } |
| | | 161 | | |
| | | 162 | | public bool Remove(T item) |
| | | 163 | | { |
| | 1906 | 164 | | lock (SyncRoot) |
| | | 165 | | { |
| | 1906 | 166 | | int index = InternalIndexOf(item); |
| | 1906 | 167 | | if (index < 0) |
| | | 168 | | { |
| | 0 | 169 | | return false; |
| | | 170 | | } |
| | | 171 | | |
| | 1906 | 172 | | RemoveItem(index); |
| | 1906 | 173 | | return true; |
| | | 174 | | } |
| | 1906 | 175 | | } |
| | | 176 | | |
| | | 177 | | public void RemoveAt(int index) |
| | | 178 | | { |
| | 0 | 179 | | lock (SyncRoot) |
| | | 180 | | { |
| | 0 | 181 | | if (index < 0 || index >= Items.Count) |
| | | 182 | | { |
| | 0 | 183 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(ind |
| | 0 | 184 | | SR.Format(SR.ValueMustBeInRange, 0, Items.Count - 1))); |
| | | 185 | | } |
| | | 186 | | |
| | 0 | 187 | | RemoveItem(index); |
| | 0 | 188 | | } |
| | 0 | 189 | | } |
| | | 190 | | |
| | | 191 | | protected virtual void ClearItems() |
| | | 192 | | { |
| | 33 | 193 | | Items.Clear(); |
| | 33 | 194 | | } |
| | | 195 | | |
| | | 196 | | protected virtual void InsertItem(int index, T item) |
| | | 197 | | { |
| | 10794 | 198 | | Items.Insert(index, item); |
| | 10794 | 199 | | } |
| | | 200 | | |
| | | 201 | | protected virtual void RemoveItem(int index) |
| | | 202 | | { |
| | 1906 | 203 | | Items.RemoveAt(index); |
| | 1906 | 204 | | } |
| | | 205 | | |
| | | 206 | | protected virtual void SetItem(int index, T item) |
| | | 207 | | { |
| | 0 | 208 | | Items[index] = item; |
| | 0 | 209 | | } |
| | | 210 | | |
| | | 211 | | bool ICollection<T>.IsReadOnly |
| | | 212 | | { |
| | 0 | 213 | | get { return false; } |
| | | 214 | | } |
| | | 215 | | |
| | | 216 | | IEnumerator IEnumerable.GetEnumerator() |
| | | 217 | | { |
| | 6 | 218 | | return ((IList)Items).GetEnumerator(); |
| | | 219 | | } |
| | | 220 | | |
| | | 221 | | bool ICollection.IsSynchronized |
| | | 222 | | { |
| | 0 | 223 | | get { return true; } |
| | | 224 | | } |
| | | 225 | | |
| | | 226 | | object ICollection.SyncRoot |
| | | 227 | | { |
| | 0 | 228 | | get { return SyncRoot; } |
| | | 229 | | } |
| | | 230 | | |
| | | 231 | | void ICollection.CopyTo(Array array, int index) |
| | | 232 | | { |
| | 0 | 233 | | lock (SyncRoot) |
| | | 234 | | { |
| | 0 | 235 | | ((IList)Items).CopyTo(array, index); |
| | 0 | 236 | | } |
| | 0 | 237 | | } |
| | | 238 | | |
| | | 239 | | object IList.this[int index] |
| | | 240 | | { |
| | | 241 | | get |
| | | 242 | | { |
| | 0 | 243 | | return this[index]; |
| | | 244 | | } |
| | | 245 | | set |
| | | 246 | | { |
| | 0 | 247 | | VerifyValueType(value); |
| | 0 | 248 | | this[index] = (T)value; |
| | 0 | 249 | | } |
| | | 250 | | } |
| | | 251 | | |
| | | 252 | | bool IList.IsReadOnly |
| | | 253 | | { |
| | 0 | 254 | | get { return false; } |
| | | 255 | | } |
| | | 256 | | |
| | | 257 | | bool IList.IsFixedSize |
| | | 258 | | { |
| | 0 | 259 | | get { return false; } |
| | | 260 | | } |
| | | 261 | | |
| | | 262 | | int IList.Add(object value) |
| | | 263 | | { |
| | 0 | 264 | | VerifyValueType(value); |
| | | 265 | | |
| | 0 | 266 | | lock (SyncRoot) |
| | | 267 | | { |
| | 0 | 268 | | Add((T)value); |
| | 0 | 269 | | return Count - 1; |
| | | 270 | | } |
| | 0 | 271 | | } |
| | | 272 | | |
| | | 273 | | bool IList.Contains(object value) |
| | | 274 | | { |
| | 0 | 275 | | VerifyValueType(value); |
| | 0 | 276 | | return Contains((T)value); |
| | | 277 | | } |
| | | 278 | | |
| | | 279 | | int IList.IndexOf(object value) |
| | | 280 | | { |
| | 0 | 281 | | VerifyValueType(value); |
| | 0 | 282 | | return IndexOf((T)value); |
| | | 283 | | } |
| | | 284 | | |
| | | 285 | | void IList.Insert(int index, object value) |
| | | 286 | | { |
| | 0 | 287 | | VerifyValueType(value); |
| | 0 | 288 | | Insert(index, (T)value); |
| | 0 | 289 | | } |
| | | 290 | | |
| | | 291 | | void IList.Remove(object value) |
| | | 292 | | { |
| | 0 | 293 | | VerifyValueType(value); |
| | 0 | 294 | | Remove((T)value); |
| | 0 | 295 | | } |
| | | 296 | | |
| | | 297 | | private static void VerifyValueType(object value) |
| | | 298 | | { |
| | 0 | 299 | | if (value == null) |
| | | 300 | | { |
| | 0 | 301 | | if (typeof(T).GetTypeInfo().IsValueType) |
| | | 302 | | { |
| | 0 | 303 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.SynchronizedCollectionWrongTypeNull) |
| | | 304 | | } |
| | | 305 | | } |
| | 0 | 306 | | else if (!(value is T)) |
| | | 307 | | { |
| | 0 | 308 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.Format(SR.SynchronizedCollectionWrongTyp |
| | | 309 | | } |
| | 0 | 310 | | } |
| | | 311 | | } |
| | | 312 | | } |