DefaultIfEmpty.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. this.source = source;
  35. this.defaultValue = defaultValue;
  36. Debug.Assert(source != null);
  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 State.Allocated:
  56. enumerator = source.GetEnumerator();
  57. if (await enumerator.MoveNext(cancellationToken)
  58. .ConfigureAwait(false))
  59. {
  60. current = enumerator.Current;
  61. state = State.Iterating;
  62. }
  63. else
  64. {
  65. current = defaultValue;
  66. state = State.Disposed;
  67. }
  68. return true;
  69. case State.Iterating:
  70. if (await enumerator.MoveNext(cancellationToken)
  71. .ConfigureAwait(false))
  72. {
  73. current = enumerator.Current;
  74. return true;
  75. }
  76. break;
  77. }
  78. Dispose();
  79. return false;
  80. }
  81. public async Task<TSource[]> ToArrayAsync(CancellationToken cancellationToken)
  82. {
  83. var array = await source.ToArray(cancellationToken).ConfigureAwait(false);
  84. return array.Length == 0 ? new[] { defaultValue } : array;
  85. }
  86. public async Task<List<TSource>> ToListAsync(CancellationToken cancellationToken)
  87. {
  88. var list = await source.ToList(cancellationToken).ConfigureAwait(false);
  89. if (list.Count == 0)
  90. {
  91. list.Add(defaultValue);
  92. }
  93. return list;
  94. }
  95. public async Task<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
  96. {
  97. int count;
  98. if (!onlyIfCheap || source is ICollection<TSource> || source is ICollection)
  99. {
  100. count = await source.Count(cancellationToken).ConfigureAwait(false);
  101. }
  102. else
  103. {
  104. var listProv = source as IIListProvider<TSource>;
  105. count = listProv == null ? -1 : await listProv.GetCountAsync(onlyIfCheap: true, cancellationToken: cancellationToken).ConfigureAwait(false);
  106. }
  107. return count == 0 ? 1 : count;
  108. }
  109. }
  110. }
  111. }