| | | 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.Generic; |
| | | 6 | | using System.Collections.ObjectModel; |
| | | 7 | | using CoreWCF.Collections.Generic; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF |
| | | 10 | | { |
| | | 11 | | public sealed class ExtensionCollection<T> : SynchronizedCollection<IExtension<T>>, IExtensionCollection<T> |
| | | 12 | | where T : IExtensibleObject<T> |
| | | 13 | | { |
| | | 14 | | private readonly T _owner; |
| | | 15 | | |
| | 34 | 16 | | public ExtensionCollection(T owner) |
| | | 17 | | { |
| | 34 | 18 | | if (owner == null) |
| | | 19 | | { |
| | 0 | 20 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(owner)); |
| | | 21 | | } |
| | | 22 | | |
| | 34 | 23 | | _owner = owner; |
| | 34 | 24 | | } |
| | | 25 | | |
| | | 26 | | public ExtensionCollection(T owner, object syncRoot) |
| | 2576 | 27 | | : base(syncRoot) |
| | | 28 | | { |
| | 2576 | 29 | | if (owner == null) |
| | | 30 | | { |
| | 0 | 31 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(owner)); |
| | | 32 | | } |
| | | 33 | | |
| | 2576 | 34 | | _owner = owner; |
| | 2576 | 35 | | } |
| | | 36 | | |
| | | 37 | | bool ICollection<IExtension<T>>.IsReadOnly |
| | | 38 | | { |
| | 0 | 39 | | get { return false; } |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | protected override void ClearItems() |
| | | 43 | | { |
| | | 44 | | IExtension<T>[] array; |
| | | 45 | | |
| | 0 | 46 | | lock (SyncRoot) |
| | | 47 | | { |
| | 0 | 48 | | array = new IExtension<T>[Count]; |
| | 0 | 49 | | CopyTo(array, 0); |
| | 0 | 50 | | base.ClearItems(); |
| | | 51 | | |
| | 0 | 52 | | foreach (IExtension<T> extension in array) |
| | | 53 | | { |
| | 0 | 54 | | extension.Detach(_owner); |
| | | 55 | | } |
| | 0 | 56 | | } |
| | 0 | 57 | | } |
| | | 58 | | |
| | | 59 | | public TE Find<TE>() |
| | | 60 | | { |
| | 4409 | 61 | | List<IExtension<T>> items = Items; |
| | | 62 | | |
| | 4409 | 63 | | lock (SyncRoot) |
| | | 64 | | { |
| | 8818 | 65 | | for (int i = Count - 1; i >= 0; i--) |
| | | 66 | | { |
| | 2005 | 67 | | IExtension<T> item = items[i]; |
| | 2005 | 68 | | if (item is TE) |
| | | 69 | | { |
| | 2005 | 70 | | return (TE)item; |
| | | 71 | | } |
| | | 72 | | } |
| | 2404 | 73 | | } |
| | | 74 | | |
| | 2404 | 75 | | return default; |
| | 2005 | 76 | | } |
| | | 77 | | |
| | | 78 | | public Collection<TE> FindAll<TE>() |
| | | 79 | | { |
| | 0 | 80 | | Collection<TE> result = new Collection<TE>(); |
| | 0 | 81 | | List<IExtension<T>> items = Items; |
| | | 82 | | |
| | 0 | 83 | | lock (SyncRoot) |
| | | 84 | | { |
| | 0 | 85 | | for (int i = 0; i < items.Count; i++) |
| | | 86 | | { |
| | 0 | 87 | | IExtension<T> item = items[i]; |
| | 0 | 88 | | if (item is TE) |
| | | 89 | | { |
| | 0 | 90 | | result.Add((TE)item); |
| | | 91 | | } |
| | | 92 | | } |
| | 0 | 93 | | } |
| | | 94 | | |
| | 0 | 95 | | return result; |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | protected override void InsertItem(int index, IExtension<T> item) |
| | | 99 | | { |
| | 2595 | 100 | | if (item == null) |
| | | 101 | | { |
| | 0 | 102 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(item)); |
| | | 103 | | } |
| | | 104 | | |
| | 2595 | 105 | | lock (SyncRoot) |
| | | 106 | | { |
| | 2595 | 107 | | item.Attach(_owner); |
| | 2595 | 108 | | base.InsertItem(index, item); |
| | 2595 | 109 | | } |
| | 2595 | 110 | | } |
| | | 111 | | |
| | | 112 | | protected override void RemoveItem(int index) |
| | | 113 | | { |
| | 1906 | 114 | | lock (SyncRoot) |
| | | 115 | | { |
| | 1906 | 116 | | Items[index].Detach(_owner); |
| | 1906 | 117 | | base.RemoveItem(index); |
| | 1906 | 118 | | } |
| | 1906 | 119 | | } |
| | | 120 | | |
| | | 121 | | protected override void SetItem(int index, IExtension<T> item) |
| | | 122 | | { |
| | 0 | 123 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.SFxCannotSetExten |
| | | 124 | | } |
| | | 125 | | } |
| | | 126 | | } |