| | 1 | | /* |
| | 2 | | * Copyright 2017 Stanislav Muhametsin. All rights Reserved. |
| | 3 | | * |
| | 4 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
| | 5 | | * you may not use this file except in compliance with the License. |
| | 6 | | * You may obtain a copy of the License at |
| | 7 | | * |
| | 8 | | * http://www.apache.org/licenses/LICENSE-2.0 |
| | 9 | | * |
| | 10 | | * Unless required by applicable law or agreed to in writing, software |
| | 11 | | * distributed under the License is distributed on an "AS IS" BASIS, |
| | 12 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| | 13 | | * implied. |
| | 14 | | * |
| | 15 | | * See the License for the specific language governing permissions and |
| | 16 | | * limitations under the License. |
| | 17 | | */ |
| | 18 | | using AsyncEnumeration.Abstractions; |
| | 19 | | using System; |
| | 20 | | using System.Collections.Generic; |
| | 21 | | using System.Text; |
| | 22 | | using System.Threading; |
| | 23 | | using System.Threading.Tasks; |
| | 24 | | using UtilPack; |
| | 25 | |
|
| | 26 | | #if !NETSTANDARD1_0 |
| | 27 | | using System.Collections.Concurrent; |
| | 28 | | #endif |
| | 29 | |
|
| | 30 | | public static partial class E_AsyncEnumeration |
| | 31 | | { |
| | 32 | | /// <summary> |
| | 33 | | /// General-purpose extension method to add all items of this <see cref="IAsyncEnumerable{T}"/> to given collection. |
| | 34 | | /// </summary> |
| | 35 | | /// <typeparam name="T">The type of items being enumerated.</typeparam> |
| | 36 | | /// <typeparam name="TCollection">The type of collection to add items to.</typeparam> |
| | 37 | | /// <param name="enumerable">This <see cref="IAsyncEnumerable{T}"/>.</param> |
| | 38 | | /// <param name="collection">The collection to add to.</param> |
| | 39 | | /// <param name="addItem">The callback to add the to the <paramref name="collection"/>.</param> |
| | 40 | | /// <returns>Potentially asynchronously returns the amount of items encountered.</returns> |
| | 41 | | /// <exception cref="NullReferenceException">If this <see cref="IAsyncEnumerable{T}"/> is <c>null</c>.</exception> |
| | 42 | | /// <exception cref="ArgumentNullException">If <paramref name="addItem"/> is <c>null</c>.</exception> |
| | 43 | | public static ValueTask<Int64> AddToCollectionAsync<T, TCollection>( this IAsyncEnumerable<T> enumerable, TCollection |
| | 44 | | { |
| 42 | 45 | | ArgumentValidator.ValidateNotNullReference( enumerable ); |
| 42 | 46 | | ArgumentValidator.ValidateNotNull( nameof( addItem ), addItem ); |
| | 47 | |
|
| 251 | 48 | | return enumerable.EnumerateAsync( item => addItem( collection, item ) ); |
| | 49 | | } |
| | 50 | |
|
| | 51 | | /// <summary> |
| | 52 | | /// General-purpose extension method to add all items of this <see cref="IAsyncEnumerable{T}"/> to given collection. |
| | 53 | | /// </summary> |
| | 54 | | /// <typeparam name="T">The type of items being enumerated.</typeparam> |
| | 55 | | /// <typeparam name="TCollection">The type of collection to add items to.</typeparam> |
| | 56 | | /// <param name="enumerable">This <see cref="IAsyncEnumerable{T}"/>.</param> |
| | 57 | | /// <param name="collection">The collection to add to.</param> |
| | 58 | | /// <param name="addItem">The callback to add the to the <paramref name="collection"/>.</param> |
| | 59 | | /// <returns>Potentially asynchronously returns the amount of items encountered.</returns> |
| | 60 | | /// <exception cref="NullReferenceException">If this <see cref="IAsyncEnumerable{T}"/> is <c>null</c>.</exception> |
| | 61 | | /// <exception cref="ArgumentNullException">If <paramref name="addItem"/> is <c>null</c>.</exception> |
| | 62 | | public static ValueTask<Int64> AddToCollectionAsync<T, TCollection>( this IAsyncEnumerable<T> enumerable, TCollection |
| | 63 | | { |
| 0 | 64 | | ArgumentValidator.ValidateNotNullReference( enumerable ); |
| 0 | 65 | | ArgumentValidator.ValidateNotNull( nameof( addItem ), addItem ); |
| | 66 | |
|
| 0 | 67 | | return enumerable.EnumerateAsync( item => { return addItem( collection, item ); } ); |
| | 68 | | } |
| | 69 | |
|
| | 70 | | /// <summary> |
| | 71 | | /// This extension method will enumerate this <see cref="IAsyncEnumerable{T}"/> into an array. |
| | 72 | | /// </summary> |
| | 73 | | /// <typeparam name="T">The type of items.</typeparam> |
| | 74 | | /// <param name="enumerable">This <see cref="IAsyncEnumerable{T}"/>.</param> |
| | 75 | | /// <returns>An array of enumerated items.</returns> |
| | 76 | | /// <exception cref="NullReferenceException">If this <see cref="IAsyncEnumerable{T}"/> is <c>null</c>.</exception> |
| | 77 | | public static async Task<T[]> ToArrayAsync<T>( this IAsyncEnumerable<T> enumerable ) |
| 42 | 78 | | => ( await enumerable.ToListAsync() ).ToArray(); |
| | 79 | |
|
| | 80 | | /// <summary> |
| | 81 | | /// This extension method will enumerate this <see cref="IAsyncEnumerable{T}"/> into a <see cref="List{T}"/>. |
| | 82 | | /// </summary> |
| | 83 | | /// <typeparam name="T">The type of items.</typeparam> |
| | 84 | | /// <param name="enumerable">This <see cref="IAsyncEnumerable{T}"/>.</param> |
| | 85 | | /// <returns>A <see cref="List{T}"/> of enumerated items.</returns> |
| | 86 | | /// <exception cref="NullReferenceException">If this <see cref="IAsyncEnumerable{T}"/> is <c>null</c>.</exception> |
| | 87 | | public static async Task<List<T>> ToListAsync<T>( this IAsyncEnumerable<T> enumerable ) |
| | 88 | | { |
| 42 | 89 | | ArgumentValidator.ValidateNotNullReference( enumerable ); |
| 42 | 90 | | var retVal = new List<T>(); |
| 251 | 91 | | await enumerable.AddToCollectionAsync( retVal, ( list, item ) => list.Add( item ) ); |
| 42 | 92 | | return retVal; |
| 42 | 93 | | } |
| | 94 | |
|
| | 95 | | /// <summary> |
| | 96 | | /// This extension method will enumerate this <see cref="IAsyncEnumerable{T}"/> into a <see cref="IDictionary{TKey, T |
| | 97 | | /// </summary> |
| | 98 | | /// <typeparam name="T">The type of items being enumerated.</typeparam> |
| | 99 | | /// <typeparam name="TKey">The type of dictionary keys.</typeparam> |
| | 100 | | /// <typeparam name="TValue">The type of dictionary values.</typeparam> |
| | 101 | | /// <param name="enumerable">This <see cref="IAsyncEnumerable{T}"/>.</param> |
| | 102 | | /// <param name="keySelector">The callback to create a dictionary key from enumerable item.</param> |
| | 103 | | /// <param name="valueSelector">The callback to create a dictionary value from enumerable item.</param> |
| | 104 | | /// <param name="equalityComparer">The optional <see cref="IEqualityComparer{T}"/> to use when creating dictionary.</ |
| | 105 | | /// <returns>Asynchronously returns a <see cref="IDictionary{TKey, TValue}"/> containing keys and values as returned |
| | 106 | | /// <exception cref="NullReferenceException">If this <see cref="IAsyncEnumerable{T}"/> is <c>null</c>.</exception> |
| | 107 | | /// <exception cref="ArgumentNullException">If either of <paramref name="keySelector"/> or <paramref name="valueSelec |
| | 108 | | public static async Task<IDictionary<TKey, TValue>> ToDictionaryAsync<T, TKey, TValue>( |
| | 109 | | this IAsyncEnumerable<T> enumerable, |
| | 110 | | Func<T, TKey> keySelector, |
| | 111 | | Func<T, TValue> valueSelector, |
| | 112 | | IEqualityComparer<TKey> equalityComparer = null |
| | 113 | | ) |
| | 114 | | { |
| 0 | 115 | | ArgumentValidator.ValidateNotNullReference( enumerable ); |
| 0 | 116 | | ArgumentValidator.ValidateNotNull( nameof( keySelector ), keySelector ); |
| 0 | 117 | | ArgumentValidator.ValidateNotNull( nameof( valueSelector ), valueSelector ); |
| | 118 | |
|
| 0 | 119 | | var retVal = new Dictionary<TKey, TValue>( equalityComparer ); |
| 0 | 120 | | await enumerable.AddToCollectionAsync( retVal, ( dictionary, item ) => dictionary.Add( keySelector( item ), valueS |
| 0 | 121 | | return retVal; |
| 0 | 122 | | } |
| | 123 | |
|
| | 124 | | /// <summary> |
| | 125 | | /// This extension method will enumerate this <see cref="IAsyncEnumerable{T}"/> into a <see cref="IDictionary{TKey, T |
| | 126 | | /// </summary> |
| | 127 | | /// <typeparam name="T">The type of items being enumerated.</typeparam> |
| | 128 | | /// <typeparam name="TKey">The type of dictionary keys.</typeparam> |
| | 129 | | /// <typeparam name="TValue">The type of dictionary values.</typeparam> |
| | 130 | | /// <param name="enumerable">This <see cref="IAsyncEnumerable{T}"/>.</param> |
| | 131 | | /// <param name="keySelector">The callback to potentially asynchronously create a dictionary key from enumerable item |
| | 132 | | /// <param name="valueSelector">The callback to potentially asynchronously create a dictionary value from enumerable |
| | 133 | | /// <param name="equalityComparer">The optional <see cref="IEqualityComparer{T}"/> to use when creating dictionary.</ |
| | 134 | | /// <returns>Asynchronously returns a <see cref="IDictionary{TKey, TValue}"/> containing keys and values as returned |
| | 135 | | /// <exception cref="NullReferenceException">If this <see cref="IAsyncEnumerable{T}"/> is <c>null</c>.</exception> |
| | 136 | | /// <exception cref="ArgumentNullException">If either of <paramref name="keySelector"/> or <paramref name="valueSelec |
| | 137 | | public static async Task<IDictionary<TKey, TValue>> ToDictionaryAsync<T, TKey, TValue>( |
| | 138 | | this IAsyncEnumerable<T> enumerable, |
| | 139 | | Func<T, ValueTask<TKey>> keySelector, |
| | 140 | | Func<T, ValueTask<TValue>> valueSelector, |
| | 141 | | IEqualityComparer<TKey> equalityComparer = null |
| | 142 | | ) |
| | 143 | | { |
| 0 | 144 | | ArgumentValidator.ValidateNotNullReference( enumerable ); |
| 0 | 145 | | ArgumentValidator.ValidateNotNull( nameof( keySelector ), keySelector ); |
| 0 | 146 | | ArgumentValidator.ValidateNotNull( nameof( valueSelector ), valueSelector ); |
| | 147 | |
|
| 0 | 148 | | var retVal = new Dictionary<TKey, TValue>( equalityComparer ); |
| 0 | 149 | | await enumerable.AddToCollectionAsync( retVal, async ( dictionary, item ) => dictionary.Add( await keySelector( it |
| 0 | 150 | | return retVal; |
| 0 | 151 | | } |
| | 152 | |
|
| | 153 | | /// <summary> |
| | 154 | | /// General-purpose extension method to add all items of this <see cref="IAsyncEnumerable{T}"/> to given concurrent c |
| | 155 | | /// </summary> |
| | 156 | | /// <typeparam name="T">The type of items being enumerated.</typeparam> |
| | 157 | | /// <typeparam name="TCollection">The type of collection to add items to.</typeparam> |
| | 158 | | /// <param name="enumerable">This <see cref="IAsyncEnumerable{T}"/>.</param> |
| | 159 | | /// <param name="collection">The collection to add to.</param> |
| | 160 | | /// <param name="addItem">The callback to add the to the <paramref name="collection"/>. May be executed concurrently. |
| | 161 | | /// <returns>Potentially asynchronously returns the amount of items encountered.</returns> |
| | 162 | | /// <exception cref="NullReferenceException">If this <see cref="IAsyncEnumerable{T}"/> is <c>null</c>.</exception> |
| | 163 | | /// <exception cref="ArgumentNullException">If <paramref name="addItem"/> is <c>null</c>.</exception> |
| | 164 | | public static ValueTask<Int64> AddToConcurrentCollectionAsync<T, TCollection>( this IAsyncEnumerable<T> enumerable, T |
| | 165 | | { |
| 0 | 166 | | ArgumentValidator.ValidateNotNullReference( enumerable ); |
| 0 | 167 | | ArgumentValidator.ValidateNotNull( nameof( addItem ), addItem ); |
| | 168 | |
|
| 0 | 169 | | return enumerable.EnumerateAsync( item => addItem( collection, item ) ); |
| | 170 | | } |
| | 171 | |
|
| | 172 | | /// <summary> |
| | 173 | | /// General-purpose extension method to add all items of this <see cref="IAsyncEnumerable{T}"/> to given concurrent c |
| | 174 | | /// </summary> |
| | 175 | | /// <typeparam name="T">The type of items being enumerated.</typeparam> |
| | 176 | | /// <typeparam name="TCollection">The type of collection to add items to.</typeparam> |
| | 177 | | /// <param name="enumerable">This <see cref="IAsyncEnumerable{T}"/>.</param> |
| | 178 | | /// <param name="collection">The collection to add to.</param> |
| | 179 | | /// <param name="addItem">The callback to asynchronously add the to the <paramref name="collection"/>. May be execute |
| | 180 | | /// <returns>Potentially asynchronously returns the amount of items encountered.</returns> |
| | 181 | | /// <exception cref="NullReferenceException">If this <see cref="IAsyncEnumerable{T}"/> is <c>null</c>.</exception> |
| | 182 | | /// <exception cref="ArgumentNullException">If <paramref name="addItem"/> is <c>null</c>.</exception> |
| | 183 | | public static ValueTask<Int64> AddToConcurrentCollectionAsync<T, TCollection>( this IAsyncEnumerable<T> enumerable, T |
| | 184 | | { |
| 0 | 185 | | ArgumentValidator.ValidateNotNullReference( enumerable ); |
| 0 | 186 | | ArgumentValidator.ValidateNotNull( nameof( addItem ), addItem ); |
| | 187 | |
|
| 0 | 188 | | return enumerable.EnumerateAsync( item => { return addItem( collection, item ); } ); |
| | 189 | | } |
| | 190 | |
|
| | 191 | | #if !NETSTANDARD1_0 |
| | 192 | |
|
| | 193 | | /// <summary> |
| | 194 | | /// This extension method creates a new <see cref="ConcurrentBag{T}"/> and possibly concurrently enumerates this <see |
| | 195 | | /// </summary> |
| | 196 | | /// <typeparam name="T">The type of items being enumerated.</typeparam> |
| | 197 | | /// <param name="enumerable">This <see cref="IAsyncEnumerable{T}"/>.</param> |
| | 198 | | /// <returns>A new <see cref="ConcurrentBag{T}"/> holding all items encountered while enumerating this <see cref="IAs |
| | 199 | | /// <exception cref="NullReferenceException">If this <see cref="IAsyncEnumerable{T}"/> is <c>null</c>.</exception> |
| | 200 | | public static async Task<ConcurrentBag<T>> ToConcurrentBagAsync<T>( this IAsyncEnumerable<T> enumerable ) |
| | 201 | | { |
| 0 | 202 | | var retVal = new ConcurrentBag<T>(); |
| 0 | 203 | | await enumerable.AddToConcurrentCollectionAsync( retVal, ( bag, item ) => bag.Add( item ) ); |
| 0 | 204 | | return retVal; |
| 0 | 205 | | } |
| | 206 | |
|
| | 207 | | /// <summary> |
| | 208 | | /// This extension method creates a new <see cref="ConcurrentQueue{T}"/> and possibly concurrently enumerates this <s |
| | 209 | | /// </summary> |
| | 210 | | /// <typeparam name="T">The type of items being enumerated.</typeparam> |
| | 211 | | /// <param name="enumerable">This <see cref="IAsyncEnumerable{T}"/>.</param> |
| | 212 | | /// <returns>A new <see cref="ConcurrentQueue{T}"/> holding all items encountered while enumerating this <see cref="I |
| | 213 | | /// <exception cref="NullReferenceException">If this <see cref="IAsyncEnumerable{T}"/> is <c>null</c>.</exception> |
| | 214 | | public static async Task<ConcurrentQueue<T>> ToConcurrentQueueAsync<T>( this IAsyncEnumerable<T> enumerable ) |
| | 215 | | { |
| 0 | 216 | | var retVal = new ConcurrentQueue<T>(); |
| 0 | 217 | | await enumerable.AddToConcurrentCollectionAsync( retVal, ( queue, item ) => queue.Enqueue( item ) ); |
| 0 | 218 | | return retVal; |
| 0 | 219 | | } |
| | 220 | |
|
| | 221 | | /// <summary> |
| | 222 | | /// This extension method creates a new <see cref="ConcurrentStack{T}"/> and possibly concurrently enumerates this <s |
| | 223 | | /// </summary> |
| | 224 | | /// <typeparam name="T">The type of items being enumerated.</typeparam> |
| | 225 | | /// <param name="enumerable">This <see cref="IAsyncEnumerable{T}"/>.</param> |
| | 226 | | /// <returns>A new <see cref="ConcurrentStack{T}"/> holding all items encountered while enumerating this <see cref="I |
| | 227 | | /// <exception cref="NullReferenceException">If this <see cref="IAsyncEnumerable{T}"/> is <c>null</c>.</exception> |
| | 228 | | public static async Task<ConcurrentStack<T>> ToConcurrentStackAsync<T>( this IAsyncEnumerable<T> enumerable ) |
| | 229 | | { |
| 0 | 230 | | var retVal = new ConcurrentStack<T>(); |
| 0 | 231 | | await enumerable.AddToConcurrentCollectionAsync( retVal, ( stack, item ) => stack.Push( item ) ); |
| 0 | 232 | | return retVal; |
| 0 | 233 | | } |
| | 234 | |
|
| | 235 | | /// <summary> |
| | 236 | | /// This extension method creates a new <see cref="ConcurrentBag{T}"/> and possibly concurrently enumerates this <see |
| | 237 | | /// </summary> |
| | 238 | | /// <typeparam name="T">The type of items being enumerated.</typeparam> |
| | 239 | | /// <typeparam name="U">The type of items added to <see cref="ConcurrentBag{T}"/>.</typeparam> |
| | 240 | | /// <param name="enumerable">This <see cref="IAsyncEnumerable{T}"/>.</param> |
| | 241 | | /// <param name="selector">The callback to asynchronously select an object to be added to <see cref="ConcurrentBag{T} |
| | 242 | | /// <returns>A new <see cref="ConcurrentBag{T}"/> holding all transformed items encountered while enumerating this <s |
| | 243 | | /// <remarks> |
| | 244 | | /// <para> |
| | 245 | | /// The motivation for this method is that often the items enumerated by <see cref="IAsyncEnumerable{T}"/> are "incom |
| | 246 | | /// Using <see cref="Select{T, U}(IAsyncEnumerable{T}, Func{T, ValueTask{U}})"/> method will force the <see cref="IAs |
| | 247 | | /// Therefore, using this method directly it is possible to enumerate this <see cref="IAsyncEnumerable{T}"/> possibly |
| | 248 | | /// </para> |
| | 249 | | /// </remarks> |
| | 250 | | public static async Task<ConcurrentBag<U>> ToConcurrentBagAsync<T, U>( this IAsyncEnumerable<T> enumerable, Func<T, T |
| | 251 | | { |
| 0 | 252 | | ArgumentValidator.ValidateNotNull( nameof( selector ), selector ); |
| 0 | 253 | | var retVal = new ConcurrentBag<U>(); |
| 0 | 254 | | await enumerable.AddToConcurrentCollectionAsync( retVal, async ( bag, item ) => bag.Add( await selector( item ) ) |
| 0 | 255 | | return retVal; |
| 0 | 256 | | } |
| | 257 | |
|
| | 258 | | /// <summary> |
| | 259 | | /// This extension method creates a new <see cref="ConcurrentQueue{T}"/> and possibly concurrently enumerates this <s |
| | 260 | | /// </summary> |
| | 261 | | /// <typeparam name="T">The type of items being enumerated.</typeparam> |
| | 262 | | /// <typeparam name="U">The type of items added to <see cref="ConcurrentQueue{T}"/>.</typeparam> |
| | 263 | | /// <param name="enumerable">This <see cref="IAsyncEnumerable{T}"/>.</param> |
| | 264 | | /// <param name="selector">The callback to asynchronously select an object to be added to <see cref="ConcurrentQueue{ |
| | 265 | | /// <returns>A new <see cref="ConcurrentQueue{T}"/> holding all transformed items encountered while enumerating this |
| | 266 | | /// <remarks> |
| | 267 | | /// <para> |
| | 268 | | /// The motivation for this method is that often the items enumerated by <see cref="IAsyncEnumerable{T}"/> are "incom |
| | 269 | | /// Using <see cref="Select{T, U}(IAsyncEnumerable{T}, Func{T, ValueTask{U}})"/> method will force the <see cref="IAs |
| | 270 | | /// Therefore, using this method directly it is possible to enumerate this <see cref="IAsyncEnumerable{T}"/> possibly |
| | 271 | | /// </para> |
| | 272 | | /// </remarks> |
| | 273 | | public static async Task<ConcurrentQueue<U>> ToConcurrentQueueAsync<T, U>( this IAsyncEnumerable<T> enumerable, Func< |
| | 274 | | { |
| 0 | 275 | | ArgumentValidator.ValidateNotNull( nameof( selector ), selector ); |
| 0 | 276 | | var retVal = new ConcurrentQueue<U>(); |
| 0 | 277 | | await enumerable.AddToConcurrentCollectionAsync( retVal, async ( queue, item ) => queue.Enqueue( await selector( i |
| 0 | 278 | | return retVal; |
| 0 | 279 | | } |
| | 280 | |
|
| | 281 | | /// <summary> |
| | 282 | | /// This extension method creates a new <see cref="ConcurrentStack{T}"/> and possibly concurrently enumerates this <s |
| | 283 | | /// </summary> |
| | 284 | | /// <typeparam name="T">The type of items being enumerated.</typeparam> |
| | 285 | | /// <typeparam name="U">The type of items added to <see cref="ConcurrentStack{T}"/>.</typeparam> |
| | 286 | | /// <param name="enumerable">This <see cref="IAsyncEnumerable{T}"/>.</param> |
| | 287 | | /// <param name="selector">The callback to asynchronously select an object to be added to <see cref="ConcurrentStack{ |
| | 288 | | /// <returns>A new <see cref="ConcurrentStack{T}"/> holding all transformed items encountered while enumerating this |
| | 289 | | /// <remarks> |
| | 290 | | /// <para> |
| | 291 | | /// The motivation for this method is that often the items enumerated by <see cref="IAsyncEnumerable{T}"/> are "incom |
| | 292 | | /// Using <see cref="Select{T, U}(IAsyncEnumerable{T}, Func{T, ValueTask{U}})"/> method will force the <see cref="IAs |
| | 293 | | /// Therefore, using this method directly it is possible to enumerate this <see cref="IAsyncEnumerable{T}"/> possibly |
| | 294 | | /// </para> |
| | 295 | | /// </remarks> |
| | 296 | | public static async Task<ConcurrentStack<U>> ToConcurrentStackAsync<T, U>( this IAsyncEnumerable<T> enumerable, Func< |
| | 297 | | { |
| 0 | 298 | | ArgumentValidator.ValidateNotNull( nameof( selector ), selector ); |
| 0 | 299 | | var retVal = new ConcurrentStack<U>(); |
| 0 | 300 | | await enumerable.AddToConcurrentCollectionAsync( retVal, async ( stack, item ) => stack.Push( await selector( item |
| 0 | 301 | | return retVal; |
| 0 | 302 | | } |
| | 303 | |
|
| | 304 | | /// <summary> |
| | 305 | | /// This extension method will possibly concurrently enumerate this <see cref="IAsyncEnumerable{T}"/> into a <see cre |
| | 306 | | /// </summary> |
| | 307 | | /// <typeparam name="T">The type of items being enumerated.</typeparam> |
| | 308 | | /// <typeparam name="TKey">The type of dictionary keys.</typeparam> |
| | 309 | | /// <typeparam name="TValue">The type of dictionary values.</typeparam> |
| | 310 | | /// <param name="enumerable">This <see cref="IAsyncEnumerable{T}"/>.</param> |
| | 311 | | /// <param name="keySelector">The callback to create a dictionary key from enumerable item.</param> |
| | 312 | | /// <param name="valueSelector">The callback to create a dictionary value from enumerable item.</param> |
| | 313 | | /// <param name="equalityComparer">The optional <see cref="IEqualityComparer{T}"/> to use when creating dictionary.</ |
| | 314 | | /// <returns>Asynchronously returns a <see cref="IDictionary{TKey, TValue}"/> containing keys and values as returned |
| | 315 | | /// <remarks> |
| | 316 | | /// <para> |
| | 317 | | /// TODO currently this will not throw if there are duplicate keys, unlike <see cref="ToDictionaryAsync{T, TKey, TVal |
| | 318 | | /// The behaviour needs to be unified/parametrized at some point. |
| | 319 | | /// </para> |
| | 320 | | /// </remarks> |
| | 321 | | /// <exception cref="NullReferenceException">If this <see cref="IAsyncEnumerable{T}"/> is <c>null</c>.</exception> |
| | 322 | | /// <exception cref="ArgumentNullException">If either of <paramref name="keySelector"/> or <paramref name="valueSelec |
| | 323 | | public static async Task<ConcurrentDictionary<TKey, TValue>> ToConcurrentDictionaryAsync<T, TKey, TValue>( |
| | 324 | | this IAsyncEnumerable<T> enumerable, |
| | 325 | | Func<T, TKey> keySelector, |
| | 326 | | Func<T, TValue> valueSelector, |
| | 327 | | IEqualityComparer<TKey> equalityComparer = null |
| | 328 | | ) |
| | 329 | | { |
| 0 | 330 | | ArgumentValidator.ValidateNotNullReference( enumerable ); |
| 0 | 331 | | ArgumentValidator.ValidateNotNull( nameof( keySelector ), keySelector ); |
| 0 | 332 | | ArgumentValidator.ValidateNotNull( nameof( valueSelector ), valueSelector ); |
| | 333 | |
|
| | 334 | | // Normal Dictionary<TKey, TValue> constructor accepts null as equality comparer, but ConcurrentDictionary throws. |
| 0 | 335 | | var retVal = new ConcurrentDictionary<TKey, TValue>( equalityComparer ?? EqualityComparer<TKey>.Default ); |
| 0 | 336 | | await enumerable.AddToConcurrentCollectionAsync( retVal, ( dictionary, item ) => dictionary.TryAdd( keySelector( i |
| 0 | 337 | | return retVal; |
| 0 | 338 | | } |
| | 339 | |
|
| | 340 | | /// <summary> |
| | 341 | | /// This extension method will possibly concurrently enumerate this <see cref="IAsyncEnumerable{T}"/> into a <see cre |
| | 342 | | /// </summary> |
| | 343 | | /// <typeparam name="T">The type of items being enumerated.</typeparam> |
| | 344 | | /// <typeparam name="TKey">The type of dictionary keys.</typeparam> |
| | 345 | | /// <typeparam name="TValue">The type of dictionary values.</typeparam> |
| | 346 | | /// <param name="enumerable">This <see cref="IAsyncEnumerable{T}"/>.</param> |
| | 347 | | /// <param name="keySelector">The callback to potentially asynchronously create a dictionary key from enumerable item |
| | 348 | | /// <param name="valueSelector">The callback to potentially asynchronously create a dictionary value from enumerable |
| | 349 | | /// <param name="equalityComparer">The optional <see cref="IEqualityComparer{T}"/> to use when creating dictionary.</ |
| | 350 | | /// <returns>Asynchronously returns a <see cref="IDictionary{TKey, TValue}"/> containing keys and values as returned |
| | 351 | | /// <remarks> |
| | 352 | | /// <para> |
| | 353 | | /// TODO currently this will not throw if there are duplicate keys, unlike <see cref="ToDictionaryAsync{T, TKey, TVal |
| | 354 | | /// The behaviour needs to be unified/parametrized at some point. |
| | 355 | | /// </para> |
| | 356 | | /// </remarks> |
| | 357 | | /// <exception cref="NullReferenceException">If this <see cref="IAsyncEnumerable{T}"/> is <c>null</c>.</exception> |
| | 358 | | /// <exception cref="ArgumentNullException">If either of <paramref name="keySelector"/> or <paramref name="valueSelec |
| | 359 | | public static async Task<ConcurrentDictionary<TKey, TValue>> ToConcurrentDictionaryAsync<T, TKey, TValue>( |
| | 360 | | this IAsyncEnumerable<T> enumerable, |
| | 361 | | Func<T, ValueTask<TKey>> keySelector, |
| | 362 | | Func<T, ValueTask<TValue>> valueSelector, |
| | 363 | | IEqualityComparer<TKey> equalityComparer = null |
| | 364 | | ) |
| | 365 | | { |
| 0 | 366 | | ArgumentValidator.ValidateNotNullReference( enumerable ); |
| 0 | 367 | | ArgumentValidator.ValidateNotNull( nameof( keySelector ), keySelector ); |
| 0 | 368 | | ArgumentValidator.ValidateNotNull( nameof( valueSelector ), valueSelector ); |
| | 369 | |
|
| | 370 | | // Normal Dictionary<TKey, TValue> constructor accepts null as equality comparer, but ConcurrentDictionary throws. |
| 0 | 371 | | var retVal = new ConcurrentDictionary<TKey, TValue>( equalityComparer ?? EqualityComparer<TKey>.Default ); |
| 0 | 372 | | await enumerable.AddToConcurrentCollectionAsync( retVal, async ( dictionary, item ) => dictionary.TryAdd( await ke |
| 0 | 373 | | return retVal; |
| 0 | 374 | | } |
| | 375 | |
|
| | 376 | | #endif |
| | 377 | | } |