Single.cs 7.3 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> 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).Single(cancellationToken);
  46. }
  47. public static Task<TSource> Single<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).Single(cancellationToken);
  54. }
  55. public static Task<TSource> SingleOrDefault<TSource>(this IAsyncEnumerable<TSource> source)
  56. {
  57. if (source == null)
  58. throw new ArgumentNullException(nameof(source));
  59. return SingleOrDefault(source, CancellationToken.None);
  60. }
  61. public static Task<TSource> SingleOrDefault<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 SingleOrDefault(source, predicate, CancellationToken.None);
  68. }
  69. public static Task<TSource> SingleOrDefault<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 SingleOrDefault(source, predicate, CancellationToken.None);
  76. }
  77. public static Task<TSource> SingleOrDefault<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  78. {
  79. if (source == null)
  80. throw new ArgumentNullException(nameof(source));
  81. return SingleOrDefault_(source, cancellationToken);
  82. }
  83. public static Task<TSource> SingleOrDefault<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).SingleOrDefault(cancellationToken);
  90. }
  91. public static Task<TSource> SingleOrDefault<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).SingleOrDefault(cancellationToken);
  98. }
  99. private static async Task<TSource> Single_<TSource>(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  100. {
  101. if (source is IList<TSource> list)
  102. {
  103. switch (list.Count)
  104. {
  105. case 0: throw new InvalidOperationException(Strings.NO_ELEMENTS);
  106. case 1: return list[0];
  107. }
  108. throw new InvalidOperationException(Strings.MORE_THAN_ONE_ELEMENT);
  109. }
  110. var e = source.GetAsyncEnumerator();
  111. try
  112. {
  113. if (!await e.MoveNextAsync(cancellationToken).ConfigureAwait(false))
  114. {
  115. throw new InvalidOperationException(Strings.NO_ELEMENTS);
  116. }
  117. var result = e.Current;
  118. if (await e.MoveNextAsync(cancellationToken).ConfigureAwait(false))
  119. {
  120. throw new InvalidOperationException(Strings.MORE_THAN_ONE_ELEMENT);
  121. }
  122. return result;
  123. }
  124. finally
  125. {
  126. await e.DisposeAsync().ConfigureAwait(false);
  127. }
  128. }
  129. private static async Task<TSource> SingleOrDefault_<TSource>(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  130. {
  131. if (source is IList<TSource> list)
  132. {
  133. switch (list.Count)
  134. {
  135. case 0: return default(TSource);
  136. case 1: return list[0];
  137. }
  138. throw new InvalidOperationException(Strings.MORE_THAN_ONE_ELEMENT);
  139. }
  140. var e = source.GetAsyncEnumerator();
  141. try
  142. {
  143. if (!await e.MoveNextAsync(cancellationToken).ConfigureAwait(false))
  144. {
  145. return default(TSource);
  146. }
  147. var result = e.Current;
  148. if (!await e.MoveNextAsync(cancellationToken).ConfigureAwait(false))
  149. {
  150. return result;
  151. }
  152. }
  153. finally
  154. {
  155. await e.DisposeAsync().ConfigureAwait(false);
  156. }
  157. throw new InvalidOperationException(Strings.MORE_THAN_ONE_ELEMENT);
  158. }
  159. }
  160. }