-
Notifications
You must be signed in to change notification settings - Fork 10
/
LinkableList.cs
109 lines (87 loc) · 3.62 KB
/
LinkableList.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
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
// If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
using ActivityPub.Types.AS;
namespace ActivityPub.Types.Util;
/// <summary>
/// Synthetic type to represent a list of <code>T</code> or Links to <code>T</code>
/// </summary>
public class LinkableList<T> : List<Linkable<T>>
where T : ASType
{
/// <inheritdoc />
public LinkableList() {}
/// <inheritdoc />
public LinkableList(int capacity) : base(capacity) {}
/// <inheritdoc />
public LinkableList(IEnumerable<Linkable<T>> collection) : base(collection) {}
/// <inheritdoc cref="LinkableList{T}(IEnumerable{Linkable{T}})" />
public LinkableList(IEnumerable<T> values) => AddRange(values);
/// <inheritdoc cref="LinkableList{T}(IEnumerable{Linkable{T}})" />
public LinkableList(IEnumerable<ASLink> links) => AddRange(links);
/// <summary>
/// All link items within the collection.
/// </summary>
public IEnumerable<ASLink> LinkItems => this
.Where(linkable => linkable.HasLink)
.Select(linkable => linkable.Link!);
/// <summary>
/// All non-link items within the collection
/// </summary>
public IEnumerable<T> ValueItems => this
.Where(linkable => linkable.HasValue)
.Select(linkable => linkable.Value!);
/// <summary>
/// Adds multiple values to the list
/// </summary>
public void AddRange(IEnumerable<T> values)
{
foreach (var value in values)
Add(value);
}
/// <summary>
/// Adds multiple links to the list
/// </summary>
public void AddRange(IEnumerable<ASLink> links)
{
foreach (var link in links)
Add(link);
}
// These are required for weird type resolution reasons.
// If removed, other code wont compile.
/// <inheritdoc cref="List{T}.Add(T)"/>
public new void Add(Linkable<T> linkable) => base.Add(linkable);
/// <inheritdoc cref="List{T}.AddRange(IEnumerable{T})"/>
public new void AddRange(IEnumerable<Linkable<T>> linkables) => base.AddRange(linkables);
/// <summary>
/// Constructs a linkable list from a single link
/// </summary>
public static implicit operator LinkableList<T>(ASLink link) => [link];
/// <summary>
/// Constructs a linkable list from a single link URI
/// </summary>
public static implicit operator LinkableList<T>(ASUri link) => [(ASLink)link];
/// <summary>
/// Constructs a linkable list from a single link URI
/// </summary>
public static implicit operator LinkableList<T>(Uri link) => [(ASLink)link];
/// <summary>
/// Constructs a linkable list from a single link URI
/// </summary>
public static implicit operator LinkableList<T>(string link) => [(ASLink)link];
/// <summary>
/// Constructs a linkable list from a single value
/// </summary>
public static implicit operator LinkableList<T>(T value) => [value];
/// <summary>
/// Constructs a linkable list from a single linkable
/// </summary>
public static implicit operator LinkableList<T>(Linkable<T> linkable) => [linkable];
/// <summary>
/// Constructs a linkable list from a list of values
/// </summary>
public static implicit operator LinkableList<T>(List<T> values) => new(values);
/// <summary>
/// Constructs a linkable list from a list of links
/// </summary>
public static implicit operator LinkableList<T>(List<ASLink> values) => new(values);
}