forked from RehanSaeed/Schema.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOneOrMany{T}.cs
More file actions
301 lines (267 loc) · 10.3 KB
/
OneOrMany{T}.cs
File metadata and controls
301 lines (267 loc) · 10.3 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
namespace Schema.NET;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.CompilerServices;
/// <summary>
/// A single or list of values.
/// </summary>
/// <typeparam name="T">The type of the values.</typeparam>
/// <seealso cref="IReadOnlyCollection{T}" />
[CollectionBuilder(typeof(OneOrManyBuilder), nameof(OneOrManyBuilder.Create))]
#pragma warning disable CA1710 // Identifiers should have correct suffix.
public readonly struct OneOrMany<T> : IReadOnlyCollection<T>, IValues, IEquatable<OneOrMany<T>>
#pragma warning restore CA1710 // Identifiers should have correct suffix.
{
private readonly T[]? collection;
/// <summary>
/// Initializes a new instance of the <see cref="OneOrMany{T}"/> struct.
/// </summary>
/// <param name="item">The single item value.</param>
public OneOrMany(T? item)
{
if (item is null || (item is string itemAsString && string.IsNullOrWhiteSpace(itemAsString)))
{
this.collection = null;
this.HasOne = false;
}
else
{
this.collection = [item];
this.HasOne = true;
}
}
/// <summary>
/// Initializes a new instance of the <see cref="OneOrMany{T}"/> struct.
/// </summary>
/// <param name="span">The span of values.</param>
public OneOrMany(params ReadOnlySpan<T?> span)
{
if (!span.IsEmpty)
{
var items = new T[span.Length];
var index = 0;
if (typeof(T) == typeof(string))
{
foreach (var item in span)
{
if (!string.IsNullOrWhiteSpace(item as string))
{
items[index] = item;
index++;
}
}
}
else
{
foreach (var item in span)
{
if (item is not null)
{
items[index] = item;
index++;
}
}
}
if (index > 0)
{
if (index == span.Length)
{
this.collection = items;
}
else
{
this.collection = new T[index];
Array.Copy(items, 0, this.collection, 0, index);
}
this.HasOne = index == 1;
return;
}
}
this.collection = null;
this.HasOne = false;
}
/// <summary>
/// Initializes a new instance of the <see cref="OneOrMany{T}"/> struct.
/// </summary>
/// <param name="collection">The collection of values.</param>
public OneOrMany(IEnumerable<T?> collection) : this(collection.ToArray().AsSpan()) { }
/// <summary>
/// Initializes a new instance of the <see cref="OneOrMany{T}"/> struct.
/// </summary>
/// <param name="collection">The list of values.</param>
public OneOrMany(IEnumerable<object?> collection) : this(collection.Cast<T?>().ToArray().AsSpan()) { }
/// <summary>
/// Gets the number of elements contained in the <see cref="OneOrMany{T}"/>.
/// </summary>
public int Count => this.collection?.Length ?? 0;
/// <summary>
/// Gets a value indicating whether this instance has a single item value.
/// </summary>
/// <value><c>true</c> if this instance has a single item value; otherwise, <c>false</c>.</value>
public bool HasOne { get; }
/// <summary>
/// Gets a value indicating whether this instance has more than one value.
/// </summary>
/// <value><c>true</c> if this instance has more than one value; otherwise, <c>false</c>.</value>
public bool HasMany => this.collection?.Length > 1;
/// <summary>
/// Performs an implicit conversion from <typeparamref name="T"/> to <see cref="OneOrMany{T}"/>.
/// </summary>
/// <param name="item">The single item value.</param>
/// <returns>The result of the conversion.</returns>
#pragma warning disable CA2225 // Operator overloads have named alternates
public static implicit operator OneOrMany<T>(T? item) => new(item);
/// <summary>
/// Performs an implicit conversion from <typeparamref name="T[]"/> to <see cref="OneOrMany{T}"/>.
/// </summary>
/// <param name="array">The array of values.</param>
/// <returns>The result of the conversion.</returns>
public static implicit operator OneOrMany<T>(T?[] array) => new(array);
/// <summary>
/// Performs an implicit conversion from <see cref="List{T}"/> to <see cref="OneOrMany{T}"/>.
/// </summary>
/// <param name="list">The list of values.</param>
/// <returns>The result of the conversion.</returns>
#pragma warning disable CA1002 // Do not expose generic lists
public static implicit operator OneOrMany<T>(List<T?> list) => new(list);
#pragma warning restore CA1002 // Do not expose generic lists
/// <summary>
/// Performs an implicit conversion from <see cref="OneOrMany{T}"/> to <typeparamref name="T"/>.
/// </summary>
/// <param name="oneOrMany">The values.</param>
/// <returns>
/// The result of the conversion.
/// </returns>
public static implicit operator T?(OneOrMany<T> oneOrMany) => oneOrMany.FirstOrDefault();
/// <summary>
/// Performs an implicit conversion from <see cref="OneOrMany{T}"/> to <typeparamref name="T[]"/>.
/// </summary>
/// <param name="oneOrMany">The values.</param>
/// <returns>
/// The result of the conversion.
/// </returns>
public static implicit operator T[](OneOrMany<T> oneOrMany) => oneOrMany.ToArray();
/// <summary>
/// Performs an implicit conversion from <see cref="OneOrMany{T}"/> to <see cref="List{T}"/>.
/// </summary>
/// <param name="oneOrMany">The values.</param>
/// <returns>
/// The result of the conversion.
/// </returns>
#pragma warning disable CA1002 // Do not expose generic lists
public static implicit operator List<T>(OneOrMany<T> oneOrMany) => oneOrMany.ToList();
#pragma warning restore CA1002 // Do not expose generic lists
#pragma warning restore CA2225 // Operator overloads have named alternates
/// <summary>
/// Implements the operator ==.
/// </summary>
/// <param name="left">The left.</param>
/// <param name="right">The right.</param>
/// <returns>
/// The result of the operator.
/// </returns>
public static bool operator ==(OneOrMany<T> left, OneOrMany<T> right) => left.Equals(right);
/// <summary>
/// Implements the operator !=.
/// </summary>
/// <param name="left">The left.</param>
/// <param name="right">The right.</param>
/// <returns>
/// The result of the operator.
/// </returns>
public static bool operator !=(OneOrMany<T> left, OneOrMany<T> right) => !(left == right);
/// <summary>
/// Returns an enumerator that iterates through the <see cref="OneOrMany{T}"/>.
/// </summary>
/// <returns>An enumerator for the <see cref="OneOrMany{T}"/>.</returns>
public IEnumerator<T> GetEnumerator()
{
if (this.collection is not null)
{
foreach (var item in this.collection)
{
yield return item;
}
}
}
/// <summary>
/// Returns an enumerator that iterates through the <see cref="OneOrMany{T}"/>.
/// </summary>
/// <returns>An enumerator for the <see cref="OneOrMany{T}"/>.</returns>
IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator();
/// <summary>
/// Creates an array from <see cref="OneOrMany{T}" />.
/// </summary>
/// <returns>An array containing all the elements.</returns>
public T[] ToArray()
{
if (this.HasOne)
{
return [this.collection![0]];
}
if (this.HasMany)
{
var result = new T[this.collection!.Length];
Array.Copy(this.collection, 0, result, 0, this.collection.Length);
return result;
}
return [];
}
/// <summary>
/// Indicates whether the current object is equal to another object of the same type.
/// </summary>
/// <param name="other">An object to compare with this object.</param>
/// <returns>
/// true if the current object is equal to the <paramref name="other" /> parameter; otherwise, false.
/// </returns>
public bool Equals(OneOrMany<T> other)
{
if (!this.HasOne && !other.HasOne && !this.HasMany && !other.HasMany)
{
return true;
}
if (this.HasOne && other.HasOne)
{
return Equals(this.collection![0], other.collection![0]);
}
if (this.HasMany && other.HasMany)
{
return this.AsSpan().SequenceEqual(other.AsSpan());
}
return false;
}
/// <summary>
/// Determines whether the specified <see cref="object" />, is equal to this instance.
/// </summary>
/// <param name="obj">The <see cref="object" /> to compare with this instance.</param>
/// <returns>
/// <c>true</c> if the specified <see cref="object" /> is equal to this instance; otherwise, <c>false</c>.
/// </returns>
public override bool Equals([NotNullWhen(true)] object? obj) => obj is OneOrMany<T> oneOrMany && this.Equals(oneOrMany);
/// <summary>
/// Returns a hash code for this instance.
/// </summary>
/// <returns>
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
/// </returns>
public override int GetHashCode() => HashCode.OfEach(this.collection);
/// <summary>
/// Returns a <see cref="ReadOnlySpan{T}"/> wrapping the current items.
/// </summary>
/// <returns>A <see cref="ReadOnlySpan{T}"/> wrapping the current items.</returns>
public ReadOnlySpan<T> AsSpan() => this.collection.AsSpan();
}
/// <summary>
/// Contains the generic static constructors needed to support collection expressions.
/// </summary>
public static class OneOrManyBuilder
{
/// <summary>
/// Initializes a new instance of the <see cref="OneOrMany{T}"/> struct.
/// </summary>
/// <param name="items">The span of values.</param>
public static OneOrMany<T> Create<T>(ReadOnlySpan<T> items) => new(items);
}