-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
FallbackIfEmpty.cs
113 lines (105 loc) · 3.77 KB
/
FallbackIfEmpty.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
namespace SuperLinq;
public static partial class SuperEnumerable
{
/// <summary>
/// Returns the elements of a sequence, but if it is empty then returns an alternate sequence from an array of
/// values.
/// </summary>
/// <typeparam name="T">
/// The type of the elements in the sequences.
/// </typeparam>
/// <param name="source">
/// The source sequence.
/// </param>
/// <param name="fallback">
/// The array that is returned as the alternate sequence if <paramref name="source"/> is empty.
/// </param>
/// <returns>
/// An <see cref="IEnumerable{T}"/> that containing fallback values if <paramref name="source"/> is empty;
/// otherwise, <paramref name="source"/>.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="source"/> or <paramref name="fallback"/> is <see langword="null"/>.
/// </exception>
/// <remarks>
/// <para>
/// The length of <paramref name="source"/> is not evaluated until enumeration. <paramref name="source"/> is
/// enumerated; if there is at least one item, the elements of <paramref name="source"/> will be streamed in a
/// deferred manner. If there are no items in <paramref name="source"/>, the items in <paramref name="fallback"/>
/// will be streamed.
/// </para>
/// </remarks>
public static IEnumerable<T> FallbackIfEmpty<T>(this IEnumerable<T> source, params T[] fallback)
{
return source.FallbackIfEmpty((IEnumerable<T>)fallback);
}
/// <summary>
/// Returns the elements of a sequence, but if it is empty then returns an alternate sequence of values.
/// </summary>
/// <typeparam name="T">
/// The type of the elements in the sequences.
/// </typeparam>
/// <param name="source">
/// The source sequence.
/// </param>
/// <param name="fallback">
/// The alternate sequence that is returned if <paramref name="source"/> is empty.
/// </param>
/// <returns>
/// An <see cref="IEnumerable{T}"/> that containing fallback values if <paramref name="source"/> is empty;
/// otherwise, <paramref name="source"/>.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="source"/> or <paramref name="fallback"/> is <see langword="null"/>.
/// </exception>
/// <remarks>
/// <para>
/// The length of <paramref name="source"/> is not evaluated until enumeration. <paramref name="source"/> is
/// enumerated; if there is at least one item, the elements of <paramref name="source"/> will be streamed using
/// deferred execution. If there are no items in <paramref name="source"/>, then <paramref name="fallback"/>
/// will be streamed using deferred execution.
/// </para>
/// </remarks>
public static IEnumerable<T> FallbackIfEmpty<T>(this IEnumerable<T> source, IEnumerable<T> fallback)
{
ArgumentNullException.ThrowIfNull(source);
ArgumentNullException.ThrowIfNull(fallback);
return source.TryGetCollectionCount() is not null
&& fallback.TryGetCollectionCount() is not null
? new FallbackIfEmptyCollectionIterator<T>(source, fallback)
: Core(source, fallback);
static IEnumerable<T> Core(IEnumerable<T> source, IEnumerable<T> fallback)
{
using (var e = source.GetEnumerator())
{
if (e.MoveNext())
{
do
{
yield return e.Current;
}
while (e.MoveNext());
yield break;
}
}
foreach (var item in fallback)
yield return item;
}
}
private sealed class FallbackIfEmptyCollectionIterator<T>(
IEnumerable<T> source,
IEnumerable<T> fallback
) : CollectionIterator<T>
{
public override int Count =>
source.GetCollectionCount() == 0
? fallback.Count()
: source.GetCollectionCount();
protected override IEnumerable<T> GetEnumerable()
{
return source.GetCollectionCount() == 0
? fallback
: source;
}
}
}