// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the Apache 2.0 License. // See the LICENSE file in the project root for more information. using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Threading; using System.Threading.Tasks; namespace System.Linq { public static partial class AsyncEnumerable { public static IAsyncEnumerable DefaultIfEmpty(this IAsyncEnumerable source, TSource defaultValue) { if (source == null) throw new ArgumentNullException(nameof(source)); return new DefaultIfEmptyAsyncIterator(source, defaultValue); } public static IAsyncEnumerable DefaultIfEmpty(this IAsyncEnumerable source) { if (source == null) throw new ArgumentNullException(nameof(source)); return DefaultIfEmpty(source, default(TSource)); } private sealed class DefaultIfEmptyAsyncIterator : AsyncIterator, IIListProvider { private readonly IAsyncEnumerable source; private readonly TSource defaultValue; private IAsyncEnumerator enumerator; public DefaultIfEmptyAsyncIterator(IAsyncEnumerable source, TSource defaultValue) { Debug.Assert(source != null); this.source = source; this.defaultValue = defaultValue; } public override AsyncIterator Clone() { return new DefaultIfEmptyAsyncIterator(source, defaultValue); } public override async Task DisposeAsync() { if (enumerator != null) { await enumerator.DisposeAsync().ConfigureAwait(false); enumerator = null; } await base.DisposeAsync().ConfigureAwait(false); } protected override async Task MoveNextCore() { switch (state) { case AsyncIteratorState.Allocated: enumerator = source.GetAsyncEnumerator(); if (await enumerator.MoveNextAsync().ConfigureAwait(false)) { current = enumerator.Current; state = AsyncIteratorState.Iterating; } else { current = defaultValue; await enumerator.DisposeAsync().ConfigureAwait(false); enumerator = null; state = AsyncIteratorState.Disposed; } return true; case AsyncIteratorState.Iterating: if (await enumerator.MoveNextAsync().ConfigureAwait(false)) { current = enumerator.Current; return true; } break; } await DisposeAsync().ConfigureAwait(false); return false; } public async Task ToArrayAsync(CancellationToken cancellationToken) { var array = await source.ToArray(cancellationToken).ConfigureAwait(false); return array.Length == 0 ? new[] { defaultValue } : array; } public async Task> ToListAsync(CancellationToken cancellationToken) { var list = await source.ToList(cancellationToken).ConfigureAwait(false); if (list.Count == 0) { list.Add(defaultValue); } return list; } public async Task GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken) { int count; if (!onlyIfCheap || source is ICollection || source is ICollection) { count = await source.Count(cancellationToken).ConfigureAwait(false); } else { var listProv = source as IIListProvider; count = listProv == null ? -1 : await listProv.GetCountAsync(onlyIfCheap: true, cancellationToken: cancellationToken).ConfigureAwait(false); } return count == 0 ? 1 : count; } } } }