| | | 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.Xml; |
| | | 7 | | |
| | | 8 | | namespace CoreWCF.IdentityModel |
| | | 9 | | { |
| | | 10 | | internal class IdentityModelDictionary : IXmlDictionary |
| | | 11 | | { |
| | 0 | 12 | | public static readonly IdentityModelDictionary Version1 = new IdentityModelDictionary(new IdentityModelStringsVe |
| | | 13 | | private readonly IdentityModelStrings _strings; |
| | | 14 | | private readonly int _count; |
| | | 15 | | private XmlDictionaryString[] _dictionaryStrings; |
| | | 16 | | private Dictionary<string, int> _dictionary; |
| | | 17 | | private XmlDictionaryString[] _versionedDictionaryStrings; |
| | | 18 | | |
| | 0 | 19 | | public IdentityModelDictionary(IdentityModelStrings strings) |
| | | 20 | | { |
| | 0 | 21 | | _strings = strings; |
| | 0 | 22 | | _count = strings.Count; |
| | 0 | 23 | | } |
| | | 24 | | |
| | | 25 | | public static IdentityModelDictionary CurrentVersion |
| | | 26 | | { |
| | | 27 | | get |
| | | 28 | | { |
| | 0 | 29 | | return Version1; |
| | | 30 | | } |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | public XmlDictionaryString CreateString(string value, int key) |
| | | 34 | | { |
| | 0 | 35 | | return new XmlDictionaryString(this, value, key); |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | public bool TryLookup(string key, out XmlDictionaryString value) |
| | | 39 | | { |
| | 0 | 40 | | if (key == null) |
| | | 41 | | { |
| | 0 | 42 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(key))); |
| | | 43 | | } |
| | | 44 | | |
| | 0 | 45 | | if (_dictionary == null) |
| | | 46 | | { |
| | 0 | 47 | | Dictionary<string, int> dictionary = new Dictionary<string, int>(_count); |
| | 0 | 48 | | for (int i = 0; i < _count; i++) |
| | | 49 | | { |
| | 0 | 50 | | dictionary.Add(_strings[i], i); |
| | | 51 | | } |
| | | 52 | | |
| | 0 | 53 | | _dictionary = dictionary; |
| | | 54 | | } |
| | 0 | 55 | | if (_dictionary.TryGetValue(key, out int id)) |
| | | 56 | | { |
| | 0 | 57 | | return TryLookup(id, out value); |
| | | 58 | | } |
| | | 59 | | |
| | 0 | 60 | | value = null; |
| | 0 | 61 | | return false; |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | public bool TryLookup(int key, out XmlDictionaryString value) |
| | | 65 | | { |
| | 0 | 66 | | if (key < 0 || key >= _count) |
| | | 67 | | { |
| | 0 | 68 | | value = null; |
| | 0 | 69 | | return false; |
| | | 70 | | } |
| | 0 | 71 | | if (_dictionaryStrings == null) |
| | | 72 | | { |
| | 0 | 73 | | _dictionaryStrings = new XmlDictionaryString[_count]; |
| | | 74 | | } |
| | | 75 | | |
| | 0 | 76 | | XmlDictionaryString s = _dictionaryStrings[key]; |
| | 0 | 77 | | if (s == null) |
| | | 78 | | { |
| | 0 | 79 | | s = CreateString(_strings[key], key); |
| | 0 | 80 | | _dictionaryStrings[key] = s; |
| | | 81 | | } |
| | 0 | 82 | | value = s; |
| | 0 | 83 | | return true; |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | public bool TryLookup(XmlDictionaryString key, out XmlDictionaryString value) |
| | | 87 | | { |
| | 0 | 88 | | if (key == null) |
| | | 89 | | { |
| | 0 | 90 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(key))); |
| | | 91 | | } |
| | | 92 | | |
| | 0 | 93 | | if (key.Dictionary == this) |
| | | 94 | | { |
| | 0 | 95 | | value = key; |
| | 0 | 96 | | return true; |
| | | 97 | | } |
| | 0 | 98 | | if (key.Dictionary == CurrentVersion) |
| | | 99 | | { |
| | 0 | 100 | | if (_versionedDictionaryStrings == null) |
| | | 101 | | { |
| | 0 | 102 | | _versionedDictionaryStrings = new XmlDictionaryString[CurrentVersion._count]; |
| | | 103 | | } |
| | | 104 | | |
| | 0 | 105 | | XmlDictionaryString s = _versionedDictionaryStrings[key.Key]; |
| | 0 | 106 | | if (s == null) |
| | | 107 | | { |
| | 0 | 108 | | if (!TryLookup(key.Value, out s)) |
| | | 109 | | { |
| | 0 | 110 | | value = null; |
| | 0 | 111 | | return false; |
| | | 112 | | } |
| | 0 | 113 | | _versionedDictionaryStrings[key.Key] = s; |
| | | 114 | | } |
| | 0 | 115 | | value = s; |
| | 0 | 116 | | return true; |
| | | 117 | | } |
| | 0 | 118 | | value = null; |
| | 0 | 119 | | return false; |
| | | 120 | | } |
| | | 121 | | } |
| | | 122 | | } |