-
Notifications
You must be signed in to change notification settings - Fork 10
/
Linkable.cs
141 lines (117 loc) · 4.33 KB
/
Linkable.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
// 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 System.Diagnostics.CodeAnalysis;
using ActivityPub.Types.AS;
namespace ActivityPub.Types.Util;
/// <summary>
/// Synthetic wrapper for elements that can be included directly or referenced by a Link.
/// </summary>
/// <typeparam name="T">Type of element</typeparam>
public sealed class Linkable<T>
where T : ASType
{
/// <summary>
/// Creates a Linkable from a reference link
/// </summary>
public Linkable(ASLink link)
{
Link = link;
HasLink = true;
}
/// <summary>
/// Creates a linkable from a value object
/// </summary>
public Linkable(T value)
{
Value = value;
HasValue = true;
}
/// <summary>
/// Creates a linkable by cloning another linkable
/// </summary>
public Linkable(Linkable<T> linkable)
{
HasValue = linkable.HasValue;
Value = linkable.Value;
}
/// <summary>
/// <see langword="true"/> if this Linkable has a link reference
/// </summary>
[MemberNotNullWhen(true, nameof(Link))]
[MemberNotNullWhen(false, nameof(Value))]
public bool HasLink { get; }
/// <summary>
/// The link reference, or <see langword="null"/> if this linkable has an object.
/// </summary>
public ASLink? Link { get; }
/// <summary>
/// <see langword="true"/> if this Linkable has a value object
/// </summary>
[MemberNotNullWhen(true, nameof(Value))]
[MemberNotNullWhen(false, nameof(Link))]
public bool HasValue { get; }
/// <summary>
/// The value object, or <see langword="this"/> if this Linkable has a link reference
/// </summary>
public T? Value { get; }
/// <summary>
/// If this Linkable has a link, then returns <see langword="true"/> and assigns it to "link".
/// Otherwise, returns <see langword="false"/>.
/// </summary>
public bool TryGetLink([NotNullWhen(true)] out ASLink? link)
{
if (HasLink)
{
link = Link!;
return true;
}
link = null;
return false;
}
/// <summary>
/// If this Linkable has a value, then returns <see langword="true"/> and assigns it to "value".
/// Otherwise, returns <see langword="false"/>.
/// </summary>
public bool TryGetValue([NotNullWhen(true)] out T? value)
{
if (HasValue)
{
value = Value!;
return true;
}
value = default;
return false;
}
private bool Equals(Linkable<T> other) => Equals(Link, other.Link) && EqualityComparer<T?>.Default.Equals(Value, other.Value);
/// <inheritdoc />
public override bool Equals(object? obj)
{
if (ReferenceEquals(null, obj))
return false;
if (ReferenceEquals(this, obj))
return true;
if (obj.GetType() != GetType())
return false;
return Equals((Linkable<T>)obj);
}
/// <inheritdoc />
public override int GetHashCode() => HashCode.Combine(Link, Value);
/// <inheritdoc cref="Linkable{T}(ASLink)"/>
public static implicit operator Linkable<T>(ASLink link) => new(link);
/// <inheritdoc cref="Linkable{T}(ASLink)"/>
public static implicit operator Linkable<T>(ASUri link) => new((ASLink)link);
/// <inheritdoc cref="Linkable{T}(ASLink)"/>
public static implicit operator Linkable<T>(Uri link) => new((ASLink)link);
/// <inheritdoc cref="Linkable{T}(ASLink)"/>
public static implicit operator Linkable<T>(string link) => new((ASLink)link);
/// <inheritdoc cref="Linkable{T}(T)"/>
public static implicit operator Linkable<T>(T value) => new(value);
/// <summary>
/// Converts this <see cref="Linkable{T}"/> to its reference link, or <see langword="null"/> if it has a value object
/// </summary>
public static implicit operator ASLink?(Linkable<T>? linkable) => linkable?.Link;
/// <summary>
/// Converts this <see cref="Linkable{T}"/> to its value object, or <see langword="null"/> if it has a reference link
/// </summary>
public static implicit operator T?(Linkable<T>? linkable) => linkable?.Value;
}