DefaultIfEmpty.cs 4.6 KB

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