DefaultIfEmpty.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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) =>
  14. DefaultIfEmpty(source, default);
  15. public static IAsyncEnumerable<TSource> DefaultIfEmpty<TSource>(this IAsyncEnumerable<TSource> source, TSource defaultValue)
  16. {
  17. if (source == null)
  18. throw Error.ArgumentNull(nameof(source));
  19. return new DefaultIfEmptyAsyncIterator<TSource>(source, defaultValue);
  20. }
  21. private sealed class DefaultIfEmptyAsyncIterator<TSource> : AsyncIterator<TSource>, IAsyncIListProvider<TSource>
  22. {
  23. private readonly IAsyncEnumerable<TSource> _source;
  24. private readonly TSource _defaultValue;
  25. private IAsyncEnumerator<TSource> _enumerator;
  26. public DefaultIfEmptyAsyncIterator(IAsyncEnumerable<TSource> source, TSource defaultValue)
  27. {
  28. Debug.Assert(source != null);
  29. _source = source;
  30. _defaultValue = defaultValue;
  31. }
  32. public override AsyncIteratorBase<TSource> Clone()
  33. {
  34. return new DefaultIfEmptyAsyncIterator<TSource>(_source, _defaultValue);
  35. }
  36. public override async ValueTask DisposeAsync()
  37. {
  38. if (_enumerator != null)
  39. {
  40. await _enumerator.DisposeAsync().ConfigureAwait(false);
  41. _enumerator = null;
  42. }
  43. await base.DisposeAsync().ConfigureAwait(false);
  44. }
  45. protected override async ValueTask<bool> MoveNextCore()
  46. {
  47. switch (_state)
  48. {
  49. case AsyncIteratorState.Allocated:
  50. _enumerator = _source.GetAsyncEnumerator(_cancellationToken);
  51. if (await _enumerator.MoveNextAsync().ConfigureAwait(false))
  52. {
  53. _current = _enumerator.Current;
  54. _state = AsyncIteratorState.Iterating;
  55. }
  56. else
  57. {
  58. _current = _defaultValue;
  59. await _enumerator.DisposeAsync().ConfigureAwait(false);
  60. _enumerator = null;
  61. _state = AsyncIteratorState.Disposed;
  62. }
  63. return true;
  64. case AsyncIteratorState.Iterating:
  65. if (await _enumerator.MoveNextAsync().ConfigureAwait(false))
  66. {
  67. _current = _enumerator.Current;
  68. return true;
  69. }
  70. break;
  71. }
  72. await DisposeAsync().ConfigureAwait(false);
  73. return false;
  74. }
  75. public async ValueTask<TSource[]> ToArrayAsync(CancellationToken cancellationToken)
  76. {
  77. var array = await _source.ToArrayAsync(cancellationToken).ConfigureAwait(false);
  78. return array.Length == 0 ? new[] { _defaultValue } : array;
  79. }
  80. public async ValueTask<List<TSource>> ToListAsync(CancellationToken cancellationToken)
  81. {
  82. var list = await _source.ToListAsync(cancellationToken).ConfigureAwait(false);
  83. if (list.Count == 0)
  84. {
  85. list.Add(_defaultValue);
  86. }
  87. return list;
  88. }
  89. public async ValueTask<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
  90. {
  91. int count;
  92. if (!onlyIfCheap || _source is ICollection<TSource> || _source is ICollection)
  93. {
  94. count = await _source.CountAsync(cancellationToken).ConfigureAwait(false);
  95. }
  96. else if (_source is IAsyncIListProvider<TSource> listProv)
  97. {
  98. count = await listProv.GetCountAsync(onlyIfCheap: true, cancellationToken).ConfigureAwait(false);
  99. }
  100. else
  101. {
  102. count = -1;
  103. }
  104. return count == 0 ? 1 : count;
  105. }
  106. }
  107. }
  108. }