DefaultIfEmpty.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Diagnostics;
  8. using System.Linq;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. namespace System.Linq
  12. {
  13. public static partial class AsyncEnumerable
  14. {
  15. public static IAsyncEnumerable<TSource> DefaultIfEmpty<TSource>(this IAsyncEnumerable<TSource> source, TSource defaultValue)
  16. {
  17. if (source == null)
  18. throw new ArgumentNullException(nameof(source));
  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. throw new ArgumentNullException(nameof(source));
  25. return DefaultIfEmpty(source, default(TSource));
  26. }
  27. private sealed class DefaultIfEmptyAsyncIterator<TSource> : AsyncIterator<TSource>, IIListProvider<TSource>
  28. {
  29. private readonly IAsyncEnumerable<TSource> source;
  30. private readonly TSource defaultValue;
  31. private IAsyncEnumerator<TSource> enumerator;
  32. public DefaultIfEmptyAsyncIterator(IAsyncEnumerable<TSource> source, TSource defaultValue)
  33. {
  34. Debug.Assert(source != null);
  35. this.source = source;
  36. this.defaultValue = defaultValue;
  37. }
  38. public override AsyncIterator<TSource> Clone()
  39. {
  40. return new DefaultIfEmptyAsyncIterator<TSource>(source, defaultValue);
  41. }
  42. public override void Dispose()
  43. {
  44. if (enumerator != null)
  45. {
  46. enumerator.Dispose();
  47. enumerator = null;
  48. }
  49. base.Dispose();
  50. }
  51. protected override async Task<bool> MoveNextCore(CancellationToken cancellationToken)
  52. {
  53. switch (state)
  54. {
  55. case AsyncIteratorState.Allocated:
  56. enumerator = source.GetEnumerator();
  57. if (await enumerator.MoveNext(cancellationToken)
  58. .ConfigureAwait(false))
  59. {
  60. current = enumerator.Current;
  61. state = AsyncIteratorState.Iterating;
  62. }
  63. else
  64. {
  65. current = defaultValue;
  66. enumerator.Dispose();
  67. enumerator = null;
  68. state = AsyncIteratorState.Disposed;
  69. }
  70. return true;
  71. case AsyncIteratorState.Iterating:
  72. if (await enumerator.MoveNext(cancellationToken)
  73. .ConfigureAwait(false))
  74. {
  75. current = enumerator.Current;
  76. return true;
  77. }
  78. break;
  79. }
  80. Dispose();
  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 IIListProvider<TSource>;
  107. count = listProv == null ? -1 : await listProv.GetCountAsync(onlyIfCheap: true, cancellationToken: cancellationToken).ConfigureAwait(false);
  108. }
  109. return count == 0 ? 1 : count;
  110. }
  111. }
  112. }
  113. }