| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using System.Linq; |
| | 8 | |
|
| | 9 | | #nullable enable |
| | 10 | |
|
| | 11 | | namespace Azure.Core |
| | 12 | | { |
| | 13 | | internal class ChangeTrackingList<T>: IList<T>, IReadOnlyList<T> |
| | 14 | | { |
| | 15 | | private IList<T>? _innerList; |
| | 16 | |
|
| 40977 | 17 | | public ChangeTrackingList() |
| | 18 | | { |
| 40977 | 19 | | } |
| | 20 | |
|
| 397 | 21 | | public ChangeTrackingList(Optional<IList<T>> optionalList) : this(optionalList.Value) |
| | 22 | | { |
| 397 | 23 | | } |
| | 24 | |
|
| 19 | 25 | | public ChangeTrackingList(Optional<IReadOnlyList<T>> optionalList) : this(optionalList.Value) |
| | 26 | | { |
| 19 | 27 | | } |
| | 28 | |
|
| 19 | 29 | | private ChangeTrackingList(IEnumerable<T> innerList) |
| | 30 | | { |
| 19 | 31 | | if (innerList == null) |
| | 32 | | { |
| 19 | 33 | | return; |
| | 34 | | } |
| | 35 | |
|
| 0 | 36 | | _innerList = innerList.ToList(); |
| 0 | 37 | | } |
| | 38 | |
|
| 397 | 39 | | private ChangeTrackingList(IList<T> innerList) |
| | 40 | | { |
| 397 | 41 | | if (innerList == null) |
| | 42 | | { |
| 397 | 43 | | return; |
| | 44 | | } |
| | 45 | |
|
| 0 | 46 | | _innerList = innerList; |
| 0 | 47 | | } |
| | 48 | |
|
| 21338 | 49 | | public bool IsUndefined => _innerList == null; |
| | 50 | |
|
| | 51 | | public void Reset() |
| | 52 | | { |
| 0 | 53 | | _innerList = null; |
| 0 | 54 | | } |
| | 55 | |
|
| | 56 | | public IEnumerator<T> GetEnumerator() |
| | 57 | | { |
| 20155 | 58 | | if (IsUndefined) |
| | 59 | | { |
| | 60 | | IEnumerator<T> EnumerateEmpty() |
| | 61 | | { |
| 16664 | 62 | | yield break; |
| | 63 | | } |
| | 64 | |
|
| 16728 | 65 | | return EnumerateEmpty(); |
| | 66 | | } |
| 3427 | 67 | | return EnsureList().GetEnumerator(); |
| | 68 | | } |
| | 69 | |
|
| | 70 | | IEnumerator IEnumerable.GetEnumerator() |
| | 71 | | { |
| 514 | 72 | | return GetEnumerator(); |
| | 73 | | } |
| | 74 | |
|
| | 75 | | public void Add(T item) |
| | 76 | | { |
| 11417 | 77 | | EnsureList().Add(item); |
| 11417 | 78 | | } |
| | 79 | |
|
| | 80 | | public void Clear() |
| | 81 | | { |
| 386 | 82 | | EnsureList().Clear(); |
| 386 | 83 | | } |
| | 84 | |
|
| | 85 | | public bool Contains(T item) |
| | 86 | | { |
| 298 | 87 | | if (IsUndefined) |
| | 88 | | { |
| 296 | 89 | | return false; |
| | 90 | | } |
| | 91 | |
|
| 2 | 92 | | return EnsureList().Contains(item); |
| | 93 | | } |
| | 94 | |
|
| | 95 | | public void CopyTo(T[] array, int arrayIndex) |
| | 96 | | { |
| 0 | 97 | | if (IsUndefined) |
| | 98 | | { |
| 0 | 99 | | return; |
| | 100 | | } |
| | 101 | |
|
| 0 | 102 | | EnsureList().CopyTo(array, arrayIndex); |
| 0 | 103 | | } |
| | 104 | |
|
| | 105 | | public bool Remove(T item) |
| | 106 | | { |
| 0 | 107 | | if (IsUndefined) |
| | 108 | | { |
| 0 | 109 | | return false; |
| | 110 | | } |
| | 111 | |
|
| 0 | 112 | | return EnsureList().Remove(item); |
| | 113 | | } |
| | 114 | |
|
| | 115 | | public int Count |
| | 116 | | { |
| | 117 | | get |
| | 118 | | { |
| 226 | 119 | | if (IsUndefined) |
| | 120 | | { |
| 210 | 121 | | return 0; |
| | 122 | | } |
| 16 | 123 | | return EnsureList().Count; |
| | 124 | | } |
| | 125 | | } |
| | 126 | |
|
| | 127 | | public bool IsReadOnly |
| | 128 | | { |
| | 129 | | get |
| | 130 | | { |
| 0 | 131 | | if (IsUndefined) |
| | 132 | | { |
| 0 | 133 | | return false; |
| | 134 | | } |
| | 135 | |
|
| 0 | 136 | | return EnsureList().IsReadOnly; |
| | 137 | | } |
| | 138 | | } |
| | 139 | |
|
| | 140 | | public int IndexOf(T item) |
| | 141 | | { |
| 0 | 142 | | if (IsUndefined) |
| | 143 | | { |
| 0 | 144 | | return -1; |
| | 145 | | } |
| | 146 | |
|
| 0 | 147 | | return EnsureList().IndexOf(item); |
| | 148 | | } |
| | 149 | |
|
| | 150 | | public void Insert(int index, T item) |
| | 151 | | { |
| 0 | 152 | | EnsureList().Insert(index, item); |
| 0 | 153 | | } |
| | 154 | |
|
| | 155 | | public void RemoveAt(int index) |
| | 156 | | { |
| 0 | 157 | | if (IsUndefined) |
| | 158 | | { |
| 0 | 159 | | throw new ArgumentOutOfRangeException(nameof(index)); |
| | 160 | | } |
| | 161 | |
|
| 0 | 162 | | EnsureList().RemoveAt(index); |
| 0 | 163 | | } |
| | 164 | |
|
| | 165 | | public T this[int index] |
| | 166 | | { |
| | 167 | | get |
| | 168 | | { |
| 4 | 169 | | if (IsUndefined) |
| | 170 | | { |
| 0 | 171 | | throw new ArgumentOutOfRangeException(nameof(index)); |
| | 172 | | } |
| | 173 | |
|
| 4 | 174 | | return EnsureList()[index]; |
| | 175 | | } |
| | 176 | | set |
| | 177 | | { |
| 0 | 178 | | if (IsUndefined) |
| | 179 | | { |
| 0 | 180 | | throw new ArgumentOutOfRangeException(nameof(index)); |
| | 181 | | } |
| | 182 | |
|
| 0 | 183 | | EnsureList()[index] = value; |
| 0 | 184 | | } |
| | 185 | | } |
| | 186 | |
|
| | 187 | | private IList<T> EnsureList() |
| | 188 | | { |
| 15252 | 189 | | return _innerList ??= new List<T>(); |
| | 190 | | } |
| | 191 | | } |
| | 192 | | } |