DefaultIfEmpty.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 Error.ArgumentNull(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 Error.ArgumentNull(nameof(source));
  26. }
  27. return DefaultIfEmpty(source, default);
  28. }
  29. private sealed class DefaultIfEmptyAsyncIterator<TSource> : AsyncIterator<TSource>, IAsyncIListProvider<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. _source = source;
  38. _defaultValue = defaultValue;
  39. }
  40. public override AsyncIterator<TSource> Clone()
  41. {
  42. return new DefaultIfEmptyAsyncIterator<TSource>(_source, _defaultValue);
  43. }
  44. public override async ValueTask DisposeAsync()
  45. {
  46. if (_enumerator != null)
  47. {
  48. await _enumerator.DisposeAsync().ConfigureAwait(false);
  49. _enumerator = null;
  50. }
  51. await base.DisposeAsync().ConfigureAwait(false);
  52. }
  53. protected override async ValueTask<bool> MoveNextCore(CancellationToken cancellationToken)
  54. {
  55. switch (state)
  56. {
  57. case AsyncIteratorState.Allocated:
  58. _enumerator = _source.GetAsyncEnumerator(cancellationToken);
  59. if (await _enumerator.MoveNextAsync().ConfigureAwait(false))
  60. {
  61. current = _enumerator.Current;
  62. state = AsyncIteratorState.Iterating;
  63. }
  64. else
  65. {
  66. current = _defaultValue;
  67. await _enumerator.DisposeAsync().ConfigureAwait(false);
  68. _enumerator = null;
  69. state = AsyncIteratorState.Disposed;
  70. }
  71. return true;
  72. case AsyncIteratorState.Iterating:
  73. if (await _enumerator.MoveNextAsync().ConfigureAwait(false))
  74. {
  75. current = _enumerator.Current;
  76. return true;
  77. }
  78. break;
  79. }
  80. await DisposeAsync().ConfigureAwait(false);
  81. return false;
  82. }
  83. public async Task<TSource[]> ToArrayAsync(CancellationToken cancellationToken)
  84. {
  85. var array = await _source.ToArray(cancellationToken).ConfigureAwait(false);
  86. return array.Length == 0 ? new[] { _defaultValue } : array;
  87. }
  88. public async Task<List<TSource>> ToListAsync(CancellationToken cancellationToken)
  89. {
  90. var list = await _source.ToList(cancellationToken).ConfigureAwait(false);
  91. if (list.Count == 0)
  92. {
  93. list.Add(_defaultValue);
  94. }
  95. return list;
  96. }
  97. public async Task<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
  98. {
  99. int count;
  100. if (!onlyIfCheap || _source is ICollection<TSource> || _source is ICollection)
  101. {
  102. count = await _source.Count(cancellationToken).ConfigureAwait(false);
  103. }
  104. else
  105. {
  106. var listProv = _source as IAsyncIListProvider<TSource>;
  107. count = listProv == null ? -1 : await listProv.GetCountAsync(onlyIfCheap: true, cancellationToken).ConfigureAwait(false);
  108. }
  109. return count == 0 ? 1 : count;
  110. }
  111. }
  112. }
  113. }