| | | 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 | | |
| | | 8 | | namespace CoreWCF.Channels |
| | | 9 | | { |
| | | 10 | | public class BindingElementCollection : Collection<BindingElement> |
| | | 11 | | { |
| | 30255 | 12 | | public BindingElementCollection() |
| | | 13 | | { |
| | 30255 | 14 | | } |
| | | 15 | | |
| | 22045 | 16 | | public BindingElementCollection(IEnumerable<BindingElement> elements) |
| | | 17 | | { |
| | 22045 | 18 | | if (elements == null) |
| | | 19 | | { |
| | 0 | 20 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(elements)); |
| | | 21 | | } |
| | | 22 | | |
| | 120328 | 23 | | foreach (BindingElement element in elements) |
| | | 24 | | { |
| | 38119 | 25 | | Add(element); |
| | | 26 | | } |
| | 22045 | 27 | | } |
| | | 28 | | |
| | 0 | 29 | | public BindingElementCollection(BindingElement[] elements) |
| | | 30 | | { |
| | 0 | 31 | | if (elements == null) |
| | | 32 | | { |
| | 0 | 33 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(elements)); |
| | | 34 | | } |
| | | 35 | | |
| | 0 | 36 | | for (int i = 0; i < elements.Length; i++) |
| | | 37 | | { |
| | 0 | 38 | | Add(elements[i]); |
| | | 39 | | } |
| | 0 | 40 | | } |
| | | 41 | | |
| | | 42 | | // returns a new collection with clones of all the elements |
| | | 43 | | public BindingElementCollection Clone() |
| | | 44 | | { |
| | 11314 | 45 | | BindingElementCollection result = new BindingElementCollection(); |
| | 68966 | 46 | | for (int i = 0; i < Count; i++) |
| | | 47 | | { |
| | 23169 | 48 | | result.Add(this[i].Clone()); |
| | | 49 | | } |
| | 11314 | 50 | | return result; |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | public void AddRange(params BindingElement[] elements) |
| | | 54 | | { |
| | 0 | 55 | | if (elements == null) |
| | | 56 | | { |
| | 0 | 57 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(elements)); |
| | | 58 | | } |
| | | 59 | | |
| | 0 | 60 | | for (int i = 0; i < elements.Length; i++) |
| | | 61 | | { |
| | 0 | 62 | | Add(elements[i]); |
| | | 63 | | } |
| | 0 | 64 | | } |
| | | 65 | | |
| | | 66 | | public bool Contains(Type bindingElementType) |
| | | 67 | | { |
| | 0 | 68 | | if (bindingElementType == null) |
| | | 69 | | { |
| | 0 | 70 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(bindingElementType)); |
| | | 71 | | } |
| | | 72 | | |
| | 0 | 73 | | for (int i = 0; i < Count; i++) |
| | | 74 | | { |
| | 0 | 75 | | if (bindingElementType.IsInstanceOfType(this[i])) |
| | | 76 | | { |
| | 0 | 77 | | return true; |
| | | 78 | | } |
| | | 79 | | } |
| | 0 | 80 | | return false; |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | public T Find<T>() |
| | | 84 | | { |
| | 4613 | 85 | | return Find<T>(false); |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | public T Remove<T>() |
| | | 89 | | { |
| | 12785 | 90 | | return Find<T>(true); |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | private T Find<T>(bool remove) |
| | | 94 | | { |
| | 43116 | 95 | | for (int index = 0; index < Count; index++) |
| | | 96 | | { |
| | 21552 | 97 | | if (this[index] is T) |
| | | 98 | | { |
| | 17392 | 99 | | T item = (T)(object)this[index]; |
| | 17392 | 100 | | if (remove) |
| | | 101 | | { |
| | 12785 | 102 | | RemoveAt(index); |
| | | 103 | | } |
| | 17392 | 104 | | return item; |
| | | 105 | | } |
| | | 106 | | } |
| | 6 | 107 | | return default; |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | public Collection<T> FindAll<T>() |
| | | 111 | | { |
| | 73 | 112 | | return FindAll<T>(false); |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | public Collection<T> RemoveAll<T>() |
| | | 116 | | { |
| | 0 | 117 | | return FindAll<T>(true); |
| | | 118 | | } |
| | | 119 | | |
| | | 120 | | private Collection<T> FindAll<T>(bool remove) |
| | | 121 | | { |
| | 73 | 122 | | Collection<T> collection = new Collection<T>(); |
| | | 123 | | |
| | 496 | 124 | | for (int index = 0; index < Count; index++) |
| | | 125 | | { |
| | 175 | 126 | | if (this[index] is T) |
| | | 127 | | { |
| | 146 | 128 | | T item = (T)(object)this[index]; |
| | 146 | 129 | | if (remove) |
| | | 130 | | { |
| | 0 | 131 | | RemoveAt(index); |
| | | 132 | | // back up the index so we inspect the new item at this location |
| | 0 | 133 | | index--; |
| | | 134 | | } |
| | 146 | 135 | | collection.Add(item); |
| | | 136 | | } |
| | | 137 | | } |
| | | 138 | | |
| | 73 | 139 | | return collection; |
| | | 140 | | } |
| | | 141 | | protected override void InsertItem(int index, BindingElement item) |
| | | 142 | | { |
| | 96419 | 143 | | if (item == null) |
| | | 144 | | { |
| | 0 | 145 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(item)); |
| | | 146 | | } |
| | | 147 | | |
| | 96419 | 148 | | base.InsertItem(index, item); |
| | 96419 | 149 | | } |
| | | 150 | | |
| | | 151 | | protected override void SetItem(int index, BindingElement item) |
| | | 152 | | { |
| | 0 | 153 | | if (item == null) |
| | | 154 | | { |
| | 0 | 155 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(item)); |
| | | 156 | | } |
| | | 157 | | |
| | 0 | 158 | | base.SetItem(index, item); |
| | 0 | 159 | | } |
| | | 160 | | |
| | | 161 | | internal BindingElementCollection Reverse() |
| | | 162 | | { |
| | 0 | 163 | | var bec = new BindingElementCollection(); |
| | 0 | 164 | | for (int i = Count - 1; i >= 0; i--) |
| | | 165 | | { |
| | 0 | 166 | | bec.Add(this[i]); |
| | | 167 | | } |
| | | 168 | | |
| | 0 | 169 | | return bec; |
| | | 170 | | } |
| | | 171 | | } |
| | | 172 | | } |