| | 1 | | /* |
| | 2 | | * WeShare API |
| | 3 | | * |
| | 4 | | * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) |
| | 5 | | * |
| | 6 | | * The version of the OpenAPI document: 1.0 |
| | 7 | | * Generated by: https://github.com/openapitools/openapi-generator.git |
| | 8 | | */ |
| | 9 | |
|
| | 10 | |
|
| | 11 | | using System; |
| | 12 | | using System.Collections; |
| | 13 | | using System.Collections.Generic; |
| | 14 | |
|
| | 15 | | namespace Applications.WeShare.Swagger.Client |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// A dictionary in which one key has many associated values. |
| | 19 | | /// </summary> |
| | 20 | | /// <typeparam name="TKey">The type of the key</typeparam> |
| | 21 | | /// <typeparam name="TValue">The type of the value associated with the key.</typeparam> |
| | 22 | | public class Multimap<TKey, TValue> : IDictionary<TKey, IList<TValue>> |
| | 23 | | { |
| | 24 | | #region Private Fields |
| | 25 | |
|
| | 26 | | private readonly Dictionary<TKey, IList<TValue>> _dictionary; |
| | 27 | |
|
| | 28 | | #endregion Private Fields |
| | 29 | |
|
| | 30 | | #region Constructors |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// Empty Constructor. |
| | 34 | | /// </summary> |
| 136 | 35 | | public Multimap() |
| 136 | 36 | | { |
| 136 | 37 | | _dictionary = new Dictionary<TKey, IList<TValue>>(); |
| 136 | 38 | | } |
| | 39 | |
|
| | 40 | | /// <summary> |
| | 41 | | /// Constructor with comparer. |
| | 42 | | /// </summary> |
| | 43 | | /// <param name="comparer"></param> |
| 0 | 44 | | public Multimap(IEqualityComparer<TKey> comparer) |
| 0 | 45 | | { |
| 0 | 46 | | _dictionary = new Dictionary<TKey, IList<TValue>>(comparer); |
| 0 | 47 | | } |
| | 48 | |
|
| | 49 | | #endregion Constructors |
| | 50 | |
|
| | 51 | | #region Enumerators |
| | 52 | |
|
| | 53 | | /// <summary> |
| | 54 | | /// To get the enumerator. |
| | 55 | | /// </summary> |
| | 56 | | /// <returns>Enumerator</returns> |
| | 57 | | public IEnumerator<KeyValuePair<TKey, IList<TValue>>> GetEnumerator() |
| 102 | 58 | | { |
| 102 | 59 | | return _dictionary.GetEnumerator(); |
| 102 | 60 | | } |
| | 61 | |
|
| | 62 | | /// <summary> |
| | 63 | | /// To get the enumerator. |
| | 64 | | /// </summary> |
| | 65 | | /// <returns>Enumerator</returns> |
| | 66 | | IEnumerator IEnumerable.GetEnumerator() |
| 0 | 67 | | { |
| 0 | 68 | | return _dictionary.GetEnumerator(); |
| 0 | 69 | | } |
| | 70 | |
|
| | 71 | | #endregion Enumerators |
| | 72 | |
|
| | 73 | | #region Public Members |
| | 74 | | /// <summary> |
| | 75 | | /// Add values to Multimap |
| | 76 | | /// </summary> |
| | 77 | | /// <param name="item">Key value pair</param> |
| | 78 | | public void Add(KeyValuePair<TKey, IList<TValue>> item) |
| 0 | 79 | | { |
| 0 | 80 | | if (!TryAdd(item.Key, item.Value)) |
| 0 | 81 | | throw new InvalidOperationException("Could not add values to Multimap."); |
| 0 | 82 | | } |
| | 83 | |
|
| | 84 | | /// <summary> |
| | 85 | | /// Add Multimap to Multimap |
| | 86 | | /// </summary> |
| | 87 | | /// <param name="multimap">Multimap</param> |
| | 88 | | public void Add(Multimap<TKey, TValue> multimap) |
| 0 | 89 | | { |
| 0 | 90 | | foreach (var item in multimap) |
| 0 | 91 | | { |
| 0 | 92 | | if (!TryAdd(item.Key, item.Value)) |
| 0 | 93 | | throw new InvalidOperationException("Could not add values to Multimap."); |
| 0 | 94 | | } |
| 0 | 95 | | } |
| | 96 | |
|
| | 97 | | /// <summary> |
| | 98 | | /// Clear Multimap |
| | 99 | | /// </summary> |
| | 100 | | public void Clear() |
| 0 | 101 | | { |
| 0 | 102 | | _dictionary.Clear(); |
| 0 | 103 | | } |
| | 104 | |
|
| | 105 | | /// <summary> |
| | 106 | | /// Determines whether Multimap contains the specified item. |
| | 107 | | /// </summary> |
| | 108 | | /// <param name="item">Key value pair</param> |
| | 109 | | /// <exception cref="NotImplementedException">Method needs to be implemented</exception> |
| | 110 | | /// <returns>true if the Multimap contains the item; otherwise, false.</returns> |
| | 111 | | public bool Contains(KeyValuePair<TKey, IList<TValue>> item) |
| 0 | 112 | | { |
| 0 | 113 | | throw new NotImplementedException(); |
| | 114 | | } |
| | 115 | |
|
| | 116 | | /// <summary> |
| | 117 | | /// Copy items of the Multimap to an array, |
| | 118 | | /// starting at a particular array index. |
| | 119 | | /// </summary> |
| | 120 | | /// <param name="array">The array that is the destination of the items copied |
| | 121 | | /// from Multimap. The array must have zero-based indexing.</param> |
| | 122 | | /// <param name="arrayIndex">The zero-based index in array at which copying begins.</param> |
| | 123 | | /// <exception cref="NotImplementedException">Method needs to be implemented</exception> |
| | 124 | | public void CopyTo(KeyValuePair<TKey, IList<TValue>>[] array, int arrayIndex) |
| 0 | 125 | | { |
| 0 | 126 | | throw new NotImplementedException(); |
| | 127 | | } |
| | 128 | |
|
| | 129 | | /// <summary> |
| | 130 | | /// Removes the specified item from the Multimap. |
| | 131 | | /// </summary> |
| | 132 | | /// <param name="item">Key value pair</param> |
| | 133 | | /// <returns>true if the item is successfully removed; otherwise, false.</returns> |
| | 134 | | /// <exception cref="NotImplementedException">Method needs to be implemented</exception> |
| | 135 | | public bool Remove(KeyValuePair<TKey, IList<TValue>> item) |
| 0 | 136 | | { |
| 0 | 137 | | throw new NotImplementedException(); |
| | 138 | | } |
| | 139 | |
|
| | 140 | | /// <summary> |
| | 141 | | /// Gets the number of items contained in the Multimap. |
| | 142 | | /// </summary> |
| 0 | 143 | | public int Count => _dictionary.Count; |
| | 144 | |
|
| | 145 | | /// <summary> |
| | 146 | | /// Gets a value indicating whether the Multimap is read-only. |
| | 147 | | /// </summary> |
| 0 | 148 | | public bool IsReadOnly => false; |
| | 149 | |
|
| | 150 | | /// <summary> |
| | 151 | | /// Adds an item with the provided key and value to the Multimap. |
| | 152 | | /// </summary> |
| | 153 | | /// <param name="key">The object to use as the key of the item to add.</param> |
| | 154 | | /// <param name="value">The object to use as the value of the item to add.</param> |
| | 155 | | /// <exception cref="InvalidOperationException">Thrown when couldn't add the value to Multimap.</exception> |
| | 156 | | public void Add(TKey key, IList<TValue> value) |
| 0 | 157 | | { |
| 0 | 158 | | if (value != null && value.Count > 0) |
| 0 | 159 | | { |
| 0 | 160 | | if (_dictionary.TryGetValue(key, out var list)) |
| 0 | 161 | | { |
| 0 | 162 | | foreach (var k in value) list.Add(k); |
| 0 | 163 | | } |
| | 164 | | else |
| 0 | 165 | | { |
| 0 | 166 | | list = new List<TValue>(value); |
| 0 | 167 | | if (!TryAdd(key, list)) |
| 0 | 168 | | throw new InvalidOperationException("Could not add values to Multimap."); |
| 0 | 169 | | } |
| 0 | 170 | | } |
| 0 | 171 | | } |
| | 172 | |
|
| | 173 | | /// <summary> |
| | 174 | | /// Determines whether the Multimap contains an item with the specified key. |
| | 175 | | /// </summary> |
| | 176 | | /// <param name="key">The key to locate in the Multimap.</param> |
| | 177 | | /// <returns>true if the Multimap contains an item with |
| | 178 | | /// the key; otherwise, false.</returns> |
| | 179 | | public bool ContainsKey(TKey key) |
| 0 | 180 | | { |
| 0 | 181 | | return _dictionary.ContainsKey(key); |
| 0 | 182 | | } |
| | 183 | |
|
| | 184 | | /// <summary> |
| | 185 | | /// Removes item with the specified key from the Multimap. |
| | 186 | | /// </summary> |
| | 187 | | /// <param name="key">The key to locate in the Multimap.</param> |
| | 188 | | /// <returns>true if the item is successfully removed; otherwise, false.</returns> |
| | 189 | | public bool Remove(TKey key) |
| 0 | 190 | | { |
| 0 | 191 | | return TryRemove(key, out var _); |
| 0 | 192 | | } |
| | 193 | |
|
| | 194 | | /// <summary> |
| | 195 | | /// Gets the value associated with the specified key. |
| | 196 | | /// </summary> |
| | 197 | | /// <param name="key">The key whose value to get.</param> |
| | 198 | | /// <param name="value">When this method returns, the value associated with the specified key, if the |
| | 199 | | /// key is found; otherwise, the default value for the type of the value parameter. |
| | 200 | | /// This parameter is passed uninitialized.</param> |
| | 201 | | /// <returns> true if the object that implements Multimap contains |
| | 202 | | /// an item with the specified key; otherwise, false.</returns> |
| | 203 | | public bool TryGetValue(TKey key, out IList<TValue> value) |
| 0 | 204 | | { |
| 0 | 205 | | return _dictionary.TryGetValue(key, out value); |
| 0 | 206 | | } |
| | 207 | |
|
| | 208 | | /// <summary> |
| | 209 | | /// Gets or sets the item with the specified key. |
| | 210 | | /// </summary> |
| | 211 | | /// <param name="key">The key of the item to get or set.</param> |
| | 212 | | /// <returns>The value of the specified key.</returns> |
| | 213 | | public IList<TValue> this[TKey key] |
| | 214 | | { |
| 7 | 215 | | get => _dictionary[key]; |
| 0 | 216 | | set => _dictionary[key] = value; |
| | 217 | | } |
| | 218 | |
|
| | 219 | | /// <summary> |
| | 220 | | /// Gets a System.Collections.Generic.ICollection containing the keys of the Multimap. |
| | 221 | | /// </summary> |
| 0 | 222 | | public ICollection<TKey> Keys => _dictionary.Keys; |
| | 223 | |
|
| | 224 | | /// <summary> |
| | 225 | | /// Gets a System.Collections.Generic.ICollection containing the values of the Multimap. |
| | 226 | | /// </summary> |
| 0 | 227 | | public ICollection<IList<TValue>> Values => _dictionary.Values; |
| | 228 | |
|
| | 229 | | /// <summary> |
| | 230 | | /// Copy the items of the Multimap to an System.Array, |
| | 231 | | /// starting at a particular System.Array index. |
| | 232 | | /// </summary> |
| | 233 | | /// <param name="array">The one-dimensional System.Array that is the destination of the items copied |
| | 234 | | /// from Multimap. The System.Array must have zero-based indexing.</param> |
| | 235 | | /// <param name="index">The zero-based index in array at which copying begins.</param> |
| | 236 | | public void CopyTo(Array array, int index) |
| 0 | 237 | | { |
| 0 | 238 | | ((ICollection)_dictionary).CopyTo(array, index); |
| 0 | 239 | | } |
| | 240 | |
|
| | 241 | | /// <summary> |
| | 242 | | /// Adds an item with the provided key and value to the Multimap. |
| | 243 | | /// </summary> |
| | 244 | | /// <param name="key">The object to use as the key of the item to add.</param> |
| | 245 | | /// <param name="value">The object to use as the value of the item to add.</param> |
| | 246 | | /// <exception cref="InvalidOperationException">Thrown when couldn't add value to Multimap.</exception> |
| | 247 | | public void Add(TKey key, TValue value) |
| 141 | 248 | | { |
| 141 | 249 | | if (value != null) |
| 141 | 250 | | { |
| 141 | 251 | | if (_dictionary.TryGetValue(key, out var list)) |
| 0 | 252 | | { |
| 0 | 253 | | list.Add(value); |
| 0 | 254 | | } |
| | 255 | | else |
| 141 | 256 | | { |
| 141 | 257 | | list = new List<TValue> { value }; |
| 141 | 258 | | if (!TryAdd(key, list)) |
| 0 | 259 | | throw new InvalidOperationException("Could not add value to Multimap."); |
| 141 | 260 | | } |
| 141 | 261 | | } |
| 141 | 262 | | } |
| | 263 | |
|
| | 264 | | #endregion Public Members |
| | 265 | |
|
| | 266 | | #region Private Members |
| | 267 | |
|
| | 268 | | /** |
| | 269 | | * Helper method to encapsulate generator differences between dictionary types. |
| | 270 | | */ |
| | 271 | | private bool TryRemove(TKey key, out IList<TValue> value) |
| 0 | 272 | | { |
| 0 | 273 | | _dictionary.TryGetValue(key, out value); |
| 0 | 274 | | return _dictionary.Remove(key); |
| 0 | 275 | | } |
| | 276 | |
|
| | 277 | | /** |
| | 278 | | * Helper method to encapsulate generator differences between dictionary types. |
| | 279 | | */ |
| | 280 | | private bool TryAdd(TKey key, IList<TValue> value) |
| 141 | 281 | | { |
| | 282 | | try |
| 141 | 283 | | { |
| 141 | 284 | | _dictionary.Add(key, value); |
| 141 | 285 | | } |
| 0 | 286 | | catch (ArgumentException) |
| 0 | 287 | | { |
| 0 | 288 | | return false; |
| | 289 | | } |
| | 290 | |
|
| 141 | 291 | | return true; |
| 141 | 292 | | } |
| | 293 | | #endregion Private Members |
| | 294 | | } |
| | 295 | | } |