Single.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace System.Linq
  10. {
  11. public static partial class AsyncEnumerable
  12. {
  13. public static Task<TSource> Single<TSource>(this IAsyncEnumerable<TSource> source)
  14. {
  15. if (source == null)
  16. throw new ArgumentNullException(nameof(source));
  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. throw new ArgumentNullException(nameof(source));
  23. if (predicate == null)
  24. throw new ArgumentNullException(nameof(predicate));
  25. return Single(source, predicate, CancellationToken.None);
  26. }
  27. public static Task<TSource> Single<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  28. {
  29. if (source == null)
  30. throw new ArgumentNullException(nameof(source));
  31. return Single_(source, cancellationToken);
  32. }
  33. public static Task<TSource> Single<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
  34. {
  35. if (source == null)
  36. throw new ArgumentNullException(nameof(source));
  37. if (predicate == null)
  38. throw new ArgumentNullException(nameof(predicate));
  39. return source.Where(predicate)
  40. .Single(cancellationToken);
  41. }
  42. public static Task<TSource> SingleOrDefault<TSource>(this IAsyncEnumerable<TSource> source)
  43. {
  44. if (source == null)
  45. throw new ArgumentNullException(nameof(source));
  46. return SingleOrDefault(source, CancellationToken.None);
  47. }
  48. public static Task<TSource> SingleOrDefault<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate)
  49. {
  50. if (source == null)
  51. throw new ArgumentNullException(nameof(source));
  52. if (predicate == null)
  53. throw new ArgumentNullException(nameof(predicate));
  54. return SingleOrDefault(source, predicate, CancellationToken.None);
  55. }
  56. public static Task<TSource> SingleOrDefault<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  57. {
  58. if (source == null)
  59. throw new ArgumentNullException(nameof(source));
  60. return SingleOrDefault_(source, cancellationToken);
  61. }
  62. public static Task<TSource> SingleOrDefault<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
  63. {
  64. if (source == null)
  65. throw new ArgumentNullException(nameof(source));
  66. if (predicate == null)
  67. throw new ArgumentNullException(nameof(predicate));
  68. return source.Where(predicate)
  69. .SingleOrDefault(cancellationToken);
  70. }
  71. private static async Task<TSource> Single_<TSource>(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  72. {
  73. if (source is IList<TSource> list)
  74. {
  75. switch (list.Count)
  76. {
  77. case 0: throw new InvalidOperationException(Strings.NO_ELEMENTS);
  78. case 1: return list[0];
  79. }
  80. throw new InvalidOperationException(Strings.MORE_THAN_ONE_ELEMENT);
  81. }
  82. var e = source.GetAsyncEnumerator();
  83. try
  84. {
  85. if (!await e.MoveNextAsync(cancellationToken)
  86. .ConfigureAwait(false))
  87. {
  88. throw new InvalidOperationException(Strings.NO_ELEMENTS);
  89. }
  90. var result = e.Current;
  91. if (await e.MoveNextAsync(cancellationToken)
  92. .ConfigureAwait(false))
  93. {
  94. throw new InvalidOperationException(Strings.MORE_THAN_ONE_ELEMENT);
  95. }
  96. return result;
  97. }
  98. finally
  99. {
  100. await e.DisposeAsync().ConfigureAwait(false);
  101. }
  102. }
  103. private static async Task<TSource> SingleOrDefault_<TSource>(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  104. {
  105. if (source is IList<TSource> list)
  106. {
  107. switch (list.Count)
  108. {
  109. case 0: return default(TSource);
  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. return default(TSource);
  121. }
  122. var result = e.Current;
  123. if (!await e.MoveNextAsync(cancellationToken)
  124. .ConfigureAwait(false))
  125. {
  126. return result;
  127. }
  128. }
  129. finally
  130. {
  131. await e.DisposeAsync().ConfigureAwait(false);
  132. }
  133. throw new InvalidOperationException(Strings.MORE_THAN_ONE_ELEMENT);
  134. }
  135. }
  136. }