| | 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 UtilPack; |
| | 22 | |
|
| | 23 | |
|
| | 24 | | namespace AsyncEnumeration.Abstractions |
| | 25 | | { |
| | 26 | | public partial interface IAsyncProvider |
| | 27 | | { |
| | 28 | | /// <summary> |
| | 29 | | /// This extension method will return <see cref="IAsyncEnumerable{T}"/> which will return only those items which a |
| | 30 | | /// </summary> |
| | 31 | | /// <typeparam name="T">The type of source enumerable items.</typeparam> |
| | 32 | | /// <typeparam name="U">The type of target items.</typeparam> |
| | 33 | | /// <param name="enumerable">This <see cref="IAsyncEnumerable{T}"/>.</param> |
| | 34 | | /// <returns><see cref="IAsyncEnumerable{T}"/> which will return only those items which are of given type.</return |
| | 35 | | /// <exception cref="NullReferenceException">If this <see cref="IAsyncEnumerable{T}"/> is <c>null</c>.</exception> |
| | 36 | | /// <seealso cref="System.Linq.Enumerable.OfType{TResult}(System.Collections.IEnumerable)"/> |
| | 37 | | IAsyncEnumerable<U> OfType<T, U>( IAsyncEnumerable<T> enumerable ); |
| | 38 | | } |
| | 39 | |
|
| | 40 | | /// <summary> |
| | 41 | | /// This struct exists to make life easier when using async variation of <see cref="System.Linq.Enumerable.OfType"/>, |
| | 42 | | /// </summary> |
| | 43 | | /// <typeparam name="T"></typeparam> |
| | 44 | | public struct OfTypeInvoker<T> |
| | 45 | | { |
| | 46 | | private readonly IAsyncEnumerable<T> _source; |
| | 47 | |
|
| | 48 | | /// <summary> |
| | 49 | | /// Creates new instance of <see cref="OfTypeInvoker{T}"/> with given <see cref="IAsyncEnumerable{T}"/>. |
| | 50 | | /// </summary> |
| | 51 | | /// <param name="source">The <see cref="IAsyncEnumerable{T}"/>.</param> |
| | 52 | | /// <exception cref="ArgumentNullException">If <paramref name="source"/> is <c>null</c>.</exception> |
| | 53 | | public OfTypeInvoker( IAsyncEnumerable<T> source ) |
| | 54 | | { |
| 7 | 55 | | this._source = ArgumentValidator.ValidateNotNull( nameof( source ), source ); |
| 7 | 56 | | } |
| | 57 | |
|
| | 58 | | /// <summary> |
| | 59 | | /// Calls <see cref="IAsyncProvider.OfType"/> with <typeparamref name="T"/> as first type parameter, and <typepara |
| | 60 | | /// </summary> |
| | 61 | | /// <typeparam name="U">The type to filter the elements of the <see cref="IAsyncEnumerable{T}"/> on.</typeparam> |
| | 62 | | /// <returns>Filtered <see cref="IAsyncEnumerable{T}"/> with all the items of <typeparamref name="U"/>.</returns> |
| | 63 | | public IAsyncEnumerable<U> Type<U>() |
| | 64 | | { |
| 7 | 65 | | return ( |
| 7 | 66 | | ( this._source ?? throw new InvalidOperationException( "This operation not possible on default-constructed t |
| 7 | 67 | | .AsyncProvider ?? throw AsyncProviderUtilities.NoAsyncProviderException() |
| 7 | 68 | | ).OfType<T, U>( this._source ); |
| | 69 | | } |
| | 70 | | } |
| | 71 | | } |
| | 72 | |
|
| | 73 | | public static partial class E_AsyncEnumeration |
| | 74 | | { |
| | 75 | |
|
| | 76 | | /// <summary> |
| | 77 | | /// This extension method will return <see cref="IAsyncEnumerable{T}"/> which will return only those items which are |
| | 78 | | /// </summary> |
| | 79 | | /// <typeparam name="T">The type of source enumerable items.</typeparam> |
| | 80 | | /// <param name="enumerable">This <see cref="IAsyncEnumerable{T}"/>.</param> |
| | 81 | | /// <returns><see cref="IAsyncEnumerable{T}"/> which will return only those items which are of given type.</returns> |
| | 82 | | /// <exception cref="NullReferenceException">If this <see cref="IAsyncEnumerable{T}"/> is <c>null</c>.</exception> |
| | 83 | | /// <seealso cref="System.Linq.Enumerable.OfType{TResult}(System.Collections.IEnumerable)"/> |
| | 84 | | public static OfTypeInvoker<T> Of<T>( this IAsyncEnumerable<T> enumerable ) |
| | 85 | | => new OfTypeInvoker<T>( ArgumentValidator.ValidateNotNullReference( enumerable ) ); |
| | 86 | |
|
| | 87 | | } |