AnyAll.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. {
  15. throw new ArgumentNullException(nameof(source));
  16. }
  17. if (predicate == null)
  18. {
  19. throw new ArgumentNullException(nameof(predicate));
  20. }
  21. return All(source, predicate, CancellationToken.None);
  22. }
  23. public static Task<bool> All<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
  24. {
  25. if (source == null)
  26. {
  27. throw new ArgumentNullException(nameof(source));
  28. }
  29. if (predicate == null)
  30. {
  31. throw new ArgumentNullException(nameof(predicate));
  32. }
  33. return All_(source, predicate, cancellationToken);
  34. }
  35. public static Task<bool> Any<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate)
  36. {
  37. if (source == null)
  38. {
  39. throw new ArgumentNullException(nameof(source));
  40. }
  41. if (predicate == null)
  42. {
  43. throw new ArgumentNullException(nameof(predicate));
  44. }
  45. return Any(source, predicate, CancellationToken.None);
  46. }
  47. public static Task<bool> Any<TSource>(this IAsyncEnumerable<TSource> source)
  48. {
  49. if (source == null)
  50. {
  51. throw new ArgumentNullException(nameof(source));
  52. }
  53. return Any(source, CancellationToken.None);
  54. }
  55. public static Task<bool> Any<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
  56. {
  57. if (source == null)
  58. {
  59. throw new ArgumentNullException(nameof(source));
  60. }
  61. if (predicate == null)
  62. {
  63. throw new ArgumentNullException(nameof(predicate));
  64. }
  65. return Any_(source, predicate, cancellationToken);
  66. }
  67. public static async Task<bool> Any<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  68. {
  69. if (source == null)
  70. {
  71. throw new ArgumentNullException(nameof(source));
  72. }
  73. using (var e = source.GetEnumerator())
  74. {
  75. return await e.MoveNext(cancellationToken).ConfigureAwait(false);
  76. }
  77. }
  78. private static async Task<bool> All_<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
  79. {
  80. using (var e = source.GetEnumerator())
  81. {
  82. while (await e.MoveNext(cancellationToken)
  83. .ConfigureAwait(false))
  84. {
  85. if (!predicate(e.Current))
  86. {
  87. return false;
  88. }
  89. }
  90. }
  91. return true;
  92. }
  93. private static async Task<bool> Any_<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
  94. {
  95. using (var e = source.GetEnumerator())
  96. {
  97. while (await e.MoveNext(cancellationToken)
  98. .ConfigureAwait(false))
  99. {
  100. if (predicate(e.Current))
  101. {
  102. return true;
  103. }
  104. }
  105. }
  106. return false;
  107. }
  108. }
  109. }