DefaultIfEmpty.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace System.Linq
  10. {
  11. public static partial class AsyncEnumerable
  12. {
  13. public static IAsyncEnumerable<TSource> DefaultIfEmpty<TSource>(this IAsyncEnumerable<TSource> source, TSource defaultValue)
  14. {
  15. if (source == null)
  16. {
  17. throw new ArgumentNullException(nameof(source));
  18. }
  19. return new DefaultIfEmptyAsyncIterator<TSource>(source, defaultValue);
  20. }
  21. public static IAsyncEnumerable<TSource> DefaultIfEmpty<TSource>(this IAsyncEnumerable<TSource> source)
  22. {
  23. if (source == null)
  24. {
  25. throw new ArgumentNullException(nameof(source));
  26. }
  27. return DefaultIfEmpty(source, default);
  28. }
  29. private sealed class DefaultIfEmptyAsyncIterator<TSource> : AsyncIterator<TSource>, IIListProvider<TSource>
  30. {
  31. private readonly IAsyncEnumerable<TSource> source;
  32. private readonly TSource defaultValue;
  33. private IAsyncEnumerator<TSource> enumerator;
  34. public DefaultIfEmptyAsyncIterator(IAsyncEnumerable<TSource> source, TSource defaultValue)
  35. {
  36. Debug.Assert(source != null);
  37. this.source = source;
  38. this.defaultValue = defaultValue;
  39. }
  40. public override AsyncIterator<TSource> Clone()
  41. {
  42. return new DefaultIfEmptyAsyncIterator<TSource>(source, defaultValue);
  43. }
  44. public override void Dispose()
  45. {
  46. if (enumerator != null)
  47. {
  48. enumerator.Dispose();
  49. enumerator = null;
  50. }
  51. base.Dispose();
  52. }
  53. protected override async Task<bool> MoveNextCore(CancellationToken cancellationToken)
  54. {
  55. switch (state)
  56. {
  57. case AsyncIteratorState.Allocated:
  58. enumerator = source.GetEnumerator();
  59. if (await enumerator.MoveNext(cancellationToken)
  60. .ConfigureAwait(false))
  61. {
  62. current = enumerator.Current;
  63. state = AsyncIteratorState.Iterating;
  64. }
  65. else
  66. {
  67. current = defaultValue;
  68. enumerator.Dispose();
  69. enumerator = null;
  70. state = AsyncIteratorState.Disposed;
  71. }
  72. return true;
  73. case AsyncIteratorState.Iterating:
  74. if (await enumerator.MoveNext(cancellationToken)
  75. .ConfigureAwait(false))
  76. {
  77. current = enumerator.Current;
  78. return true;
  79. }
  80. break;
  81. }
  82. Dispose();
  83. return false;
  84. }
  85. public async Task<TSource[]> ToArrayAsync(CancellationToken cancellationToken)
  86. {
  87. var array = await source.ToArray(cancellationToken).ConfigureAwait(false);
  88. return array.Length == 0 ? new[] { defaultValue } : array;
  89. }
  90. public async Task<List<TSource>> ToListAsync(CancellationToken cancellationToken)
  91. {
  92. var list = await source.ToList(cancellationToken).ConfigureAwait(false);
  93. if (list.Count == 0)
  94. {
  95. list.Add(defaultValue);
  96. }
  97. return list;
  98. }
  99. public async Task<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
  100. {
  101. int count;
  102. if (!onlyIfCheap || source is ICollection<TSource> || source is ICollection)
  103. {
  104. count = await source.Count(cancellationToken).ConfigureAwait(false);
  105. }
  106. else
  107. {
  108. var listProv = source as IIListProvider<TSource>;
  109. count = listProv == null ? -1 : await listProv.GetCountAsync(onlyIfCheap: true, cancellationToken: cancellationToken).ConfigureAwait(false);
  110. }
  111. return count == 0 ? 1 : count;
  112. }
  113. }
  114. }
  115. }