Single.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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> Single<TSource>(this IAsyncEnumerable<TSource> source)
  12. {
  13. if (source == null)
  14. throw new ArgumentNullException(nameof(source));
  15. return Single(source, CancellationToken.None);
  16. }
  17. public static Task<TSource> Single<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 Single(source, predicate, CancellationToken.None);
  24. }
  25. public static Task<TSource> Single<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 Single(source, predicate, CancellationToken.None);
  32. }
  33. public static Task<TSource> Single<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  34. {
  35. if (source == null)
  36. throw new ArgumentNullException(nameof(source));
  37. return Single_(source, cancellationToken);
  38. }
  39. public static Task<TSource> Single<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. .Single(cancellationToken);
  47. }
  48. public static Task<TSource> Single<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. .Single(cancellationToken);
  56. }
  57. public static Task<TSource> SingleOrDefault<TSource>(this IAsyncEnumerable<TSource> source)
  58. {
  59. if (source == null)
  60. throw new ArgumentNullException(nameof(source));
  61. return SingleOrDefault(source, CancellationToken.None);
  62. }
  63. public static Task<TSource> SingleOrDefault<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 SingleOrDefault(source, predicate, CancellationToken.None);
  70. }
  71. public static Task<TSource> SingleOrDefault<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 SingleOrDefault(source, predicate, CancellationToken.None);
  78. }
  79. public static Task<TSource> SingleOrDefault<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  80. {
  81. if (source == null)
  82. throw new ArgumentNullException(nameof(source));
  83. return SingleOrDefault_(source, cancellationToken);
  84. }
  85. public static Task<TSource> SingleOrDefault<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. .SingleOrDefault(cancellationToken);
  93. }
  94. public static Task<TSource> SingleOrDefault<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. .SingleOrDefault(cancellationToken);
  102. }
  103. private static async Task<TSource> Single_<TSource>(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  104. {
  105. if (source is IList<TSource> list)
  106. {
  107. switch (list.Count)
  108. {
  109. case 0: throw new InvalidOperationException(Strings.NO_ELEMENTS);
  110. case 1: return list[0];
  111. }
  112. throw new InvalidOperationException(Strings.MORE_THAN_ONE_ELEMENT);
  113. }
  114. var e = source.GetAsyncEnumerator();
  115. try
  116. {
  117. if (!await e.MoveNextAsync(cancellationToken)
  118. .ConfigureAwait(false))
  119. {
  120. throw new InvalidOperationException(Strings.NO_ELEMENTS);
  121. }
  122. var result = e.Current;
  123. if (await e.MoveNextAsync(cancellationToken)
  124. .ConfigureAwait(false))
  125. {
  126. throw new InvalidOperationException(Strings.MORE_THAN_ONE_ELEMENT);
  127. }
  128. return result;
  129. }
  130. finally
  131. {
  132. await e.DisposeAsync().ConfigureAwait(false);
  133. }
  134. }
  135. private static async Task<TSource> SingleOrDefault_<TSource>(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  136. {
  137. if (source is IList<TSource> list)
  138. {
  139. switch (list.Count)
  140. {
  141. case 0: return default(TSource);
  142. case 1: return list[0];
  143. }
  144. throw new InvalidOperationException(Strings.MORE_THAN_ONE_ELEMENT);
  145. }
  146. var e = source.GetAsyncEnumerator();
  147. try
  148. {
  149. if (!await e.MoveNextAsync(cancellationToken)
  150. .ConfigureAwait(false))
  151. {
  152. return default(TSource);
  153. }
  154. var result = e.Current;
  155. if (!await e.MoveNextAsync(cancellationToken)
  156. .ConfigureAwait(false))
  157. {
  158. return result;
  159. }
  160. }
  161. finally
  162. {
  163. await e.DisposeAsync().ConfigureAwait(false);
  164. }
  165. throw new InvalidOperationException(Strings.MORE_THAN_ONE_ELEMENT);
  166. }
  167. }
  168. }