| | 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.Reflection; |
| | 22 | | using System.Text; |
| | 23 | | using System.Threading.Tasks; |
| | 24 | | using UtilPack; |
| | 25 | |
|
| | 26 | | namespace AsyncEnumeration |
| | 27 | | { |
| | 28 | | /// <summary> |
| | 29 | | /// This is utility class for various empty asynchronous enumerables and enumerators. |
| | 30 | | /// </summary> |
| | 31 | | /// <typeparam name="T">The type of items being enumerated.</typeparam> |
| | 32 | | public static class EmptyAsync<T> |
| | 33 | | { |
| | 34 | | private sealed class EmptyAsyncEnumerable : IAsyncEnumerable<T> |
| | 35 | | { |
| | 36 | | public IAsyncEnumerator<T> GetAsyncEnumerator() |
| 7 | 37 | | => Enumerator; |
| | 38 | |
|
| | 39 | | public IAsyncProvider AsyncProvider |
| 29 | 40 | | => EmptyAsyncProvider.Instance; |
| | 41 | |
|
| | 42 | | } |
| | 43 | |
|
| | 44 | | private sealed class EmptyAsyncEnumerator : IAsyncEnumerator<T> |
| | 45 | | { |
| | 46 | | public Task<Boolean> WaitForNextAsync() |
| 8 | 47 | | => TaskUtils.False; |
| | 48 | |
|
| | 49 | | public T TryGetNext( out Boolean success ) |
| | 50 | | { |
| 1 | 51 | | success = false; |
| 1 | 52 | | return default; |
| | 53 | | } |
| | 54 | |
|
| | 55 | | public Task DisposeAsync() |
| 7 | 56 | | => TaskUtils.CompletedTask; |
| | 57 | | } |
| | 58 | |
|
| | 59 | | /// <summary> |
| | 60 | | /// Gets the <see cref="IAsyncEnumerator{T}"/> which will return no items. |
| | 61 | | /// </summary> |
| | 62 | | /// <value>The <see cref="IAsyncEnumerator{T}"/> which will return no items.</value> |
| 12 | 63 | | public static IAsyncEnumerator<T> Enumerator { get; } = new EmptyAsyncEnumerator(); |
| | 64 | |
|
| | 65 | | /// <summary> |
| | 66 | | /// Gets the <see cref="IAsyncEnumerable{T}"/> which will always return <see cref="IAsyncEnumerator{T}"/> with no |
| | 67 | | /// </summary> |
| | 68 | | /// <value>The <see cref="IAsyncEnumerable{T}"/> which will always return <see cref="IAsyncEnumerator{T}"/> with n |
| 20 | 69 | | public static IAsyncEnumerable<T> Enumerable { get; } = new EmptyAsyncEnumerable(); |
| | 70 | |
|
| | 71 | | } |
| | 72 | |
|
| | 73 | | internal sealed class EmptyAsyncProvider : IAsyncProvider |
| | 74 | | { |
| | 75 | | public static EmptyAsyncProvider Instance { get; } = new EmptyAsyncProvider(); |
| | 76 | |
|
| | 77 | | private EmptyAsyncProvider() |
| | 78 | | { |
| | 79 | |
|
| | 80 | | } |
| | 81 | |
|
| | 82 | | public Task<T> AggregateAsync<T>( IAsyncEnumerable<T> source, Func<T, T, T> func ) |
| | 83 | | => throw AsyncProviderUtilities.EmptySequenceException(); |
| | 84 | |
|
| | 85 | | public Task<T> AggregateAsync<T>( IAsyncEnumerable<T> source, Func<T, T, ValueTask<T>> asyncFunc ) |
| | 86 | | => throw AsyncProviderUtilities.EmptySequenceException(); |
| | 87 | |
|
| | 88 | | public Task<TResult> AggregateAsync<T, TResult>( IAsyncEnumerable<T> source, Func<TResult, T, TResult> func, TResu |
| | 89 | | => |
| | 90 | | #if NET40 |
| | 91 | | TaskEx |
| | 92 | | #else |
| | 93 | | Task |
| | 94 | | #endif |
| | 95 | | .FromResult( seed ); |
| | 96 | |
|
| | 97 | | public Task<TResult> AggregateAsync<T, TResult>( IAsyncEnumerable<T> source, Func<TResult, T, ValueTask<TResult>> |
| | 98 | | => |
| | 99 | | #if NET40 |
| | 100 | | TaskEx |
| | 101 | | #else |
| | 102 | | Task |
| | 103 | | #endif |
| | 104 | | .FromResult( seed ); |
| | 105 | |
|
| | 106 | | public Task<Boolean> AllAsync<T>( IAsyncEnumerable<T> source, Func<T, Boolean> predicate ) |
| | 107 | | => TaskUtils.True; |
| | 108 | |
|
| | 109 | | public Task<Boolean> AllAsync<T>( IAsyncEnumerable<T> source, Func<T, ValueTask<Boolean>> asyncPredicate ) |
| | 110 | | => TaskUtils.True; |
| | 111 | |
|
| | 112 | | public Task<Boolean> AnyAsync<T>( IAsyncEnumerable<T> source ) |
| | 113 | | => TaskUtils.False; |
| | 114 | |
|
| | 115 | | public Task<Boolean> AnyAsync<T>( IAsyncEnumerable<T> source, Func<T, Boolean> predicate ) |
| | 116 | | => TaskUtils.False; |
| | 117 | |
|
| | 118 | | public Task<Boolean> AnyAsync<T>( IAsyncEnumerable<T> source, Func<T, ValueTask<Boolean>> asyncPredicate ) |
| | 119 | | => TaskUtils.False; |
| | 120 | |
|
| | 121 | | //public ValueTask<Int64> EnumerateAsync<T>( IAsyncEnumerable<T> enumerable ) |
| | 122 | | // => new ValueTask<Int64>( 0 ); |
| | 123 | |
|
| | 124 | | //public ValueTask<Int64> EnumerateAsync<T>( IAsyncEnumerable<T> enumerable, Action<T> action ) |
| | 125 | | // => new ValueTask<Int64>( 0 ); |
| | 126 | |
|
| | 127 | | //public ValueTask<Int64> EnumerateAsync<T>( IAsyncEnumerable<T> enumerable, Func<T, Task> asyncAction ) |
| | 128 | | // => new ValueTask<Int64>( 0 ); |
| | 129 | |
|
| | 130 | | public Task<T> FirstAsync<T>( IAsyncEnumerable<T> enumerable ) |
| | 131 | | => throw AsyncProviderUtilities.EmptySequenceException(); |
| | 132 | |
|
| | 133 | | public Task<T> FirstOrDefaultAsync<T>( IAsyncEnumerable<T> enumerable ) |
| | 134 | | => |
| | 135 | | #if NET40 |
| | 136 | | TaskEx |
| | 137 | | #else |
| | 138 | | Task |
| | 139 | | #endif |
| | 140 | | .FromResult( default( T ) ); |
| | 141 | |
|
| | 142 | | public IAsyncEnumerable<U> OfType<T, U>( IAsyncEnumerable<T> enumerable ) |
| | 143 | | => AsyncProviderUtilities.IsOfType( |
| | 144 | | typeof( T ) |
| | 145 | | #if !NET40 |
| | 146 | | .GetTypeInfo() |
| | 147 | | #endif |
| | 148 | | , typeof( U ) |
| | 149 | | #if !NET40 |
| | 150 | | .GetTypeInfo() |
| | 151 | | #endif |
| | 152 | | ) ? |
| | 153 | | (IAsyncEnumerable<U>) enumerable : |
| | 154 | | EmptyAsync<U>.Enumerable; |
| | 155 | |
|
| | 156 | | public IAsyncEnumerable<U> Select<T, U>( IAsyncEnumerable<T> enumerable, Func<T, U> selector ) |
| | 157 | | => EmptyAsync<U>.Enumerable; |
| | 158 | |
|
| | 159 | | public IAsyncEnumerable<U> Select<T, U>( IAsyncEnumerable<T> enumerable, Func<T, ValueTask<U>> asyncSelector ) |
| | 160 | | => EmptyAsync<U>.Enumerable; |
| | 161 | |
|
| | 162 | | public IAsyncEnumerable<U> SelectMany<T, U>( IAsyncEnumerable<T> enumerable, Func<T, IEnumerable<U>> selector ) |
| | 163 | | => EmptyAsync<U>.Enumerable; |
| | 164 | |
|
| | 165 | | public IAsyncEnumerable<U> SelectMany<T, U>( IAsyncEnumerable<T> enumerable, Func<T, IAsyncEnumerable<U>> asyncSel |
| | 166 | | => EmptyAsync<U>.Enumerable; |
| | 167 | |
|
| | 168 | | public IAsyncEnumerable<U> SelectMany<T, U>( IAsyncEnumerable<T> enumerable, Func<T, Task<IEnumerable<U>>> selecto |
| | 169 | | => EmptyAsync<U>.Enumerable; |
| | 170 | |
|
| | 171 | | public IAsyncEnumerable<U> SelectMany<T, U>( IAsyncEnumerable<T> enumerable, Func<T, Task<IAsyncEnumerable<U>>> as |
| | 172 | | => EmptyAsync<U>.Enumerable; |
| | 173 | |
|
| | 174 | | public IAsyncEnumerable<T> Skip<T>( IAsyncEnumerable<T> enumerable, Int32 amount ) |
| | 175 | | => enumerable; |
| | 176 | |
|
| | 177 | | public IAsyncEnumerable<T> Skip<T>( IAsyncEnumerable<T> enumerable, Int64 amount ) |
| | 178 | | => enumerable; |
| | 179 | |
|
| | 180 | | public IAsyncEnumerable<T> SkipWhile<T>( IAsyncEnumerable<T> enumerable, Func<T, Boolean> predicate ) |
| | 181 | | => enumerable; |
| | 182 | |
|
| | 183 | | public IAsyncEnumerable<T> SkipWhile<T>( IAsyncEnumerable<T> enumerable, Func<T, ValueTask<Boolean>> asyncPredicat |
| | 184 | | => enumerable; |
| | 185 | |
|
| | 186 | | public IAsyncEnumerable<T> Take<T>( IAsyncEnumerable<T> enumerable, Int32 amount ) |
| | 187 | | => enumerable; |
| | 188 | |
|
| | 189 | | public IAsyncEnumerable<T> Take<T>( IAsyncEnumerable<T> enumerable, Int64 amount ) |
| | 190 | | => enumerable; |
| | 191 | |
|
| | 192 | | public IAsyncEnumerable<T> TakeWhile<T>( IAsyncEnumerable<T> enumerable, Func<T, Boolean> predicate ) |
| | 193 | | => enumerable; |
| | 194 | |
|
| | 195 | | public IAsyncEnumerable<T> TakeWhile<T>( IAsyncEnumerable<T> enumerable, Func<T, Task<Boolean>> asyncPredicate ) |
| | 196 | | => enumerable; |
| | 197 | |
|
| | 198 | | public IAsyncEnumerable<T> Where<T>( IAsyncEnumerable<T> enumerable, Func<T, Boolean> predicate ) |
| | 199 | | => enumerable; |
| | 200 | |
|
| | 201 | | public IAsyncEnumerable<T> Where<T>( IAsyncEnumerable<T> enumerable, Func<T, Task<Boolean>> asyncPredicate ) |
| | 202 | | => enumerable; |
| | 203 | | } |
| | 204 | | } |