DefaultIfEmpty.cs 4.8 KB

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