1
0

Single.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. {
  15. throw new ArgumentNullException(nameof(source));
  16. }
  17. return Single(source, CancellationToken.None);
  18. }
  19. public static Task<TSource> Single<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 Single(source, predicate, CancellationToken.None);
  30. }
  31. public static Task<TSource> Single<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  32. {
  33. if (source == null)
  34. {
  35. throw new ArgumentNullException(nameof(source));
  36. }
  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. {
  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. .Single(cancellationToken);
  51. }
  52. public static Task<TSource> SingleOrDefault<TSource>(this IAsyncEnumerable<TSource> source)
  53. {
  54. if (source == null)
  55. {
  56. throw new ArgumentNullException(nameof(source));
  57. }
  58. return SingleOrDefault(source, CancellationToken.None);
  59. }
  60. public static Task<TSource> SingleOrDefault<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 SingleOrDefault(source, predicate, CancellationToken.None);
  71. }
  72. public static Task<TSource> SingleOrDefault<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  73. {
  74. if (source == null)
  75. {
  76. throw new ArgumentNullException(nameof(source));
  77. }
  78. return SingleOrDefault_(source, cancellationToken);
  79. }
  80. public static Task<TSource> SingleOrDefault<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. .SingleOrDefault(cancellationToken);
  92. }
  93. private static async Task<TSource> Single_<TSource>(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  94. {
  95. if (source is IList<TSource> list)
  96. {
  97. switch (list.Count)
  98. {
  99. case 0: throw new InvalidOperationException(Strings.NO_ELEMENTS);
  100. case 1: return list[0];
  101. }
  102. throw new InvalidOperationException(Strings.MORE_THAN_ONE_ELEMENT);
  103. }
  104. using (var e = source.GetEnumerator())
  105. {
  106. if (!await e.MoveNext(cancellationToken)
  107. .ConfigureAwait(false))
  108. {
  109. throw new InvalidOperationException(Strings.NO_ELEMENTS);
  110. }
  111. var result = e.Current;
  112. if (await e.MoveNext(cancellationToken)
  113. .ConfigureAwait(false))
  114. {
  115. throw new InvalidOperationException(Strings.MORE_THAN_ONE_ELEMENT);
  116. }
  117. return result;
  118. }
  119. }
  120. private static async Task<TSource> SingleOrDefault_<TSource>(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  121. {
  122. if (source is IList<TSource> list)
  123. {
  124. switch (list.Count)
  125. {
  126. case 0: return default;
  127. case 1: return list[0];
  128. }
  129. throw new InvalidOperationException(Strings.MORE_THAN_ONE_ELEMENT);
  130. }
  131. using (var e = source.GetEnumerator())
  132. {
  133. if (!await e.MoveNext(cancellationToken)
  134. .ConfigureAwait(false))
  135. {
  136. return default;
  137. }
  138. var result = e.Current;
  139. if (!await e.MoveNext(cancellationToken)
  140. .ConfigureAwait(false))
  141. {
  142. return result;
  143. }
  144. }
  145. throw new InvalidOperationException(Strings.MORE_THAN_ONE_ELEMENT);
  146. }
  147. }
  148. }