| | | 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.Collections; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Diagnostics; |
| | | 7 | | |
| | | 8 | | namespace System.Text.Json.Serialization |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// A list of configuration items that can be locked for modification |
| | | 12 | | /// </summary> |
| | | 13 | | [DebuggerDisplay("{DebuggerDisplay,nq}")] |
| | | 14 | | [DebuggerTypeProxy(typeof(ConfigurationList<>.ConfigurationListDebugView))] |
| | | 15 | | internal abstract class ConfigurationList<TItem> : IList<TItem> |
| | | 16 | | { |
| | | 17 | | protected readonly List<TItem> _list; |
| | | 18 | | |
| | 1299 | 19 | | public ConfigurationList(IEnumerable<TItem>? source = null) |
| | 1299 | 20 | | { |
| | 1299 | 21 | | _list = source is null ? new List<TItem>() : new List<TItem>(source); |
| | 1299 | 22 | | } |
| | | 23 | | |
| | | 24 | | public abstract bool IsReadOnly { get; } |
| | | 25 | | protected abstract void OnCollectionModifying(); |
| | 15588 | 26 | | protected virtual void OnCollectionModified() { } |
| | 0 | 27 | | protected virtual void ValidateAddedValue(TItem item) { } |
| | | 28 | | |
| | | 29 | | public TItem this[int index] |
| | | 30 | | { |
| | | 31 | | get |
| | 7794 | 32 | | { |
| | 7794 | 33 | | return _list[index]; |
| | 7794 | 34 | | } |
| | | 35 | | set |
| | 0 | 36 | | { |
| | 0 | 37 | | ArgumentNullException.ThrowIfNull(value); |
| | | 38 | | |
| | 0 | 39 | | OnCollectionModifying(); |
| | 0 | 40 | | ValidateAddedValue(value); |
| | 0 | 41 | | _list[index] = value; |
| | 0 | 42 | | OnCollectionModified(); |
| | 0 | 43 | | } |
| | | 44 | | } |
| | | 45 | | |
| | 19485 | 46 | | public int Count => _list.Count; |
| | | 47 | | |
| | | 48 | | public void Add(TItem item) |
| | 7794 | 49 | | { |
| | 7794 | 50 | | ArgumentNullException.ThrowIfNull(item); |
| | | 51 | | |
| | 7794 | 52 | | OnCollectionModifying(); |
| | 7794 | 53 | | ValidateAddedValue(item); |
| | 7794 | 54 | | _list.Add(item); |
| | 7794 | 55 | | OnCollectionModified(); |
| | 7794 | 56 | | } |
| | | 57 | | |
| | | 58 | | public void Clear() |
| | 0 | 59 | | { |
| | 0 | 60 | | OnCollectionModifying(); |
| | 0 | 61 | | _list.Clear(); |
| | 0 | 62 | | OnCollectionModified(); |
| | 0 | 63 | | } |
| | | 64 | | |
| | | 65 | | public bool Contains(TItem item) |
| | 0 | 66 | | { |
| | 0 | 67 | | return _list.Contains(item); |
| | 0 | 68 | | } |
| | | 69 | | |
| | | 70 | | public void CopyTo(TItem[] array, int arrayIndex) |
| | 0 | 71 | | { |
| | 0 | 72 | | _list.CopyTo(array, arrayIndex); |
| | 0 | 73 | | } |
| | | 74 | | |
| | | 75 | | public List<TItem>.Enumerator GetEnumerator() |
| | 1299 | 76 | | { |
| | 1299 | 77 | | return _list.GetEnumerator(); |
| | 1299 | 78 | | } |
| | | 79 | | |
| | | 80 | | public int IndexOf(TItem item) |
| | 0 | 81 | | { |
| | 0 | 82 | | return _list.IndexOf(item); |
| | 0 | 83 | | } |
| | | 84 | | |
| | | 85 | | public void Insert(int index, TItem item) |
| | 0 | 86 | | { |
| | 0 | 87 | | ArgumentNullException.ThrowIfNull(item); |
| | | 88 | | |
| | 0 | 89 | | OnCollectionModifying(); |
| | 0 | 90 | | ValidateAddedValue(item); |
| | 0 | 91 | | _list.Insert(index, item); |
| | 0 | 92 | | OnCollectionModified(); |
| | 0 | 93 | | } |
| | | 94 | | |
| | | 95 | | public bool Remove(TItem item) |
| | 0 | 96 | | { |
| | 0 | 97 | | OnCollectionModifying(); |
| | 0 | 98 | | bool removed = _list.Remove(item); |
| | 0 | 99 | | if (removed) |
| | 0 | 100 | | { |
| | 0 | 101 | | OnCollectionModified(); |
| | 0 | 102 | | } |
| | | 103 | | |
| | 0 | 104 | | return removed; |
| | 0 | 105 | | } |
| | | 106 | | |
| | | 107 | | public void RemoveAt(int index) |
| | 0 | 108 | | { |
| | 0 | 109 | | OnCollectionModifying(); |
| | 0 | 110 | | _list.RemoveAt(index); |
| | 0 | 111 | | OnCollectionModified(); |
| | 0 | 112 | | } |
| | | 113 | | |
| | | 114 | | IEnumerator<TItem> IEnumerable<TItem>.GetEnumerator() |
| | 0 | 115 | | { |
| | 0 | 116 | | return _list.GetEnumerator(); |
| | 0 | 117 | | } |
| | | 118 | | |
| | | 119 | | IEnumerator IEnumerable.GetEnumerator() |
| | 0 | 120 | | { |
| | 0 | 121 | | return _list.GetEnumerator(); |
| | 0 | 122 | | } |
| | | 123 | | |
| | | 124 | | [DebuggerBrowsable(DebuggerBrowsableState.Never)] |
| | 0 | 125 | | private string DebuggerDisplay => $"Count = {Count}, IsReadOnly = {IsReadOnly}"; |
| | | 126 | | |
| | 0 | 127 | | private sealed class ConfigurationListDebugView(ConfigurationList<TItem> collection) |
| | | 128 | | { |
| | | 129 | | [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)] |
| | 0 | 130 | | public TItem[] Items => collection._list.ToArray(); |
| | | 131 | | } |
| | | 132 | | } |
| | | 133 | | } |