DefaultIfEmpty.cs 3.2 KB

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