Last.cs 6.8 KB

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