AnyAll.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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, bool> predicate, CancellationToken cancellationToken)
  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);
  28. }
  29. public static Task<bool> Any<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate)
  30. {
  31. if (source == null)
  32. throw new ArgumentNullException(nameof(source));
  33. if (predicate == null)
  34. throw new ArgumentNullException(nameof(predicate));
  35. return Any(source, predicate, CancellationToken.None);
  36. }
  37. public static Task<bool> Any<TSource>(this IAsyncEnumerable<TSource> source)
  38. {
  39. if (source == null)
  40. throw new ArgumentNullException(nameof(source));
  41. return Any(source, CancellationToken.None);
  42. }
  43. public static Task<bool> Any<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
  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);
  50. }
  51. public static async Task<bool> Any<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  52. {
  53. if (source == null)
  54. throw new ArgumentNullException(nameof(source));
  55. using (var e = source.GetEnumerator())
  56. {
  57. return await e.MoveNext(cancellationToken).ConfigureAwait(false);
  58. }
  59. }
  60. private static async Task<bool> All_<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
  61. {
  62. using (var e = source.GetEnumerator())
  63. {
  64. while (await e.MoveNext(cancellationToken)
  65. .ConfigureAwait(false))
  66. {
  67. if (!predicate(e.Current))
  68. return false;
  69. }
  70. }
  71. return true;
  72. }
  73. private static async Task<bool> Any_<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
  74. {
  75. using (var e = source.GetEnumerator())
  76. {
  77. while (await e.MoveNext(cancellationToken)
  78. .ConfigureAwait(false))
  79. {
  80. if (predicate(e.Current))
  81. return true;
  82. }
  83. }
  84. return false;
  85. }
  86. }
  87. }