-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQuote.cs
224 lines (197 loc) · 6.63 KB
/
Quote.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
using System;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
namespace JollyQuotes
{
/// <summary>
/// Contains a quote, its author, source, date and related tags.
/// </summary>
/// <remarks>This class implements the <see cref="IEquatable{T}"/> interface - two instances are compare by their value, not reference.</remarks>
[JsonObject]
public record Quote : IQuote
{
/// <summary>
/// <see cref="JsonConverter"/> that formats <see cref="DateTime"/> and <see cref="DateTimeOffset"/> values to use only the <c>date</c> part.
/// </summary>
public sealed class DateOnlyConverter : IsoDateTimeConverter
{
/// <summary>
/// Initializes a new instance of the <see cref="DateOnlyConverter"/> class.
/// </summary>
public DateOnlyConverter()
{
DateTimeFormat = Quote.DateTimeFormat;
}
}
private readonly string _value;
private readonly string _author;
private readonly string _source;
private readonly string[] _tags;
/// <summary>
/// Format of date time values used by <c>JollyQuotes</c> libraries.
/// </summary>
public const string DateTimeFormat = "yyyy-MM-dd";
/// <summary>
/// <see cref="JsonSerializerSettings"/> used to internally deserialize json objects.
/// </summary>
public static JsonSerializerSettings JsonSettings { get; } = new()
{
DateFormatString = DateTimeFormat
};
/// <summary>
/// Value used when an author of a quote is unknown.
/// </summary>
public const string UnknownAuthor = "Unknown";
/// <summary>
/// Value of <see cref="Value"/> used in <see cref="Unknown"/>.
/// </summary>
public const string NoContent = "No Content";
/// <summary>
/// Returns a new instance of the <see cref="Quote"/> class representing an unknown quote (can be used e.g. when quote with tag was not found).
/// </summary>
public static Quote Unknown => new(NoContent, UnknownAuthor);
Id IQuote.Id => GetId();
/// <inheritdoc/>
/// <exception cref="ArgumentNullException">Value is <see langword="null"/> or empty.</exception>
[JsonProperty("author", Order = 1, Required = Required.Always)]
public string Author
{
get => _author;
init
{
if (string.IsNullOrWhiteSpace(value))
{
throw Error.NullOrEmpty(nameof(value));
}
_author = value;
}
}
/// <inheritdoc/>
[JsonProperty("date", Order = 2, Required = Required.DisallowNull)]
[JsonConverter(typeof(DateOnlyConverter))]
public DateTime? Date { get; init; }
/// <inheritdoc/>
/// <exception cref="ArgumentNullException">Value is <see langword="null"/>.</exception>
[JsonProperty("source", Order = 3, Required = Required.DisallowNull)]
public string Source
{
get => _source;
init
{
if (value is null)
{
throw Error.Null(nameof(value));
}
_source = value;
}
}
/// <summary>
/// Tags associated with the quote.
/// </summary>
/// <exception cref="ArgumentNullException">Value is <see langword="null"/>.</exception>
[JsonProperty("tags", Order = 4, Required = Required.DisallowNull)]
public string[] Tags
{
get => _tags;
init
{
if (value is null)
{
throw Error.Null(nameof(value));
}
_tags = value;
}
}
/// <inheritdoc/>
/// <exception cref="ArgumentException">Value is <see langword="null"/> or empty.</exception>
[JsonProperty("quote", Order = 0, Required = Required.Always)]
public string Value
{
get => _value;
init
{
if (string.IsNullOrWhiteSpace(value))
{
throw Error.NullOrEmpty(nameof(value));
}
_value = value;
}
}
/// <summary>
/// Initializes a new instance of the <see cref="Quote"/> class with a <paramref name="quote"/> and an <paramref name="author"/> specified.
/// </summary>
/// <param name="quote">Actual quote.</param>
/// <param name="author">Author of the quote.</param>
/// <exception cref="ArgumentException"><paramref name="quote"/> is <see langword="null"/> or empty. -or- <paramref name="author"/> is <see langword="null"/> or empty.</exception>
public Quote(string quote, string author) : this(quote, author, null, default)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="Quote"/> class with a <paramref name="quote"/>, <paramref name="author"/> and a <paramref name="source"/> specified.
/// </summary>
/// <param name="quote">Actual quote.</param>
/// <param name="author">Author of the quote.</param>
/// <param name="source">Source of the quote, e.g. a link, file name or raw text.</param>
/// <exception cref="ArgumentException"><paramref name="quote"/> is <see langword="null"/> or empty. -or- <paramref name="author"/> is <see langword="null"/> or empty.</exception>
public Quote(string quote, string author, string? source) : this(quote, author, source, default)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="Quote"/> class with a <paramref name="quote"/>, <paramref name="author"/>, <paramref name="source"/>, <paramref name="date"/> and related <paramref name="tags"/> specified.
/// </summary>
/// <param name="quote">Actual quote.</param>
/// <param name="author">Author of the quote.</param>
/// <param name="source">Source of the quote, e.g. a link, file name or raw text.</param>
/// <param name="date">Date at which the quote was said/written.</param>
/// <param name="tags">Tags associated with the quote.</param>
/// <exception cref="ArgumentException"><paramref name="quote"/> is <see langword="null"/> or empty. -or- <paramref name="author"/> is <see langword="null"/> or empty.</exception>
[JsonConstructor]
public Quote(string quote, string author, string? source, DateTime? date, params string[]? tags)
{
if (string.IsNullOrWhiteSpace(quote))
{
throw Error.NullOrEmpty(nameof(quote));
}
if (string.IsNullOrWhiteSpace(author))
{
throw Error.NullOrEmpty(nameof(author));
}
_value = quote;
_author = author;
_source = source ?? string.Empty;
Date = date;
_tags = tags ?? Array.Empty<string>();
}
/// <inheritdoc cref="IEquatable{T}.Equals(T)"/>
public virtual bool Equals(Quote? other)
{
return
other is not null &&
other._value == _value &&
other._author == _author &&
other._source == _source &&
other.Date == Date &&
other._tags.Length == _tags.Length &&
other._tags.SequenceEqual(_tags);
}
/// <inheritdoc/>
public override int GetHashCode()
{
HashCode hash = new();
hash.Add(_author);
hash.Add(_value);
hash.Add(_source);
hash.Add(Date);
hash.AddSequence(_tags);
return hash.ToHashCode();
}
/// <summary>
/// Returns an unique id of the quote.
/// </summary>
protected virtual Id GetId()
{
return new Id(_value);
}
}
}