Last.cs 7.0 KB

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