AnyAll.cs 6.9 KB

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