FirstOrDefault.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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<TSource> FirstOrDefault<TSource>(this IAsyncEnumerable<TSource> source)
  12. {
  13. if (source == null)
  14. throw new ArgumentNullException(nameof(source));
  15. return FirstOrDefaultCore(source, CancellationToken.None);
  16. }
  17. public static Task<TSource> FirstOrDefault<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  18. {
  19. if (source == null)
  20. throw new ArgumentNullException(nameof(source));
  21. return FirstOrDefaultCore(source, cancellationToken);
  22. }
  23. public static Task<TSource> FirstOrDefault<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate)
  24. {
  25. if (source == null)
  26. throw new ArgumentNullException(nameof(source));
  27. if (predicate == null)
  28. throw new ArgumentNullException(nameof(predicate));
  29. return FirstOrDefaultCore(source, predicate, CancellationToken.None);
  30. }
  31. public static Task<TSource> FirstOrDefault<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
  32. {
  33. if (source == null)
  34. throw new ArgumentNullException(nameof(source));
  35. if (predicate == null)
  36. throw new ArgumentNullException(nameof(predicate));
  37. return FirstOrDefaultCore(source, predicate, cancellationToken);
  38. }
  39. public static Task<TSource> FirstOrDefault<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<bool>> predicate)
  40. {
  41. if (source == null)
  42. throw new ArgumentNullException(nameof(source));
  43. if (predicate == null)
  44. throw new ArgumentNullException(nameof(predicate));
  45. return FirstOrDefaultCore(source, predicate, CancellationToken.None);
  46. }
  47. public static Task<TSource> FirstOrDefault<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<bool>> predicate, CancellationToken cancellationToken)
  48. {
  49. if (source == null)
  50. throw new ArgumentNullException(nameof(source));
  51. if (predicate == null)
  52. throw new ArgumentNullException(nameof(predicate));
  53. return FirstOrDefaultCore(source, predicate, cancellationToken);
  54. }
  55. private static async Task<TSource> FirstOrDefaultCore<TSource>(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  56. {
  57. var first = await TryGetFirst(source, cancellationToken).ConfigureAwait(false);
  58. return first.HasValue ? first.Value : default;
  59. }
  60. private static async Task<TSource> FirstOrDefaultCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
  61. {
  62. var first = await TryGetFirst(source, predicate, cancellationToken).ConfigureAwait(false);
  63. return first.HasValue ? first.Value : default;
  64. }
  65. private static async Task<TSource> FirstOrDefaultCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, Task<bool>> predicate, CancellationToken cancellationToken)
  66. {
  67. var first = await TryGetFirst(source, predicate, cancellationToken).ConfigureAwait(false);
  68. return first.HasValue ? first.Value : default;
  69. }
  70. private static async Task<Maybe<TSource>> TryGetFirst<TSource>(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  71. {
  72. if (source is IList<TSource> list)
  73. {
  74. if (list.Count > 0)
  75. {
  76. return new Maybe<TSource>(list[0]);
  77. }
  78. }
  79. else if (source is IAsyncPartition<TSource> p)
  80. {
  81. return await p.TryGetFirstAsync(cancellationToken).ConfigureAwait(false);
  82. }
  83. else
  84. {
  85. var e = source.GetAsyncEnumerator(cancellationToken);
  86. try
  87. {
  88. if (await e.MoveNextAsync().ConfigureAwait(false))
  89. {
  90. return new Maybe<TSource>(e.Current);
  91. }
  92. }
  93. finally
  94. {
  95. await e.DisposeAsync().ConfigureAwait(false);
  96. }
  97. }
  98. return new Maybe<TSource>();
  99. }
  100. private static async Task<Maybe<TSource>> TryGetFirst<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
  101. {
  102. var e = source.GetAsyncEnumerator(cancellationToken);
  103. try
  104. {
  105. if (await e.MoveNextAsync().ConfigureAwait(false))
  106. {
  107. var value = e.Current;
  108. if (predicate(value))
  109. {
  110. return new Maybe<TSource>(value);
  111. }
  112. }
  113. }
  114. finally
  115. {
  116. await e.DisposeAsync().ConfigureAwait(false);
  117. }
  118. return new Maybe<TSource>();
  119. }
  120. private static async Task<Maybe<TSource>> TryGetFirst<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, Task<bool>> predicate, CancellationToken cancellationToken)
  121. {
  122. var e = source.GetAsyncEnumerator(cancellationToken);
  123. try
  124. {
  125. if (await e.MoveNextAsync().ConfigureAwait(false))
  126. {
  127. var value = e.Current;
  128. if (await predicate(value).ConfigureAwait(false))
  129. {
  130. return new Maybe<TSource>(value);
  131. }
  132. }
  133. }
  134. finally
  135. {
  136. await e.DisposeAsync().ConfigureAwait(false);
  137. }
  138. return new Maybe<TSource>();
  139. }
  140. }
  141. }