-
-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathOneOrMany{T}.cs
329 lines (299 loc) · 12.6 KB
/
OneOrMany{T}.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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
namespace Schema.NET
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
/// <summary>
/// A single or list of values.
/// </summary>
/// <typeparam name="T">The type of the values.</typeparam>
/// <seealso cref="ICollection{T}" />
public readonly struct OneOrMany<T>
: IReadOnlyCollection<T>, IEnumerable<T>, IValues, IEquatable<OneOrMany<T>>
{
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)
{
#pragma warning disable CA1508 // TODO: Remove this suppression in .NET 6 where the warning is fixed.
if (item is null || (item is string itemAsString && string.IsNullOrWhiteSpace(itemAsString)))
#pragma warning restore CA1508 // TODO: Remove this suppression in .NET 6 where the warning is fixed.
{
this.collection = null;
this.HasOne = false;
}
else
{
this.collection = new[] { 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(ReadOnlySpan<T> span)
{
if (!span.IsEmpty)
{
var items = new T[span.Length];
var index = 0;
if (typeof(T) == typeof(string))
{
for (var i = 0; i < span.Length; i++)
{
var item = span[i];
#pragma warning disable CA1508 // TODO: Remove this suppression in .NET 6 where the warning is fixed.
if (!string.IsNullOrWhiteSpace(item as string))
#pragma warning restore CA1508 // TODO: Remove this suppression in .NET 6 where the warning is fixed.
{
items[index] = item;
index++;
}
}
}
else
{
for (var i = 0; i < span.Length; i++)
{
var item = span[i];
#pragma warning disable CA1508 // TODO: Remove this suppression in .NET 6 where the warning is fixed.
if (item is not null)
#pragma warning restore CA1508 // TODO: Remove this suppression in .NET 6 where the warning is fixed.
{
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="array">The array of values.</param>
public OneOrMany(params T[] array)
: this(array.AsSpan())
{
}
/// <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);
#pragma warning restore CA2225 // Operator overloads have named alternates
/// <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>
#pragma warning disable CA2225 // Operator overloads have named alternates
public static implicit operator OneOrMany<T>(T[] array) => new(array);
#pragma warning restore CA2225 // Operator overloads have named alternates
/// <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 CA2225 // Operator overloads have named alternates
#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
#pragma warning restore CA2225 // Operator overloads have named alternates
/// <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>
#pragma warning disable CA2225 // Operator overloads have named alternates
public static implicit operator T?(OneOrMany<T> oneOrMany) => oneOrMany.FirstOrDefault();
#pragma warning restore CA2225 // Operator overloads have named alternates
/// <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>
#pragma warning disable CA2225 // Operator overloads have named alternates
public static implicit operator T[](OneOrMany<T> oneOrMany) => oneOrMany.ToArray();
#pragma warning restore CA2225 // Operator overloads have named alternates
/// <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 CA2225 // Operator overloads have named alternates
#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)
{
for (var i = 0; i < this.collection.Length; i++)
{
yield return this.collection[i];
}
}
}
/// <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 new[] { this.collection![0] };
}
else if (this.HasMany)
{
var result = new T[this.collection!.Length];
Array.Copy(this.collection, 0, result, 0, this.collection.Length);
return result;
}
else
{
#if NETSTANDARD1_1
return new T[0];
#else
return Array.Empty<T>();
#endif
}
}
/// <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;
}
else if (this.HasOne && other.HasOne)
{
return Equals(this.collection![0], other.collection![0]);
}
else if (this.HasMany && other.HasMany)
{
if (this.collection!.Length != other.collection!.Length)
{
return false;
}
for (var i = 0; i < this.collection.Length; i++)
{
if (!EqualityComparer<T>.Default.Equals(this.collection[i], other.collection[i]))
{
return false;
}
}
return true;
}
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(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);
}
}