Last.cs 6.8 KB

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