Summary

Class:AsyncEnumeration.Implementation.Provider.OfTypeEnumerator`2
Assembly:AsyncEnumeration.Implementation.Provider
File(s):/repo-dir/contents/Source/Code/AsyncEnumeration.Implementation.Provider/OfType.cs
Covered lines:16
Uncovered lines:0
Coverable lines:16
Total lines:98
Line coverage:100%
Branch coverage:100%
Tag:7d9974899246b95481b7aa9cd3a1462ae2a67c91

Coverage History

Metrics

MethodCyclomatic complexity NPath complexity Sequence coverage Branch coverage
.ctor(...)101%0%
WaitForNextAsync()101%0%
TryGetNext(...)801%1%
DisposeAsync()101%0%

File(s)

/repo-dir/contents/Source/Code/AsyncEnumeration.Implementation.Provider/OfType.cs

#LineLine coverage
 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 */
 18using AsyncEnumeration.Abstractions;
 19using System;
 20using System.Collections.Generic;
 21using System.Reflection;
 22using System.Text;
 23using System.Threading;
 24using System.Threading.Tasks;
 25using UtilPack;
 26
 27namespace 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
 464      public OfTypeEnumerator(
 465         IAsyncEnumerator<T> source
 466         )
 67      {
 468         this._source = ArgumentValidator.ValidateNotNull( nameof( source ), source );
 469      }
 70
 71      public Task<Boolean> WaitForNextAsync()
 872         => this._source.WaitForNextAsync();
 73
 74      public U TryGetNext( out Boolean success )
 75      {
 776         var encountered = false;
 77         T item;
 778         U returnedItem = default;
 79         do
 80         {
 1181            item = this._source.TryGetNext( out success );
 1182            if ( success && item is U tmp )
 83            {
 384               encountered = true;
 385               returnedItem = tmp;
 86            }
 1187         } while ( success && !encountered );
 88
 789         success = encountered;
 790         return returnedItem;
 91      }
 92
 93      public Task DisposeAsync()
 494         => this._source.DisposeAsync();
 95   }
 96
 97
 98}