| | | 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 | | |
| | | 7 | | namespace CoreWCF.IdentityModel |
| | | 8 | | { |
| | | 9 | | internal struct MostlySingletonList<T> where T : class |
| | | 10 | | { |
| | | 11 | | private T _singleton; |
| | | 12 | | private List<T> _list; |
| | | 13 | | |
| | | 14 | | public T this[int index] |
| | | 15 | | { |
| | | 16 | | get |
| | | 17 | | { |
| | 0 | 18 | | if (_list == null) |
| | | 19 | | { |
| | 0 | 20 | | EnsureValidSingletonIndex(index); |
| | 0 | 21 | | return _singleton; |
| | | 22 | | } |
| | | 23 | | else |
| | | 24 | | { |
| | 0 | 25 | | return _list[index]; |
| | | 26 | | } |
| | | 27 | | } |
| | | 28 | | } |
| | | 29 | | |
| | 0 | 30 | | public int Count { get; private set; } |
| | | 31 | | |
| | | 32 | | public void Add(T item) |
| | | 33 | | { |
| | 0 | 34 | | if (_list == null) |
| | | 35 | | { |
| | 0 | 36 | | if (Count == 0) |
| | | 37 | | { |
| | 0 | 38 | | _singleton = item; |
| | 0 | 39 | | Count = 1; |
| | 0 | 40 | | return; |
| | | 41 | | } |
| | 0 | 42 | | _list = new List<T> |
| | 0 | 43 | | { |
| | 0 | 44 | | _singleton |
| | 0 | 45 | | }; |
| | 0 | 46 | | _singleton = null; |
| | | 47 | | } |
| | 0 | 48 | | _list.Add(item); |
| | 0 | 49 | | Count++; |
| | 0 | 50 | | } |
| | | 51 | | |
| | | 52 | | private static bool Compare(T x, T y) |
| | | 53 | | { |
| | 0 | 54 | | return x == null ? y == null : x.Equals(y); |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | public bool Contains(T item) |
| | | 58 | | { |
| | 0 | 59 | | return IndexOf(item) >= 0; |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | private void EnsureValidSingletonIndex(int index) |
| | | 63 | | { |
| | 0 | 64 | | if (Count != 1) |
| | | 65 | | { |
| | 0 | 66 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("count", SR.Fo |
| | | 67 | | } |
| | | 68 | | |
| | 0 | 69 | | if (index != 0) |
| | | 70 | | { |
| | 0 | 71 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(index), |
| | | 72 | | } |
| | 0 | 73 | | } |
| | | 74 | | |
| | | 75 | | private bool MatchesSingleton(T item) |
| | | 76 | | { |
| | 0 | 77 | | return Count == 1 && Compare(_singleton, item); |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | public int IndexOf(T item) |
| | | 81 | | { |
| | 0 | 82 | | if (_list == null) |
| | | 83 | | { |
| | 0 | 84 | | return MatchesSingleton(item) ? 0 : -1; |
| | | 85 | | } |
| | | 86 | | else |
| | | 87 | | { |
| | 0 | 88 | | return _list.IndexOf(item); |
| | | 89 | | } |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | public bool Remove(T item) |
| | | 93 | | { |
| | 0 | 94 | | if (_list == null) |
| | | 95 | | { |
| | 0 | 96 | | if (MatchesSingleton(item)) |
| | | 97 | | { |
| | 0 | 98 | | _singleton = null; |
| | 0 | 99 | | Count = 0; |
| | 0 | 100 | | return true; |
| | | 101 | | } |
| | | 102 | | else |
| | | 103 | | { |
| | 0 | 104 | | return false; |
| | | 105 | | } |
| | | 106 | | } |
| | | 107 | | else |
| | | 108 | | { |
| | 0 | 109 | | bool result = _list.Remove(item); |
| | 0 | 110 | | if (result) |
| | | 111 | | { |
| | 0 | 112 | | Count--; |
| | | 113 | | } |
| | 0 | 114 | | return result; |
| | | 115 | | } |
| | | 116 | | } |
| | | 117 | | |
| | | 118 | | public void RemoveAt(int index) |
| | | 119 | | { |
| | 0 | 120 | | if (_list == null) |
| | | 121 | | { |
| | 0 | 122 | | EnsureValidSingletonIndex(index); |
| | 0 | 123 | | _singleton = null; |
| | 0 | 124 | | Count = 0; |
| | | 125 | | } |
| | | 126 | | else |
| | | 127 | | { |
| | 0 | 128 | | _list.RemoveAt(index); |
| | 0 | 129 | | Count--; |
| | | 130 | | } |
| | 0 | 131 | | } |
| | | 132 | | } |
| | | 133 | | } |