LastOrDefault.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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> LastOrDefault<TSource>(this IAsyncEnumerable<TSource> source)
  12. {
  13. if (source == null)
  14. throw new ArgumentNullException(nameof(source));
  15. return LastOrDefaultCore(source, CancellationToken.None);
  16. }
  17. public static Task<TSource> LastOrDefault<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  18. {
  19. if (source == null)
  20. throw new ArgumentNullException(nameof(source));
  21. return LastOrDefaultCore(source, cancellationToken);
  22. }
  23. public static Task<TSource> LastOrDefault<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 LastOrDefaultCore(source, predicate, CancellationToken.None);
  30. }
  31. public static Task<TSource> LastOrDefault<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 LastOrDefaultCore(source, predicate, cancellationToken);
  38. }
  39. public static Task<TSource> LastOrDefault<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 LastOrDefaultCore(source, predicate, CancellationToken.None);
  46. }
  47. public static Task<TSource> LastOrDefault<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 LastOrDefaultCore(source, predicate, cancellationToken);
  54. }
  55. private static async Task<TSource> LastOrDefaultCore<TSource>(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  56. {
  57. var last = await TryGetLast(source, cancellationToken).ConfigureAwait(false);
  58. return last.HasValue ? last.Value : default;
  59. }
  60. private static async Task<TSource> LastOrDefaultCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
  61. {
  62. var last = await TryGetLast(source, predicate, cancellationToken).ConfigureAwait(false);
  63. return last.HasValue ? last.Value : default;
  64. }
  65. private static async Task<TSource> LastOrDefaultCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, Task<bool>> predicate, CancellationToken cancellationToken)
  66. {
  67. var last = await TryGetLast(source, predicate, cancellationToken).ConfigureAwait(false);
  68. return last.HasValue ? last.Value : default;
  69. }
  70. private static async Task<Maybe<TSource>> TryGetLast<TSource>(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  71. {
  72. if (source is IList<TSource> list)
  73. {
  74. var count = list.Count;
  75. if (count > 0)
  76. {
  77. return new Maybe<TSource>(list[count - 1]);
  78. }
  79. }
  80. else if (source is IAsyncPartition<TSource> p)
  81. {
  82. return await p.TryGetLastAsync(cancellationToken).ConfigureAwait(false);
  83. }
  84. else
  85. {
  86. var last = default(TSource);
  87. var hasLast = false;
  88. var e = source.GetAsyncEnumerator(cancellationToken);
  89. try
  90. {
  91. while (await e.MoveNextAsync().ConfigureAwait(false))
  92. {
  93. hasLast = true;
  94. last = e.Current;
  95. }
  96. }
  97. finally
  98. {
  99. await e.DisposeAsync().ConfigureAwait(false);
  100. }
  101. if (hasLast)
  102. {
  103. return new Maybe<TSource>(last);
  104. }
  105. }
  106. return new Maybe<TSource>();
  107. }
  108. private static async Task<Maybe<TSource>> TryGetLast<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
  109. {
  110. var last = default(TSource);
  111. var hasLast = false;
  112. var e = source.GetAsyncEnumerator(cancellationToken);
  113. try
  114. {
  115. while (await e.MoveNextAsync().ConfigureAwait(false))
  116. {
  117. var value = e.Current;
  118. if (predicate(value))
  119. {
  120. hasLast = true;
  121. last = value;
  122. }
  123. }
  124. }
  125. finally
  126. {
  127. await e.DisposeAsync().ConfigureAwait(false);
  128. }
  129. if (hasLast)
  130. {
  131. return new Maybe<TSource>(last);
  132. }
  133. return new Maybe<TSource>();
  134. }
  135. private static async Task<Maybe<TSource>> TryGetLast<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, Task<bool>> predicate, CancellationToken cancellationToken)
  136. {
  137. var last = default(TSource);
  138. var hasLast = false;
  139. var e = source.GetAsyncEnumerator(cancellationToken);
  140. try
  141. {
  142. while (await e.MoveNextAsync().ConfigureAwait(false))
  143. {
  144. var value = e.Current;
  145. if (await predicate(value).ConfigureAwait(false))
  146. {
  147. hasLast = true;
  148. last = value;
  149. }
  150. }
  151. }
  152. finally
  153. {
  154. await e.DisposeAsync().ConfigureAwait(false);
  155. }
  156. if (hasLast)
  157. {
  158. return new Maybe<TSource>(last);
  159. }
  160. return new Maybe<TSource>();
  161. }
  162. }
  163. }