|
| 1 | +using System.Collections; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Net; |
| 5 | +using System.Text.Json; |
| 6 | + |
| 7 | +namespace ProxyCheckUtil |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// Dictionary that deserializes IP addresses keys as <see cref="IPAddress"/> and values as <see cref="ProxyCheckResult.IpResult"/>. |
| 11 | + /// </summary> |
| 12 | + internal class IpResultDictionary : IDictionary<string, JsonElement> |
| 13 | + { |
| 14 | + private readonly Dictionary<string, JsonElement> _extensionData = new(); |
| 15 | + private readonly Dictionary<IPAddress, ProxyCheckResult.IpResult> _results; |
| 16 | + |
| 17 | + public IpResultDictionary(Dictionary<IPAddress, ProxyCheckResult.IpResult> results) |
| 18 | + { |
| 19 | + _results = results; |
| 20 | + } |
| 21 | + |
| 22 | + public IEnumerator<KeyValuePair<string, JsonElement>> GetEnumerator() |
| 23 | + { |
| 24 | + foreach (var kvp in _results) |
| 25 | + { |
| 26 | + yield return new KeyValuePair<string, JsonElement>(kvp.Key.ToString(), JsonSerializer.SerializeToDocument(kvp.Value, ProxyJsonContext.Default.IpResult).RootElement); |
| 27 | + } |
| 28 | + |
| 29 | + foreach (var kvp in _extensionData) |
| 30 | + { |
| 31 | + yield return kvp; |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + public void Add(KeyValuePair<string, JsonElement> item) |
| 36 | + { |
| 37 | + Add(item.Key, item.Value); |
| 38 | + } |
| 39 | + |
| 40 | + public void Clear() |
| 41 | + { |
| 42 | + _results.Clear(); |
| 43 | + _extensionData.Clear(); |
| 44 | + } |
| 45 | + |
| 46 | + public bool Contains(KeyValuePair<string, JsonElement> item) |
| 47 | + { |
| 48 | + return ContainsKey(item.Key); |
| 49 | + } |
| 50 | + |
| 51 | + public void CopyTo(KeyValuePair<string, JsonElement>[] array, int arrayIndex) |
| 52 | + { |
| 53 | + foreach (var kvp in _results) |
| 54 | + { |
| 55 | + array[arrayIndex++] = new KeyValuePair<string, JsonElement>(kvp.Key.ToString(), JsonSerializer.SerializeToDocument(kvp.Value, ProxyJsonContext.Default.IpResult).RootElement); |
| 56 | + } |
| 57 | + |
| 58 | + foreach (var kvp in _extensionData) |
| 59 | + { |
| 60 | + array[arrayIndex++] = kvp; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + public bool Remove(KeyValuePair<string, JsonElement> item) |
| 65 | + { |
| 66 | + return Remove(item.Key); |
| 67 | + } |
| 68 | + |
| 69 | + public int Count => _results.Count + _extensionData.Count; |
| 70 | + |
| 71 | + public bool IsReadOnly => false; |
| 72 | + |
| 73 | + public void Add(string key, JsonElement value) |
| 74 | + { |
| 75 | + if (IPAddress.TryParse(key, out var ip)) |
| 76 | + { |
| 77 | + _results.Add(ip, value.Deserialize(ProxyJsonContext.Default.IpResult)!); |
| 78 | + } |
| 79 | + else |
| 80 | + { |
| 81 | + _extensionData.Add(key, value); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + public bool ContainsKey(string key) |
| 86 | + { |
| 87 | + return IPAddress.TryParse(key, out var ip) |
| 88 | + ? _results.ContainsKey(ip) |
| 89 | + : _extensionData.ContainsKey(key); |
| 90 | + } |
| 91 | + |
| 92 | + public bool Remove(string key) |
| 93 | + { |
| 94 | + return IPAddress.TryParse(key, out var ip) |
| 95 | + ? _results.Remove(ip) |
| 96 | + : _extensionData.Remove(key); |
| 97 | + } |
| 98 | + |
| 99 | + public bool TryGetValue(string key, out JsonElement value) |
| 100 | + { |
| 101 | + if (_results.TryGetValue(IPAddress.Parse(key), out var result)) |
| 102 | + { |
| 103 | + value = JsonSerializer.SerializeToDocument(result, ProxyJsonContext.Default.IpResult).RootElement; |
| 104 | + return true; |
| 105 | + } |
| 106 | + |
| 107 | + value = default; |
| 108 | + return false; |
| 109 | + } |
| 110 | + |
| 111 | + public JsonElement this[string key] |
| 112 | + { |
| 113 | + get => IPAddress.TryParse(key, out var ip) |
| 114 | + ? JsonSerializer.SerializeToDocument(_results[ip], ProxyJsonContext.Default.IpResult).RootElement |
| 115 | + : _extensionData[key]; |
| 116 | + |
| 117 | + set |
| 118 | + { |
| 119 | + if (IPAddress.TryParse(key, out var ip)) |
| 120 | + { |
| 121 | + _results[ip] = value.Deserialize(ProxyJsonContext.Default.IpResult)!; |
| 122 | + } |
| 123 | + else |
| 124 | + { |
| 125 | + _extensionData[key] = value; |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + ICollection<string> IDictionary<string, JsonElement>.Keys => _results.Keys |
| 131 | + .Select(ip => ip.ToString()) |
| 132 | + .Concat(_extensionData.Keys) |
| 133 | + .ToList(); |
| 134 | + |
| 135 | + ICollection<JsonElement> IDictionary<string, JsonElement>.Values => _results.Values |
| 136 | + .Select(result => JsonSerializer.SerializeToDocument(result, ProxyJsonContext.Default.IpResult).RootElement) |
| 137 | + .Concat(_extensionData.Values) |
| 138 | + .ToList(); |
| 139 | + |
| 140 | + IEnumerator IEnumerable.GetEnumerator() |
| 141 | + { |
| 142 | + return GetEnumerator(); |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments