Last.cs 5.3 KB

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