| | 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.Linq; |
| | 22 | | using System.Text; |
| | 23 | | using System.Threading; |
| | 24 | | using System.Threading.Tasks; |
| | 25 | | using UtilPack; |
| | 26 | |
|
| | 27 | | namespace AsyncEnumeration.Implementation.Enumerable |
| | 28 | | { |
| | 29 | |
|
| | 30 | | /// <summary> |
| | 31 | | /// This class implements <see cref="IAsyncEnumerable{T}"/> by using the given <see cref="IAsyncProvider"/> and callb |
| | 32 | | /// </summary> |
| | 33 | | /// <typeparam name="T">The type of items being enumerated.</typeparam> |
| | 34 | | internal sealed class AsyncEnumerableFunctionalWrapper<T> : IAsyncEnumerable<T> |
| | 35 | | { |
| | 36 | | private readonly Func<IAsyncEnumerator<T>> _getEnumerator; |
| | 37 | | private readonly IAsyncProvider _asyncProvider; |
| | 38 | |
|
| | 39 | | /// <summary> |
| | 40 | | /// Creates a new instance of <see cref="AsyncEnumerableFunctionalWrapper{T}"/> with given callback. |
| | 41 | | /// </summary> |
| | 42 | | /// <param name="getEnumerator">The callback to create <see cref="IAsyncEnumerator{T}"/>.</param> |
| | 43 | | /// <param name="asyncProvider">The <see cref="IAsyncProvider"/> to use.</param> |
| | 44 | | /// <exception cref="ArgumentNullException">If either of <paramref name="getEnumerator"/> or <paramref name="async |
| | 45 | | public AsyncEnumerableFunctionalWrapper( |
| | 46 | | IAsyncProvider asyncProvider, |
| | 47 | | Func<IAsyncEnumerator<T>> getEnumerator |
| | 48 | | ) |
| | 49 | | { |
| | 50 | | this._getEnumerator = ArgumentValidator.ValidateNotNull( nameof( getEnumerator ), getEnumerator ); |
| | 51 | | this._asyncProvider = ArgumentValidator.ValidateNotNull( nameof( asyncProvider ), asyncProvider ); |
| | 52 | | } |
| | 53 | |
|
| | 54 | | IAsyncProvider IAsyncEnumerable.AsyncProvider => this._asyncProvider; |
| | 55 | |
|
| | 56 | | IAsyncEnumerator<T> IAsyncEnumerable<T>.GetAsyncEnumerator() => this._getEnumerator(); |
| | 57 | | } |
| | 58 | |
|
| | 59 | | /// <summary> |
| | 60 | | /// This class implements <see cref="IAsyncEnumerable{T}"/> by using the given <see cref="IAsyncProvider"/>, callback |
| | 61 | | /// </summary> |
| | 62 | | /// <typeparam name="T">The type of items being enumerated.</typeparam> |
| | 63 | | /// <typeparam name="TArg">The type of argument for the callback.</typeparam> |
| | 64 | | internal sealed class AsyncEnumerableFunctionalWrapper<T, TArg> : IAsyncEnumerable<T> |
| | 65 | | { |
| | 66 | | private readonly Func<TArg, IAsyncEnumerator<T>> _getEnumerator; |
| | 67 | | private readonly IAsyncProvider _asyncProvider; |
| | 68 | | private readonly TArg _arg; |
| | 69 | |
|
| | 70 | | /// <summary> |
| | 71 | | /// Creates a new instance of <see cref="AsyncEnumerableFunctionalWrapper{T, TArg}"/> with given callback. |
| | 72 | | /// </summary> |
| | 73 | | /// <param name="arg">The argument for <paramref name="getEnumerator"/> callback.</param> |
| | 74 | | /// <param name="getEnumerator">The callback to create <see cref="IAsyncEnumerator{T}"/>.</param> |
| | 75 | | /// <param name="asyncProvider">The <see cref="IAsyncProvider"/> for the returned <see cref="IAsyncEnumerable{T}"/ |
| | 76 | | /// <exception cref="ArgumentNullException">If either of <paramref name="getEnumerator"/> or <paramref name="async |
| 34 | 77 | | public AsyncEnumerableFunctionalWrapper( |
| 34 | 78 | | IAsyncProvider asyncProvider, |
| 34 | 79 | | TArg arg, |
| 34 | 80 | | Func<TArg, IAsyncEnumerator<T>> getEnumerator |
| 34 | 81 | | ) |
| | 82 | | { |
| 34 | 83 | | this._arg = arg; |
| 34 | 84 | | this._getEnumerator = ArgumentValidator.ValidateNotNull( nameof( getEnumerator ), getEnumerator ); |
| 34 | 85 | | this._asyncProvider = ArgumentValidator.ValidateNotNull( nameof( asyncProvider ), asyncProvider ); |
| 34 | 86 | | } |
| | 87 | |
|
| 42 | 88 | | IAsyncProvider IAsyncEnumerable.AsyncProvider => this._asyncProvider; |
| | 89 | |
|
| 34 | 90 | | IAsyncEnumerator<T> IAsyncEnumerable<T>.GetAsyncEnumerator() => this._getEnumerator( this._arg ); |
| | 91 | | } |
| | 92 | |
|
| | 93 | | //internal static class UtilPackExtensions2 |
| | 94 | | //{ |
| | 95 | | // // TODO move to UtilPack |
| | 96 | | // public static Boolean TryAddWithLocking<TKey, TValue>( this IDictionary<TKey, TValue> dictionary, TKey key, TVal |
| | 97 | | // { |
| | 98 | | // lock ( lockObject ?? dictionary ) |
| | 99 | | // { |
| | 100 | | // var retVal = !dictionary.ContainsKey( key ); |
| | 101 | | // if ( retVal ) |
| | 102 | | // { |
| | 103 | | // dictionary.Add( key, value ); |
| | 104 | | // } |
| | 105 | |
|
| | 106 | | // return retVal; |
| | 107 | | // } |
| | 108 | | // } |
| | 109 | |
|
| | 110 | | // public static Boolean TryRemoveWithLocking<TKey, TValue>( this IDictionary<TKey, TValue> dictionary, TKey key, o |
| | 111 | | // { |
| | 112 | | // lock ( lockObject ?? dictionary ) |
| | 113 | | // { |
| | 114 | | // var retVal = dictionary.ContainsKey( key ); |
| | 115 | | // value = retVal ? dictionary[key] : default; |
| | 116 | | // dictionary.Remove( key ); |
| | 117 | | // return retVal; |
| | 118 | | // } |
| | 119 | | // } |
| | 120 | |
|
| | 121 | | // public static void AddWithLocking<TValue>( this IList<TValue> list, TValue item, Object lockObject = null ) |
| | 122 | | // { |
| | 123 | | // lock ( lockObject ?? list ) |
| | 124 | | // { |
| | 125 | | // list.Add( item ); |
| | 126 | | // } |
| | 127 | | // } |
| | 128 | |
|
| | 129 | | // public static Boolean TryPopWithLocking<TValue>( this IList<TValue> list, out TValue value, Object lockObject = |
| | 130 | | // { |
| | 131 | | // lock ( lockObject ?? list ) |
| | 132 | | // { |
| | 133 | | // var count = list.Count; |
| | 134 | | // var retVal = list.Count > 0; |
| | 135 | |
|
| | 136 | | // value = retVal ? list[count - 1] : default; |
| | 137 | | // list.RemoveAt( count - 1 ); |
| | 138 | | // return retVal; |
| | 139 | | // } |
| | 140 | | // } |
| | 141 | |
|
| | 142 | | // public static void ClearWithLocking<TValue>( this ICollection<TValue> collection, Object lockObject = null ) |
| | 143 | | // { |
| | 144 | | // lock ( lockObject ?? collection ) |
| | 145 | | // { |
| | 146 | | // collection.Clear(); |
| | 147 | | // } |
| | 148 | | // } |
| | 149 | | //} |
| | 150 | |
|
| | 151 | | } |