-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathReferenceDataCodeCollection.cs
More file actions
80 lines (61 loc) · 3.13 KB
/
Copy pathReferenceDataCodeCollection.cs
File metadata and controls
80 lines (61 loc) · 3.13 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
namespace CoreEx.RefData;
/// <summary>
/// Provides a special purpose <see cref="IReferenceData"/> collection specifically for managing a referenced list of <i>serialization identifiers</i>, being the underlying <see cref="IReferenceData.Code"/>.
/// </summary>
public class ReferenceDataCodeCollection<TRef> : IReferenceDataCodeCollection, ICollection<TRef> where TRef : IReferenceData, new()
{
private readonly List<string?> _codes;
/// <summary>
/// Initializes a new instance of the <see cref="ReferenceDataCodeCollection{TRef}"/> class.
/// </summary>
public ReferenceDataCodeCollection() => _codes = [];
/// <summary>
/// Initializes a new instance of the <see cref="ReferenceDataCodeCollection{TRef}"/> class with a reference to an external <see cref="IReferenceData.Code"/> list.
/// </summary>
/// <param name="codes">A reference to the external <see cref="IReferenceData.Code"/> list; it is this list that will be maintained by this collection.</param>
public ReferenceDataCodeCollection(ref List<string?>? codes) => _codes = codes ?? [];
/// <summary>
/// Initializes a new instance of the <see cref="ReferenceDataCodeCollection{TRef}"/> class with a list of items.
/// </summary>
/// <param name="items">The list of <see cref="IReferenceData"/> items.</param>
public ReferenceDataCodeCollection(IEnumerable<TRef> items) => _codes = [.. (items ?? []).Select(x => x.Code)];
/// <summary>
/// Initializes a new instance of the <see cref="ReferenceDataCodeCollection{TRef}"/> class with a <see cref="IReferenceData.Code"/> array.
/// </summary>
/// <param name="codes">The <see cref="IReferenceData.Code"/> array.</param>
public ReferenceDataCodeCollection(params IEnumerable<string?> codes) => _codes = [.. codes];
/// <inheritdoc/>
public bool HasInvalidItems => this.Any(x => x is null || !x.IsValid);
/// <inheritdoc/>
public bool HasInactiveItems => this.Any(x => x is not null && x.IsValid && !x.IsActive);
/// <inheritdoc/>
public int Count => _codes.Count;
/// <inheritdoc/>
public List<string?> ToCodeList() => [.. _codes];
/// <inheritdoc/>
public IEnumerable<IReferenceData> ToRefDataList() => [.. this];
/// <inheritdoc/>
public bool IsReadOnly => ((IList)_codes).IsReadOnly;
/// <inheritdoc/>
public void Add(TRef item) => _codes.Add(item?.Code);
/// <inheritdoc/>
public void Clear() => _codes.Clear();
/// <inheritdoc/>
public bool Contains(TRef item) => ((IList)_codes).Contains(item);
/// <inheritdoc/>
public void CopyTo(TRef[] array, int arrayIndex) => ((IList)_codes).CopyTo(array, arrayIndex);
/// <inheritdoc/>
public IEnumerator<TRef> GetEnumerator()
{
foreach (string? code in _codes)
{
yield return ReferenceDataOrchestrator.TryGetByCode<TRef>(code, out var item) ? item : item;
}
}
/// <inheritdoc/>
public int IndexOf(TRef item) => _codes.IndexOf(item?.Code);
/// <inheritdoc/>
public bool Remove(TRef item) => _codes.Remove(item?.Code);
/// <inheritdoc/>
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}