DefaultIfEmpty.cs 4.7 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, TSource defaultValue)
  14. {
  15. if (source == null)
  16. throw new ArgumentNullException(nameof(source));
  17. return new DefaultIfEmptyAsyncIterator<TSource>(source, defaultValue);
  18. }
  19. public static IAsyncEnumerable<TSource> DefaultIfEmpty<TSource>(this IAsyncEnumerable<TSource> source)
  20. {
  21. if (source == null)
  22. throw new ArgumentNullException(nameof(source));
  23. return DefaultIfEmpty(source, default(TSource));
  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. this.source = source;
  34. this.defaultValue = defaultValue;
  35. }
  36. public override AsyncIterator<TSource> Clone()
  37. {
  38. return new DefaultIfEmptyAsyncIterator<TSource>(source, defaultValue);
  39. }
  40. public override async Task 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 Task<bool> MoveNextCore()
  50. {
  51. switch (state)
  52. {
  53. case AsyncIteratorState.Allocated:
  54. enumerator = source.GetAsyncEnumerator();
  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.ToArray(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.ToList(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.Count(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: cancellationToken).ConfigureAwait(false);
  104. }
  105. return count == 0 ? 1 : count;
  106. }
  107. }
  108. }
  109. }