-
Notifications
You must be signed in to change notification settings - Fork 1
/
Checkable.cs
378 lines (336 loc) · 23.1 KB
/
Checkable.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
using System;
using System.Collections.Generic;
using System.Linq;
namespace Green
{
/// <summary>
/// Defines operators available to <see langword="bool"/>-valued queries
/// </summary>
public static partial class Checkable
{
/// <summary>
/// Checks if the target is <see langword="true"/>
/// </summary>
/// <param name="check">The <see langword="bool"/>-valued query being continued</param>
/// <returns>A continuation of <paramref name="check"/> applying this operator. Implicitly converts to <see langword="bool"/>.</returns>
public static Check<bool> IsTrue(this Check<bool> check) =>
check.That(t => t);
/// <summary>
/// Checks if the target is <see langword="false"/>
/// </summary>
/// <param name="check">The <see langword="bool"/>-valued query being continued</param>
/// <returns>A continuation of <paramref name="check"/> applying this operator. Implicitly converts to <see langword="bool"/>.</returns>
public static Check<bool> IsFalse(this Check<bool> check) =>
check.Not(t => t);
/// <summary>
/// Checks if the target is not <see langword="null"/> and results in <see langword="true"/> from <paramref name="checkValue"/>
/// </summary>
/// <typeparam name="T">The type of target value</typeparam>
/// <param name="check">The <see langword="bool"/>-valued query being continued</param>
/// <param name="checkValue">The function that checks the value if present</param>
/// <returns>A continuation of <paramref name="check"/> applying this operator. Implicitly converts to <see langword="bool"/>.</returns>
public static Check<T?> HasValue<T>(this Check<T?> check, Func<Check<T>, bool> checkValue) where T : struct =>
check.That(t => t != null && checkValue != null && checkValue(Check.That(t.Value)));
//
// Null
//
/// <summary>
/// Checks if the target is <see langword="null"/>
/// </summary>
/// <typeparam name="T">The type of target value</typeparam>
/// <param name="check">The <see langword="bool"/>-valued query being continued</param>
/// <returns>A continuation of <paramref name="check"/> applying this operator. Implicitly converts to <see langword="bool"/>.</returns>
public static Check<T> IsNull<T>(this Check<T> check) where T : class =>
check.That(t => t == null);
/// <summary>
/// Checks if the target is <see langword="null"/>
/// </summary>
/// <typeparam name="T">The type of target value</typeparam>
/// <param name="check">The <see langword="bool"/>-valued query being continued</param>
/// <returns>A continuation of <paramref name="check"/> applying this operator. Implicitly converts to <see langword="bool"/>.</returns>
public static Check<T?> IsNull<T>(this Check<T?> check) where T : struct =>
check.That(t => t == null);
/// <summary>
/// Checks if the target is not <see langword="null"/>
/// </summary>
/// <typeparam name="T">The type of target value</typeparam>
/// <param name="check">The <see langword="bool"/>-valued query being continued</param>
/// <returns>A continuation of <paramref name="check"/> applying this operator. Implicitly converts to <see langword="bool"/>.</returns>
public static Check<T> IsNotNull<T>(this Check<T> check) where T : class =>
check.Not(t => t == null);
/// <summary>
/// Checks if the target is not <see langword="null"/>
/// </summary>
/// <typeparam name="T">The type of target value</typeparam>
/// <param name="check">The <see langword="bool"/>-valued query being continued</param>
/// <returns>A continuation of <paramref name="check"/> applying this operator. Implicitly converts to <see langword="bool"/>.</returns>
public static Check<T?> IsNotNull<T>(this Check<T?> check) where T : struct =>
check.Not(t => t == null);
//
// Comparisons
//
/// <summary>
/// Checks if the target equals <paramref name="value"/> using <paramref name="comparer"/>
/// </summary>
/// <typeparam name="T">The type of target value</typeparam>
/// <param name="check">The <see langword="bool"/>-valued query being continued</param>
/// <param name="value">The value to compare</param>
/// <param name="comparer">The object that performs the comparison</param>
/// <returns>A continuation of <paramref name="check"/> applying this operator. Implicitly converts to <see langword="bool"/>.</returns>
public static Check<T> Is<T>(this Check<T> check, T value, IEqualityComparer<T> comparer) where T : IEquatable<T> =>
check.That(t => Equals(t, value, comparer));
/// <summary>
/// Checks if the target does not equal <paramref name="value"/> using <paramref name="comparer"/>
/// </summary>
/// <typeparam name="T">The type of target value</typeparam>
/// <param name="check">The <see langword="bool"/>-valued query being continued</param>
/// <param name="value">The value to compare</param>
/// <param name="comparer">The object that performs the comparison</param>
/// <returns>A continuation of <paramref name="check"/> applying this operator. Implicitly converts to <see langword="bool"/>.</returns>
public static Check<T> IsNot<T>(this Check<T> check, T value, IEqualityComparer<T> comparer) where T : IEquatable<T> =>
check.Not(t => Equals(t, value, comparer));
/// <summary>
/// Checks if the target is less than <paramref name="value"/> using <paramref name="comparer"/>
/// </summary>
/// <typeparam name="T">The type of target value</typeparam>
/// <param name="check">The <see langword="bool"/>-valued query being continued</param>
/// <param name="value">The value to compare</param>
/// <param name="comparer">The object that performs the comparison</param>
/// <returns>A continuation of <paramref name="check"/> applying this operator. Implicitly converts to <see langword="bool"/>.</returns>
public static Check<T> IsLessThan<T>(this Check<T> check, T value, IComparer<T> comparer) where T : IComparable<T> =>
check.That(t => Compare(t, value, comparer) < 0);
/// <summary>
/// Checks if the target is greater than <paramref name="value"/> using <paramref name="comparer"/>
/// </summary>
/// <typeparam name="T">The type of target value</typeparam>
/// <param name="check">The <see langword="bool"/>-valued query being continued</param>
/// <param name="value">The value to compare</param>
/// <param name="comparer">The object that performs the comparison</param>
/// <returns>A continuation of <paramref name="check"/> applying this operator. Implicitly converts to <see langword="bool"/>.</returns>
public static Check<T> IsGreaterThan<T>(this Check<T> check, T value, IComparer<T> comparer) where T : IComparable<T> =>
check.That(t => Compare(t, value, comparer) > 0);
/// <summary>
/// Checks if the target is at least <paramref name="minimum"/> using <paramref name="comparer"/>
/// </summary>
/// <typeparam name="T">The type of target value</typeparam>
/// <param name="check">The <see langword="bool"/>-valued query being continued</param>
/// <param name="minimum">The value to compare</param>
/// <param name="comparer">The object that performs the comparison</param>
/// <returns>A continuation of <paramref name="check"/> applying this operator. Implicitly converts to <see langword="bool"/>.</returns>
public static Check<T> IsAtLeast<T>(this Check<T> check, T minimum, IComparer<T> comparer) where T : IComparable<T> =>
check.That(t => Compare(t, minimum, comparer) >= 0);
/// <summary>
/// Checks if the target is at most <paramref name="maximum"/> using <paramref name="comparer"/>
/// </summary>
/// <typeparam name="T">The type of target value</typeparam>
/// <param name="check">The <see langword="bool"/>-valued query being continued</param>
/// <param name="maximum">The value to compare</param>
/// <param name="comparer">The object that performs the comparison</param>
/// <returns>A continuation of <paramref name="check"/> applying this operator. Implicitly converts to <see langword="bool"/>.</returns>
public static Check<T> IsAtMost<T>(this Check<T> check, T maximum, IComparer<T> comparer) where T : IComparable<T> =>
check.That(t => Compare(t, maximum, comparer) <= 0);
/// <summary>
/// Checks if the target is at least <paramref name="minimum"/> and at most <paramref name="maximum"/> using <paramref name="comparer"/>
/// </summary>
/// <typeparam name="T">The type of target value</typeparam>
/// <param name="check">The <see langword="bool"/>-valued query being continued</param>
/// <param name="minimum">The minimum value to compare using <paramref name="comparer"/></param>
/// <param name="maximum">The maximum value to compare using <paramref name="comparer"/></param>
/// <param name="comparer">The object that performs the comparisons</param>
/// <returns>A continuation of <paramref name="check"/> applying this operator. Implicitly converts to <see langword="bool"/>.</returns>
public static Check<T> IsInRange<T>(this Check<T> check, T minimum, T maximum, IComparer<T> comparer) where T : IComparable<T> =>
check.That(t => Compare(t, minimum, comparer) >= 0 && Compare(t, maximum, comparer) <= 0);
static bool Equals<T>(T target, T other, IEqualityComparer<T> comparer) =>
(comparer ?? EqualityComparer<T>.Default).Equals(target, other);
static int Compare<T>(T target, T other, IComparer<T> comparer) =>
(comparer ?? Comparer<T>.Default).Compare(target, other);
//
// Comparisons (default comparer)
//
/// <summary>
/// Checks if the target equals <paramref name="value"/> using <see cref="EqualityComparer{T}.Default"/>
/// </summary>
/// <typeparam name="T">The type of target value</typeparam>
/// <param name="check">The <see langword="bool"/>-valued query being continued</param>
/// <param name="value">The value to compare using <see cref="EqualityComparer{T}.Default"/></param>
/// <returns>A continuation of <paramref name="check"/> applying this operator. Implicitly converts to <see langword="bool"/>.</returns>
public static Check<T> Is<T>(this Check<T> check, T value) where T : IEquatable<T> =>
check.Is(value, EqualityComparer<T>.Default);
/// <summary>
/// Checks if the target does not equal <paramref name="value"/> using <see cref="EqualityComparer{T}.Default"/>
/// </summary>
/// <typeparam name="T">The type of target value</typeparam>
/// <param name="check">The <see langword="bool"/>-valued query being continued</param>
/// <param name="value">The value to compare using <see cref="EqualityComparer{T}.Default"/></param>
/// <returns>A continuation of <paramref name="check"/> applying this operator. Implicitly converts to <see langword="bool"/>.</returns>
public static Check<T> IsNot<T>(this Check<T> check, T value) where T : IEquatable<T> =>
check.IsNot(value, EqualityComparer<T>.Default);
/// <summary>
/// Checks if the target is less than <paramref name="value"/> using <see cref="EqualityComparer{T}.Default"/>
/// </summary>
/// <typeparam name="T">The type of target value</typeparam>
/// <param name="check">The <see langword="bool"/>-valued query being continued</param>
/// <param name="value">The value to compare using <see cref="EqualityComparer{T}.Default"/></param>
/// <returns>A continuation of <paramref name="check"/> applying this operator. Implicitly converts to <see langword="bool"/>.</returns>
public static Check<T> IsLessThan<T>(this Check<T> check, T value) where T : IComparable<T> =>
check.IsLessThan(value, Comparer<T>.Default);
/// <summary>
/// Checks if the target is greater than <paramref name="value"/> using <see cref="EqualityComparer{T}.Default"/>
/// </summary>
/// <typeparam name="T">The type of target value</typeparam>
/// <param name="check">The <see langword="bool"/>-valued query being continued</param>
/// <param name="value">The value to compare using <see cref="EqualityComparer{T}.Default"/></param>
/// <returns>A continuation of <paramref name="check"/> applying this operator. Implicitly converts to <see langword="bool"/>.</returns>
public static Check<T> IsGreaterThan<T>(this Check<T> check, T value) where T : IComparable<T> =>
check.IsGreaterThan(value, Comparer<T>.Default);
/// <summary>
/// Checks if the target is at least <paramref name="minimum"/> using <see cref="EqualityComparer{T}.Default"/>
/// </summary>
/// <typeparam name="T">The type of target value</typeparam>
/// <param name="check">The <see langword="bool"/>-valued query being continued</param>
/// <param name="minimum">The value to compare using <see cref="EqualityComparer{T}.Default"/></param>
/// <returns>A continuation of <paramref name="check"/> applying this operator. Implicitly converts to <see langword="bool"/>.</returns>
public static Check<T> IsAtLeast<T>(this Check<T> check, T minimum) where T : IComparable<T> =>
check.IsAtLeast(minimum, Comparer<T>.Default);
/// <summary>
/// Checks if the target is at most <paramref name="maximum"/> using <see cref="EqualityComparer{T}.Default"/>
/// </summary>
/// <typeparam name="T">The type of target value</typeparam>
/// <param name="check">The <see langword="bool"/>-valued query being continued</param>
/// <param name="maximum">The value to compare using <see cref="EqualityComparer{T}.Default"/></param>
/// <returns>A continuation of <paramref name="check"/> applying this operator. Implicitly converts to <see langword="bool"/>.</returns>
public static Check<T> IsAtMost<T>(this Check<T> check, T maximum) where T : IComparable<T> =>
check.IsAtMost(maximum, Comparer<T>.Default);
/// <summary>
/// Checks if the target is at least <paramref name="minimum"/> and at most <paramref name="maximum"/> using <see cref="EqualityComparer{T}.Default"/>
/// </summary>
/// <typeparam name="T">The type of target value</typeparam>
/// <param name="check">The <see langword="bool"/>-valued query being continued</param>
/// <param name="minimum">The minimum value to compare using <see cref="EqualityComparer{T}.Default"/></param>
/// <param name="maximum">The maximum value to compare using <see cref="EqualityComparer{T}.Default"/></param>
/// <returns>A continuation of <paramref name="check"/> applying this operator. Implicitly converts to <see langword="bool"/>.</returns>
public static Check<T> IsInRange<T>(this Check<T> check, T minimum, T maximum) where T : IComparable<T> =>
check.IsInRange(minimum, maximum, Comparer<T>.Default);
//
// In
//
/// <summary>
/// Checks if <paramref name="values"/> contains the target using <paramref name="comparer"/>
/// </summary>
/// <typeparam name="T">The type of target value</typeparam>
/// <param name="check">The <see langword="bool"/>-valued query being continued</param>
/// <param name="values">The values to compare using <paramref name="comparer"/></param>
/// <param name="comparer">The object that performs the comparisons</param>
/// <returns>A continuation of <paramref name="check"/> applying this operator. Implicitly converts to <see langword="bool"/>.</returns>
public static Check<T> IsIn<T>(this Check<T> check, IEnumerable<T> values, IEqualityComparer<T> comparer) =>
check.That(t => values != null && values.Contains(t, comparer));
/// <summary>
/// Checks if <paramref name="values"/> contains the target using <see cref="EqualityComparer{T}.Default"/>
/// </summary>
/// <typeparam name="T">The type of target value</typeparam>
/// <param name="check">The <see langword="bool"/>-valued query being continued</param>
/// <param name="values">The values to compare using <see cref="EqualityComparer{T}.Default"/></param>
/// <returns>A continuation of <paramref name="check"/> applying this operator. Implicitly converts to <see langword="bool"/>.</returns>
public static Check<T> IsIn<T>(this Check<T> check, IEnumerable<T> values) =>
check.IsIn(values, EqualityComparer<T>.Default);
/// <summary>
/// Checks if <paramref name="values"/> contains the target using <see cref="EqualityComparer{T}.Default"/>
/// </summary>
/// <typeparam name="T">The type of target value</typeparam>
/// <param name="check">The <see langword="bool"/>-valued query being continued</param>
/// <param name="values">The values to compare using <see cref="EqualityComparer{T}.Default"/></param>
/// <returns>A continuation of <paramref name="check"/> applying this operator. Implicitly converts to <see langword="bool"/>.</returns>
public static Check<T> IsIn<T>(this Check<T> check, params T[] values) =>
check.IsIn(values, EqualityComparer<T>.Default);
/// <summary>
/// Checks if <paramref name="values"/> contains the target using <paramref name="comparer"/>
/// </summary>
/// <typeparam name="T">The type of target value</typeparam>
/// <param name="check">The <see langword="bool"/>-valued query being continued</param>
/// <param name="comparer">The object that performs the comparisons</param>
/// <param name="values">The values to compare using <paramref name="comparer"/></param>
/// <returns>A continuation of <paramref name="check"/> applying this operator. Implicitly converts to <see langword="bool"/>.</returns>
public static Check<T> IsIn<T>(this Check<T> check, IEqualityComparer<T> comparer, params T[] values) =>
check.IsIn(values, comparer);
//
// Not in
//
/// <summary>
/// Checks if <paramref name="values"/> does not contain the target using <paramref name="comparer"/>
/// </summary>
/// <typeparam name="T">The type of target value</typeparam>
/// <param name="check">The <see langword="bool"/>-valued query being continued</param>
/// <param name="values">The values to compare using <paramref name="comparer"/></param>
/// <param name="comparer">The object that performs the comparisons</param>
/// <returns>A continuation of <paramref name="check"/> applying this operator. Implicitly converts to <see langword="bool"/>.</returns>
public static Check<T> IsNotIn<T>(this Check<T> check, IEnumerable<T> values, IEqualityComparer<T> comparer) =>
check.Not(t => values != null && values.Contains(t, comparer));
/// <summary>
/// Checks if <paramref name="values"/> does not contain the target using <see cref="EqualityComparer{T}.Default"/>
/// </summary>
/// <typeparam name="T">The type of target value</typeparam>
/// <param name="check">The <see langword="bool"/>-valued query being continued</param>
/// <param name="values">The values to compare using <see cref="EqualityComparer{T}.Default"/></param>
/// <returns>A continuation of <paramref name="check"/> applying this operator. Implicitly converts to <see langword="bool"/>.</returns>
public static Check<T> IsNotIn<T>(this Check<T> check, IEnumerable<T> values) =>
check.IsNotIn(values, EqualityComparer<T>.Default);
/// <summary>
/// Checks if <paramref name="values"/> does not contain the target using <paramref name="comparer"/>
/// </summary>
/// <typeparam name="T">The type of target value</typeparam>
/// <param name="check">The <see langword="bool"/>-valued query being continued</param>
/// <param name="comparer">The object that performs the comparisons</param>
/// <param name="values">The values to compare using <paramref name="comparer"/></param>
/// <returns>A continuation of <paramref name="check"/> applying this operator. Implicitly converts to <see langword="bool"/>.</returns>
public static Check<T> IsNotIn<T>(this Check<T> check, IEqualityComparer<T> comparer, params T[] values) =>
check.IsNotIn(values, comparer);
/// <summary>
/// Checks if <paramref name="values"/> does not contain the target using <see cref="EqualityComparer{T}.Default"/>
/// </summary>
/// <typeparam name="T">The type of target value</typeparam>
/// <param name="check">The <see langword="bool"/>-valued query being continued</param>
/// <param name="values">The values to compare using <see cref="EqualityComparer{T}.Default"/></param>
/// <returns>A continuation of <paramref name="check"/> applying this operator. Implicitly converts to <see langword="bool"/>.</returns>
public static Check<T> IsNotIn<T>(this Check<T> check, params T[] values) =>
check.IsNotIn(values, EqualityComparer<T>.Default);
//
// Types
//
/// <summary>
/// Checks if the target type is assignable from <paramref name="type"/>, that is, if it can appear on the left-hand side of an assignment with <paramref name="type"/> on the right
/// </summary>
/// <param name="check">The <see langword="bool"/>-valued query being continued</param>
/// <param name="type">The type to check for assignability to the target type</param>
/// <returns>A continuation of <paramref name="check"/> applying this operator. Implicitly converts to <see langword="bool"/>.</returns>
public static Check<Type> IsAssignableFrom(this Check<Type> check, Type type) =>
check.That(t => t != null && type != null && t.IsAssignableFrom(type));
/// <summary>
/// Checks if the target type is assignable to <paramref name="type"/>, that is, if it can appear on the right-hand side of an assignment with <paramref name="type"/> on the left
/// </summary>
/// <param name="check">The <see langword="bool"/>-valued query being continued</param>
/// <param name="type">The type to check for assignability from the target type</param>
/// <returns>A continuation of <paramref name="check"/> applying this operator. Implicitly converts to <see langword="bool"/>.</returns>
public static Check<Type> IsAssignableTo(this Check<Type> check, Type type) =>
check.That(t => t != null && type != null && type.IsAssignableFrom(t));
/// <summary>
/// Checks if the target type is assignable to <typeparamref name="T"/>, that is, if it can appear on the right-hand side of an assignment with <typeparamref name="T"/> on the left
/// </summary>
/// <typeparam name="T">The type to check for assignability from the target type</typeparam>
/// <param name="check">The <see langword="bool"/>-valued query being continued</param>
/// <returns>A continuation of <paramref name="check"/> applying this operator. Implicitly converts to <see langword="bool"/>.</returns>
public static Check<Type> IsAssignableTo<T>(this Check<Type> check) =>
check.IsAssignableTo(typeof(T));
//
// Checked arguments
//
/// <summary>
/// Calls the specified function with a <see cref="Check{T}"/> referencing <paramref name="target"/>
/// </summary>
/// <typeparam name="T">The type of target value</typeparam>
/// <param name="checkValue">The function to invoke with a checked argument</param>
/// <param name="target">The value of the checked argument</param>
/// <returns>The result of calling <paramref name="checkValue"/> with a <see cref="Check{T}"/> referencing <paramref name="target"/></returns>
public static bool Invoke<T>(this Func<Check<T>, bool> checkValue, T target) =>
checkValue != null && checkValue(Check.That(target));
}
}