/
Ensure.cs
487 lines (454 loc) · 20.6 KB
/
Ensure.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/* Copyright 2013-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
namespace MongoDB.Driver.Core.Misc
{
/// <summary>
/// Represents methods that can be used to ensure that parameter values meet expected conditions.
/// </summary>
[DebuggerStepThrough]
public static class Ensure
{
/// <summary>
/// Ensures that the value of a parameter is not null.
/// </summary>
/// <typeparam name="T">Type type of the value.</typeparam>
/// <param name="value">The value of the parameter.</param>
/// <param name="paramName">The name of the parameter.</param>
/// <returns>The value of the parameter.</returns>
public static Nullable<T> HasValue<T>(Nullable<T> value, string paramName) where T : struct
{
if (!value.HasValue)
{
throw new ArgumentException("The Nullable parameter must have a value.", paramName);
}
return value;
}
/// <summary>
/// Ensures that the value of a parameter is between a minimum and a maximum value.
/// </summary>
/// <typeparam name="T">Type type of the value.</typeparam>
/// <param name="value">The value of the parameter.</param>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <param name="paramName">The name of the parameter.</param>
/// <returns>The value of the parameter.</returns>
public static T IsBetween<T>(T value, T min, T max, string paramName) where T : IComparable<T>
{
if (value.CompareTo(min) < 0 || value.CompareTo(max) > 0)
{
var message = string.Format("Value is not between {1} and {2}: {0}.", value, min, max);
throw new ArgumentOutOfRangeException(paramName, message);
}
return value;
}
/// <summary>
/// Ensures that the value of a parameter is equal to a comparand.
/// </summary>
/// <typeparam name="T">Type type of the value.</typeparam>
/// <param name="value">The value of the parameter.</param>
/// <param name="comparand">The comparand.</param>
/// <param name="paramName">The name of the parameter.</param>
/// <returns>The value of the parameter.</returns>
public static T IsEqualTo<T>(T value, T comparand, string paramName)
{
if (!value.Equals(comparand))
{
var message = string.Format("Value is not equal to {1}: {0}.", value, comparand);
throw new ArgumentException(message, paramName);
}
return value;
}
/// <summary>
/// Ensures that the value of a parameter is greater than a comparand.
/// </summary>
/// <typeparam name="T">Type type of the value.</typeparam>
/// <param name="value">The value of the parameter.</param>
/// <param name="comparand">The comparand.</param>
/// <param name="paramName">The name of the parameter.</param>
/// <returns>The value of the parameter.</returns>
public static T IsGreaterThan<T>(T value, T comparand, string paramName) where T : IComparable<T>
{
if (value.CompareTo(comparand) <= 0)
{
var message = $"Value is not greater than {comparand}: {value}.";
throw new ArgumentOutOfRangeException(paramName, message);
}
return value;
}
/// <summary>
/// Ensures that the value of a parameter is greater than or equal to a comparand.
/// </summary>
/// <typeparam name="T">Type type of the value.</typeparam>
/// <param name="value">The value of the parameter.</param>
/// <param name="comparand">The comparand.</param>
/// <param name="paramName">The name of the parameter.</param>
/// <returns>The value of the parameter.</returns>
public static T IsGreaterThanOrEqualTo<T>(T value, T comparand, string paramName) where T : IComparable<T>
{
if (value.CompareTo(comparand) < 0)
{
var message = string.Format("Value is not greater than or equal to {1}: {0}.", value, comparand);
throw new ArgumentOutOfRangeException(paramName, message);
}
return value;
}
/// <summary>
/// Ensures that the value of a parameter is greater than or equal to zero.
/// </summary>
/// <param name="value">The value of the parameter.</param>
/// <param name="paramName">The name of the parameter.</param>
/// <returns>The value of the parameter.</returns>
public static int IsGreaterThanOrEqualToZero(int value, string paramName) =>
IsGreaterThanOrEqualTo(value, 0, paramName);
/// <summary>
/// Ensures that the value of a parameter is greater than or equal to zero.
/// </summary>
/// <param name="value">The value of the parameter.</param>
/// <param name="paramName">The name of the parameter.</param>
/// <returns>The value of the parameter.</returns>
public static long IsGreaterThanOrEqualToZero(long value, string paramName) =>
IsGreaterThanOrEqualTo(value, 0, paramName);
/// <summary>
/// Ensures that the value of a parameter is greater than or equal to zero.
/// </summary>
/// <param name="value">The value of the parameter.</param>
/// <param name="paramName">The name of the parameter.</param>
/// <returns>The value of the parameter.</returns>
public static TimeSpan IsGreaterThanOrEqualToZero(TimeSpan value, string paramName) =>
IsGreaterThanOrEqualTo(value, TimeSpan.Zero, paramName);
/// <summary>
/// Ensures that the value of a parameter is greater than zero.
/// </summary>
/// <param name="value">The value of the parameter.</param>
/// <param name="paramName">The name of the parameter.</param>
/// <returns>The value of the parameter.</returns>
public static int IsGreaterThanZero(int value, string paramName) =>
IsGreaterThan(value, 0, paramName);
/// <summary>
/// Ensures that the value of a parameter is greater than zero.
/// </summary>
/// <param name="value">The value of the parameter.</param>
/// <param name="paramName">The name of the parameter.</param>
/// <returns>The value of the parameter.</returns>
public static long IsGreaterThanZero(long value, string paramName) =>
IsGreaterThan(value, 0, paramName);
/// <summary>
/// Ensures that the value of a parameter is greater than zero.
/// </summary>
/// <param name="value">The value of the parameter.</param>
/// <param name="paramName">The name of the parameter.</param>
/// <returns>The value of the parameter.</returns>
public static double IsGreaterThanZero(double value, string paramName) =>
IsGreaterThan(value, 0, paramName);
/// <summary>
/// Ensures that the value of a parameter is greater than zero.
/// </summary>
/// <param name="value">The value of the parameter.</param>
/// <param name="paramName">The name of the parameter.</param>
/// <returns>The value of the parameter.</returns>
public static TimeSpan IsGreaterThanZero(TimeSpan value, string paramName) =>
IsGreaterThan(value, TimeSpan.Zero, paramName);
/// <summary>
/// Ensures that the value of a parameter is infinite or greater than or equal to zero.
/// </summary>
/// <param name="value">The value of the parameter.</param>
/// <param name="paramName">The name of the parameter.</param>
/// <returns>The value of the parameter.</returns>
public static TimeSpan IsInfiniteOrGreaterThanOrEqualToZero(TimeSpan value, string paramName)
{
if (value < TimeSpan.Zero && value != Timeout.InfiniteTimeSpan)
{
var message = string.Format("Value is not infinite or greater than or equal to zero: {0}.", TimeSpanParser.ToString(value));
throw new ArgumentOutOfRangeException(paramName, message);
}
return value;
}
/// <summary>
/// Ensures that the value of a parameter is infinite or greater than zero.
/// </summary>
/// <param name="value">The value of the parameter.</param>
/// <param name="paramName">The name of the parameter.</param>
/// <returns>The value of the parameter.</returns>
public static TimeSpan IsInfiniteOrGreaterThanZero(TimeSpan value, string paramName)
{
if (value == Timeout.InfiniteTimeSpan || value > TimeSpan.Zero)
{
return value;
}
var message = string.Format("Value is not infinite or greater than zero: {0}.", TimeSpanParser.ToString(value));
throw new ArgumentOutOfRangeException(paramName, message);
}
/// <summary>
/// Ensures that the value of a parameter is not null.
/// </summary>
/// <typeparam name="T">Type type of the value.</typeparam>
/// <param name="value">The value of the parameter.</param>
/// <param name="paramName">The name of the parameter.</param>
/// <returns>The value of the parameter.</returns>
public static T IsNotNull<T>(T value, string paramName) where T : class
{
if (value == null)
{
throw new ArgumentNullException(paramName, "Value cannot be null.");
}
return value;
}
/// <summary>
/// Ensures that the value of a parameter is not null and does not contain any nulls.
/// </summary>
/// <typeparam name="T">Type of the value.</typeparam>
/// <param name="values">The value of the parameter.</param>
/// <param name="paramName">The name of the parameter.</param>
/// <returns>The value of the parameter.</returns>
public static IEnumerable<T> IsNotNullAndDoesNotContainAnyNulls<T>(IEnumerable<T> values, string paramName)
where T : class
{
if (values == null)
{
throw new ArgumentNullException(paramName, "Values cannot be null.");
}
if (values.Any(v => v == null))
{
throw new ArgumentNullException(paramName, "Values cannot contain any null items.");
}
return values;
}
/// <summary>
/// Ensures that the value of a parameter is not null or empty.
/// </summary>
/// <param name="value">The value of the parameter.</param>
/// <param name="paramName">The name of the parameter.</param>
/// <returns>The value of the parameter.</returns>
public static string IsNotNullOrEmpty(string value, string paramName)
{
if (value == null)
{
throw new ArgumentNullException(paramName);
}
if (value.Length == 0)
{
throw new ArgumentException("Value cannot be empty.", paramName);
}
return value;
}
/// <summary>
/// Ensures that the value of a parameter is null.
/// </summary>
/// <typeparam name="T">Type type of the value.</typeparam>
/// <param name="value">The value of the parameter.</param>
/// <param name="paramName">The name of the parameter.</param>
/// <returns>The value of the parameter.</returns>
public static T IsNull<T>(T value, string paramName) where T : class
{
if (value != null)
{
throw new ArgumentNullException(paramName, "Value must be null.");
}
return value;
}
/// <summary>
/// Ensures that the value of a parameter is null or is between a minimum and a maximum value.
/// </summary>
/// <typeparam name="T">Type type of the value.</typeparam>
/// <param name="value">The value of the parameter.</param>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <param name="paramName">The name of the parameter.</param>
/// <returns>The value of the parameter.</returns>
public static T? IsNullOrBetween<T>(T? value, T min, T max, string paramName) where T : struct, IComparable<T>
{
if (value != null)
{
IsBetween(value.Value, min, max, paramName);
}
return value;
}
/// <summary>
/// Ensures that the value of a parameter is null or greater than or equal to zero.
/// </summary>
/// <param name="value">The value of the parameter.</param>
/// <param name="paramName">The name of the parameter.</param>
/// <returns>The value of the parameter.</returns>
public static int? IsNullOrGreaterThanOrEqualToZero(int? value, string paramName)
{
if (value != null)
{
IsGreaterThanOrEqualToZero(value.Value, paramName);
}
return value;
}
/// <summary>
/// Ensures that the value of a parameter is null or greater than or equal to zero.
/// </summary>
/// <param name="value">The value of the parameter.</param>
/// <param name="paramName">The name of the parameter.</param>
/// <returns>The value of the parameter.</returns>
public static long? IsNullOrGreaterThanOrEqualToZero(long? value, string paramName)
{
if (value != null)
{
IsGreaterThanOrEqualToZero(value.Value, paramName);
}
return value;
}
/// <summary>
/// Ensures that the value of a parameter is null or greater than zero.
/// </summary>
/// <param name="value">The value of the parameter.</param>
/// <param name="paramName">The name of the parameter.</param>
/// <returns>The value of the parameter.</returns>
public static int? IsNullOrGreaterThanZero(int? value, string paramName)
{
if (value != null)
{
IsGreaterThanZero(value.Value, paramName);
}
return value;
}
/// <summary>
/// Ensures that the value of a parameter is null or greater than zero.
/// </summary>
/// <param name="value">The value of the parameter.</param>
/// <param name="paramName">The name of the parameter.</param>
/// <returns>The value of the parameter.</returns>
public static long? IsNullOrGreaterThanZero(long? value, string paramName)
{
if (value != null)
{
IsGreaterThanZero(value.Value, paramName);
}
return value;
}
/// <summary>
/// Ensures that the value of a parameter is null or greater than zero.
/// </summary>
/// <param name="value">The value of the parameter.</param>
/// <param name="paramName">The name of the parameter.</param>
/// <returns>The value of the parameter.</returns>
public static TimeSpan? IsNullOrGreaterThanZero(TimeSpan? value, string paramName)
{
if (value != null)
{
IsGreaterThanZero(value.Value, paramName);
}
return value;
}
/// <summary>
/// Ensures that the value of a parameter is null, or infinite, or greater than or equal to zero.
/// </summary>
/// <param name="value">The value of the parameter.</param>
/// <param name="paramName">The name of the parameter.</param>
/// <returns>The value of the parameter.</returns>
public static TimeSpan? IsNullOrInfiniteOrGreaterThanOrEqualToZero(TimeSpan? value, string paramName)
{
if (value.HasValue && value.Value < TimeSpan.Zero && value.Value != Timeout.InfiniteTimeSpan)
{
var message = string.Format("Value is not null or infinite or greater than or equal to zero: {0}.", TimeSpanParser.ToString(value.Value));
throw new ArgumentOutOfRangeException(paramName, message);
}
return value;
}
/// <summary>
/// Ensures that the value of a parameter is null or not empty.
/// </summary>
/// <param name="value">The value of the parameter.</param>
/// <param name="paramName">The name of the parameter.</param>
/// <returns>The value of the parameter.</returns>
public static string IsNullOrNotEmpty(string value, string paramName)
{
if (value != null && value == "")
{
throw new ArgumentException("Value cannot be empty.", paramName);
}
return value;
}
/// <summary>
/// Ensures that the value of a parameter is null or a valid timeout.
/// </summary>
/// <param name="value">The value of the parameter.</param>
/// <param name="paramName">The name of the parameter.</param>
/// <returns>The value of the parameter.</returns>
public static TimeSpan? IsNullOrValidTimeout(TimeSpan? value, string paramName)
{
if (value != null)
{
IsValidTimeout(value.Value, paramName);
}
return value;
}
/// <summary>
/// Ensures that the value of a parameter is a valid timeout.
/// </summary>
/// <param name="value">The value of the parameter.</param>
/// <param name="paramName">The name of the parameter.</param>
/// <returns>The value of the parameter.</returns>
public static TimeSpan IsValidTimeout(TimeSpan value, string paramName)
{
if (value < TimeSpan.Zero && value != Timeout.InfiniteTimeSpan)
{
var message = string.Format("Invalid timeout: {0}.", value);
throw new ArgumentException(message, paramName);
}
return value;
}
/// <summary>
/// Ensures that an assertion is true.
/// </summary>
/// <param name="assertion">The assertion.</param>
/// <param name="message">The message to use with the exception that is thrown if the assertion is false.</param>
public static void That(bool assertion, string message)
{
if (!assertion)
{
throw new ArgumentException(message);
}
}
/// <summary>
/// Ensures that an assertion is true.
/// </summary>
/// <param name="assertion">The assertion.</param>
/// <param name="message">The message to use with the exception that is thrown if the assertion is false.</param>
/// <param name="paramName">The parameter name.</param>
public static void That(bool assertion, string message, string paramName)
{
if (!assertion)
{
throw new ArgumentException(message, paramName);
}
}
/// <summary>
/// Ensures that the value of a parameter meets an assertion.
/// </summary>
/// <typeparam name="T">Type type of the value.</typeparam>
/// <param name="value">The value of the parameter.</param>
/// <param name="assertion">The assertion.</param>
/// <param name="paramName">The name of the parameter.</param>
/// <param name="message">The message to use with the exception that is thrown if the assertion is false.</param>
/// <returns>The value of the parameter.</returns>
public static T That<T>(T value, Func<T, bool> assertion, string paramName, string message)
{
if (!assertion(value))
{
throw new ArgumentException(message, paramName);
}
return value;
}
}
}