AnyAll.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // // Licensed to the .NET Foundation under one or more agreements.
  2. // // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // // See the LICENSE file in the project root for more information.
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace System.Linq
  10. {
  11. public static partial class AsyncEnumerable
  12. {
  13. public static Task<bool> All<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate)
  14. {
  15. if (source == null)
  16. throw new ArgumentNullException(nameof(source));
  17. if (predicate == null)
  18. throw new ArgumentNullException(nameof(predicate));
  19. return All(source, predicate, CancellationToken.None);
  20. }
  21. public static Task<bool> All<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<bool>> predicate)
  22. {
  23. if (source == null)
  24. throw new ArgumentNullException(nameof(source));
  25. if (predicate == null)
  26. throw new ArgumentNullException(nameof(predicate));
  27. return All(source, predicate, CancellationToken.None);
  28. }
  29. public static Task<bool> All<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
  30. {
  31. if (source == null)
  32. throw new ArgumentNullException(nameof(source));
  33. if (predicate == null)
  34. throw new ArgumentNullException(nameof(predicate));
  35. return All_(source, predicate, cancellationToken);
  36. }
  37. public static Task<bool> All<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<bool>> predicate, CancellationToken cancellationToken)
  38. {
  39. if (source == null)
  40. throw new ArgumentNullException(nameof(source));
  41. if (predicate == null)
  42. throw new ArgumentNullException(nameof(predicate));
  43. return All_(source, predicate, cancellationToken);
  44. }
  45. public static Task<bool> Any<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate)
  46. {
  47. if (source == null)
  48. throw new ArgumentNullException(nameof(source));
  49. if (predicate == null)
  50. throw new ArgumentNullException(nameof(predicate));
  51. return Any(source, predicate, CancellationToken.None);
  52. }
  53. public static Task<bool> Any<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<bool>> predicate)
  54. {
  55. if (source == null)
  56. throw new ArgumentNullException(nameof(source));
  57. if (predicate == null)
  58. throw new ArgumentNullException(nameof(predicate));
  59. return Any(source, predicate, CancellationToken.None);
  60. }
  61. public static Task<bool> Any<TSource>(this IAsyncEnumerable<TSource> source)
  62. {
  63. if (source == null)
  64. throw new ArgumentNullException(nameof(source));
  65. return Any(source, CancellationToken.None);
  66. }
  67. public static Task<bool> Any<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
  68. {
  69. if (source == null)
  70. throw new ArgumentNullException(nameof(source));
  71. if (predicate == null)
  72. throw new ArgumentNullException(nameof(predicate));
  73. return Any_(source, predicate, cancellationToken);
  74. }
  75. public static Task<bool> Any<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<bool>> predicate, CancellationToken cancellationToken)
  76. {
  77. if (source == null)
  78. throw new ArgumentNullException(nameof(source));
  79. if (predicate == null)
  80. throw new ArgumentNullException(nameof(predicate));
  81. return Any_(source, predicate, cancellationToken);
  82. }
  83. public static async Task<bool> Any<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  84. {
  85. if (source == null)
  86. throw new ArgumentNullException(nameof(source));
  87. var e = source.GetAsyncEnumerator();
  88. try
  89. {
  90. return await e.MoveNextAsync(cancellationToken).ConfigureAwait(false);
  91. }
  92. finally
  93. {
  94. await e.DisposeAsync().ConfigureAwait(false);
  95. }
  96. }
  97. private static async Task<bool> All_<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
  98. {
  99. var e = source.GetAsyncEnumerator();
  100. try
  101. {
  102. while (await e.MoveNextAsync(cancellationToken)
  103. .ConfigureAwait(false))
  104. {
  105. if (!predicate(e.Current))
  106. return false;
  107. }
  108. }
  109. finally
  110. {
  111. await e.DisposeAsync().ConfigureAwait(false);
  112. }
  113. return true;
  114. }
  115. private static async Task<bool> All_<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, Task<bool>> predicate, CancellationToken cancellationToken)
  116. {
  117. var e = source.GetAsyncEnumerator();
  118. try
  119. {
  120. while (await e.MoveNextAsync(cancellationToken)
  121. .ConfigureAwait(false))
  122. {
  123. if (!await predicate(e.Current).ConfigureAwait(false))
  124. return false;
  125. }
  126. }
  127. finally
  128. {
  129. await e.DisposeAsync().ConfigureAwait(false);
  130. }
  131. return true;
  132. }
  133. private static async Task<bool> Any_<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
  134. {
  135. var e = source.GetAsyncEnumerator();
  136. try
  137. {
  138. while (await e.MoveNextAsync(cancellationToken)
  139. .ConfigureAwait(false))
  140. {
  141. if (predicate(e.Current))
  142. return true;
  143. }
  144. }
  145. finally
  146. {
  147. await e.DisposeAsync().ConfigureAwait(false);
  148. }
  149. return false;
  150. }
  151. private static async Task<bool> Any_<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, Task<bool>> predicate, CancellationToken cancellationToken)
  152. {
  153. var e = source.GetAsyncEnumerator();
  154. try
  155. {
  156. while (await e.MoveNextAsync(cancellationToken)
  157. .ConfigureAwait(false))
  158. {
  159. if (await predicate(e.Current).ConfigureAwait(false))
  160. return true;
  161. }
  162. }
  163. finally
  164. {
  165. await e.DisposeAsync().ConfigureAwait(false);
  166. }
  167. return false;
  168. }
  169. }
  170. }