| | 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 | |
|
| 0 | 17 | | public ChangeTrackingList() |
| | 18 | | { |
| 0 | 19 | | } |
| | 20 | |
|
| 0 | 21 | | public ChangeTrackingList(Optional<IList<T>> optionalList) : this(optionalList.Value) |
| | 22 | | { |
| 0 | 23 | | } |
| | 24 | |
|
| 0 | 25 | | public ChangeTrackingList(Optional<IReadOnlyList<T>> optionalList) : this(optionalList.Value) |
| | 26 | | { |
| 0 | 27 | | } |
| | 28 | |
|
| 0 | 29 | | private ChangeTrackingList(IEnumerable<T> innerList) |
| | 30 | | { |
| 0 | 31 | | if (innerList == null) |
| | 32 | | { |
| 0 | 33 | | return; |
| | 34 | | } |
| | 35 | |
|
| 0 | 36 | | _innerList = innerList.ToList(); |
| 0 | 37 | | } |
| | 38 | |
|
| 0 | 39 | | private ChangeTrackingList(IList<T> innerList) |
| | 40 | | { |
| 0 | 41 | | if (innerList == null) |
| | 42 | | { |
| 0 | 43 | | return; |
| | 44 | | } |
| | 45 | |
|
| 0 | 46 | | _innerList = innerList; |
| 0 | 47 | | } |
| | 48 | |
|
| 0 | 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 | | { |
| 0 | 58 | | if (IsUndefined) |
| | 59 | | { |
| | 60 | | IEnumerator<T> EnumerateEmpty() |
| | 61 | | { |
| 0 | 62 | | yield break; |
| | 63 | | } |
| | 64 | |
|
| 0 | 65 | | return EnumerateEmpty(); |
| | 66 | | } |
| 0 | 67 | | return EnsureList().GetEnumerator(); |
| | 68 | | } |
| | 69 | |
|
| | 70 | | IEnumerator IEnumerable.GetEnumerator() |
| | 71 | | { |
| 0 | 72 | | return GetEnumerator(); |
| | 73 | | } |
| | 74 | |
|
| | 75 | | public void Add(T item) |
| | 76 | | { |
| 0 | 77 | | EnsureList().Add(item); |
| 0 | 78 | | } |
| | 79 | |
|
| | 80 | | public void Clear() |
| | 81 | | { |
| 0 | 82 | | EnsureList().Clear(); |
| 0 | 83 | | } |
| | 84 | |
|
| | 85 | | public bool Contains(T item) |
| | 86 | | { |
| 0 | 87 | | if (IsUndefined) |
| | 88 | | { |
| 0 | 89 | | return false; |
| | 90 | | } |
| | 91 | |
|
| 0 | 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 | | { |
| 0 | 119 | | if (IsUndefined) |
| | 120 | | { |
| 0 | 121 | | return 0; |
| | 122 | | } |
| 0 | 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 | | { |
| 0 | 169 | | if (IsUndefined) |
| | 170 | | { |
| 0 | 171 | | throw new ArgumentOutOfRangeException(nameof(index)); |
| | 172 | | } |
| | 173 | |
|
| 0 | 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 | | { |
| 0 | 189 | | return _innerList ??= new List<T>(); |
| | 190 | | } |
| | 191 | | } |
| | 192 | | } |