| | | 1 | | using System.Collections.Generic; |
| | | 2 | | using System.Collections.ObjectModel; |
| | | 3 | | |
| | | 4 | | namespace WireMock.Util |
| | | 5 | | { |
| | | 6 | | /// <summary> |
| | | 7 | | /// A special Collection that overrides methods of <see cref="ObservableCollection{T}"/> to make them thread safe |
| | | 8 | | /// </summary> |
| | | 9 | | /// <typeparam name="T">The type of elements in the collection.</typeparam> |
| | | 10 | | /// <inheritdoc cref="ObservableCollection{T}" /> |
| | | 11 | | public class ConcurentObservableCollection<T> : ObservableCollection<T> |
| | | 12 | | { |
| | 54 | 13 | | private readonly object _lockObject = new object(); |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Initializes a new instance of the <see cref="T:WireMock.Util.ConcurentObservableCollection`1" /> class. |
| | | 17 | | /// </summary> |
| | 162 | 18 | | public ConcurentObservableCollection() { } |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Initializes a new instance of the <see cref="ConcurentObservableCollection{T}"/> class that contains element |
| | | 22 | | /// </summary> |
| | | 23 | | /// <param name="list">The list from which the elements are copied.</param> |
| | 0 | 24 | | public ConcurentObservableCollection(List<T> list) : base(list) { } |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Initializes a new instance of the <see cref="ConcurentObservableCollection{T}"/> class that contains element |
| | | 28 | | /// </summary> |
| | | 29 | | /// <param name="collection">The collection from which the elements are copied.</param> |
| | 0 | 30 | | public ConcurentObservableCollection(IEnumerable<T> collection) : base(collection) { } |
| | | 31 | | |
| | | 32 | | /// <inheritdoc cref="ObservableCollection{T}.ClearItems"/> |
| | | 33 | | protected override void ClearItems() |
| | 1 | 34 | | { |
| | 1 | 35 | | lock (_lockObject) |
| | 1 | 36 | | { |
| | 1 | 37 | | base.ClearItems(); |
| | 1 | 38 | | } |
| | 1 | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <inheritdoc cref="ObservableCollection{T}.RemoveItem"/> |
| | | 42 | | protected override void RemoveItem(int index) |
| | 2 | 43 | | { |
| | 2 | 44 | | lock (_lockObject) |
| | 2 | 45 | | { |
| | 2 | 46 | | base.RemoveItem(index); |
| | 2 | 47 | | } |
| | 2 | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <inheritdoc cref="ObservableCollection{T}.InsertItem"/> |
| | | 51 | | protected override void InsertItem(int index, T item) |
| | 40 | 52 | | { |
| | 40 | 53 | | lock (_lockObject) |
| | 40 | 54 | | { |
| | 40 | 55 | | base.InsertItem(index, item); |
| | 40 | 56 | | } |
| | 40 | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <inheritdoc cref="ObservableCollection{T}.SetItem"/> |
| | | 60 | | protected override void SetItem(int index, T item) |
| | 0 | 61 | | { |
| | 0 | 62 | | lock (_lockObject) |
| | 0 | 63 | | { |
| | 0 | 64 | | base.SetItem(index, item); |
| | 0 | 65 | | } |
| | 0 | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <inheritdoc cref="ObservableCollection{T}.MoveItem"/> |
| | | 69 | | protected override void MoveItem(int oldIndex, int newIndex) |
| | 0 | 70 | | { |
| | 0 | 71 | | lock (_lockObject) |
| | 0 | 72 | | { |
| | 0 | 73 | | base.MoveItem(oldIndex, newIndex); |
| | 0 | 74 | | } |
| | 0 | 75 | | } |
| | | 76 | | } |
| | | 77 | | } |