| | 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; |
| | 24 | | using System.Threading.Tasks; |
| | 25 | | using UtilPack; |
| | 26 | |
|
| | 27 | | namespace AsyncEnumeration.Implementation.Provider |
| | 28 | | { |
| | 29 | |
|
| | 30 | | public partial class DefaultAsyncProvider |
| | 31 | | { |
| | 32 | | /// <summary> |
| | 33 | | /// This extension method will return <see cref="IAsyncEnumerable{T}"/> which will return only those items which a |
| | 34 | | /// </summary> |
| | 35 | | /// <typeparam name="T">The type of source enumerable items.</typeparam> |
| | 36 | | /// <typeparam name="U">The type of target items.</typeparam> |
| | 37 | | /// <param name="enumerable">This <see cref="IAsyncEnumerable{T}"/>.</param> |
| | 38 | | /// <returns><see cref="IAsyncEnumerable{T}"/> which will return only those items which are of given type.</return |
| | 39 | | /// <exception cref="NullReferenceException">If this <see cref="IAsyncEnumerable{T}"/> is <c>null</c>.</exception> |
| | 40 | | /// <seealso cref="System.Linq.Enumerable.OfType{TResult}(System.Collections.IEnumerable)"/> |
| | 41 | | public IAsyncEnumerable<U> OfType<T, U>( IAsyncEnumerable<T> enumerable ) |
| | 42 | | { |
| | 43 | | ArgumentValidator.ValidateNotNullReference( enumerable ); |
| | 44 | | return AsyncProviderUtilities.IsOfType( |
| | 45 | | typeof( T ) |
| | 46 | | #if !NET40 |
| | 47 | | .GetTypeInfo() |
| | 48 | | #endif |
| | 49 | | , typeof( U ) |
| | 50 | | #if !NET40 |
| | 51 | | .GetTypeInfo() |
| | 52 | | #endif |
| | 53 | | ) ? |
| | 54 | | (IAsyncEnumerable<U>) enumerable : |
| | 55 | | FromTransformCallback( enumerable, e => new OfTypeEnumerator<T, U>( e ) ); |
| | 56 | | } |
| | 57 | | } |
| | 58 | |
|
| | 59 | |
|
| | 60 | | internal sealed class OfTypeEnumerator<T, U> : IAsyncEnumerator<U> |
| | 61 | | { |
| | 62 | | private readonly IAsyncEnumerator<T> _source; |
| | 63 | |
|
| 4 | 64 | | public OfTypeEnumerator( |
| 4 | 65 | | IAsyncEnumerator<T> source |
| 4 | 66 | | ) |
| | 67 | | { |
| 4 | 68 | | this._source = ArgumentValidator.ValidateNotNull( nameof( source ), source ); |
| 4 | 69 | | } |
| | 70 | |
|
| | 71 | | public Task<Boolean> WaitForNextAsync() |
| 8 | 72 | | => this._source.WaitForNextAsync(); |
| | 73 | |
|
| | 74 | | public U TryGetNext( out Boolean success ) |
| | 75 | | { |
| 7 | 76 | | var encountered = false; |
| | 77 | | T item; |
| 7 | 78 | | U returnedItem = default; |
| | 79 | | do |
| | 80 | | { |
| 11 | 81 | | item = this._source.TryGetNext( out success ); |
| 11 | 82 | | if ( success && item is U tmp ) |
| | 83 | | { |
| 3 | 84 | | encountered = true; |
| 3 | 85 | | returnedItem = tmp; |
| | 86 | | } |
| 11 | 87 | | } while ( success && !encountered ); |
| | 88 | |
|
| 7 | 89 | | success = encountered; |
| 7 | 90 | | return returnedItem; |
| | 91 | | } |
| | 92 | |
|
| | 93 | | public Task DisposeAsync() |
| 4 | 94 | | => this._source.DisposeAsync(); |
| | 95 | | } |
| | 96 | |
|
| | 97 | |
|
| | 98 | | } |