-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseArgUtils.cs
More file actions
847 lines (804 loc) · 33.3 KB
/
Copy pathBaseArgUtils.cs
File metadata and controls
847 lines (804 loc) · 33.3 KB
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
using System;
using System.Collections.Generic;
using System.Net;
namespace BaseLib
{
/// <summary>
/// Utilities for argument checking
/// </summary>
public static class BaseArgUtils
{
/// <summary>
/// Check maximum value, display units as inches.
/// </summary>
/// <param name="value"></param>
/// <param name="maxValue"></param>
/// <param name="argumentName"></param>
public static void ArgumentMaxCheckInch(double value, double maxValue,
string argumentName)
{
if (maxValue < value)
{
throw new ArgumentException(argumentName + "=" + value +
"\", above maximum expected " + maxValue + "\".");
}
}
/// <summary>
/// Check minimum value, display units as inches.
/// </summary>
/// <param name="value"></param>
/// <param name="minValue"></param>
/// <param name="argumentName"></param>
public static void ArgumentMinCheckInch(double value, double minValue,
string argumentName)
{
if (minValue > value)
{
throw new ArgumentException(argumentName + "=" + value +
"\", below minimum expected " + minValue + "\".");
}
}
/// <summary>
/// Throw if list contains aValue. If it does, throw an exception with message including
/// valueName.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <param name="aValue"></param>
/// <param name="valueName"></param>
public static void CheckListDoesNotContain<T>(List<T> list, T aValue, string valueName)
{
NullCheck(list, "list");
if (list.Contains(aValue))
{
throw new ArgumentException("List should not contain " + valueName + "=" + aValue);
}
}
/// <summary>
/// Throw ArgumentOutOfRangeException with argumentMinName, argumentMaxName
/// etc. in the message if min is > max.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="min"></param>
/// <param name="max"></param>
/// <param name="argumentMinName"></param>
/// <param name="argumentMaxName"></param>
/// <returns></returns>
// ReSharper disable InconsistentNaming
public static void CheckMinLEMax<T>(T min, T max, string argumentMinName,
// ReSharper restore InconsistentNaming
string argumentMaxName) where T : IComparable<T>
{
if (min.CompareTo(max) > 0)
{
NullCheck(argumentMinName, "argumentMinName");
NullCheck(argumentMaxName, "argumentMaxName");
throw new ArgumentOutOfRangeException(
argumentMinName + "=" + min + " must be < " +
argumentMaxName + "=" + max + ".");
}
}
/// <summary>
/// Throw ArgumentOutOfRangeException with argumentName etc. in the
/// message if value is outside minValue..maxValue.
/// </summary>
/// <param name="minValue"></param>
/// <param name="maxValue"></param>
/// <param name="value"></param>
/// <param name="argumentName"></param>
public static T CheckRange<T>(T minValue, T maxValue, T value,
string argumentName) where T : IComparable<T>
{
CheckMinLEMax(minValue, maxValue, "minValue", "maxValue");
if (minValue.CompareTo(value) > 0)
{
throw new ArgumentOutOfRangeException(argumentName + "=" + value +
", valid range " + minValue + " to " + maxValue + ".");
}
if (maxValue.CompareTo(value) < 0)
{
throw new ArgumentOutOfRangeException(argumentName + "=" + value +
", valid range " + minValue + " to " + maxValue + ".");
}
return value;
}
/// <summary>
/// Throw IndexOutOfRangeException with argument name etc. in the message if index is outside the range
/// 0..(length-1). Return index.
/// </summary>
/// <param name="length"> </param>
/// <param name="index"> </param>
/// <param name="indexName"> </param>
/// <returns></returns>
public static int CheckIndex(int length, int index, string indexName)
{
if ((index < 0) || (index >= length))
{
throw new IndexOutOfRangeException(indexName + "=" + index +
", valid range 0 to " + (length - 1) + ".");
}
return index;
}
/// <summary>
/// Throw ArgumentOutOfRangeException if (byte) argument is not equal to required value.
/// </summary>
/// <param name="value"></param>
/// <param name="requiredValue"></param>
/// <param name="argumentName"></param>
public static byte CheckEqual(byte value, byte requiredValue, string argumentName)
{
if (requiredValue != value)
{
throw new ArgumentOutOfRangeException(argumentName + "(byte)=" +
value + ", must be " + requiredValue + ".");
}
return value;
}
/// <summary>
/// Throw if array lengths are not equal or on null a, b.
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <param name="argumentNameA"></param>
/// <param name="argumentNameB"></param>
// ReSharper disable InconsistentNaming
public static void CheckEqualLength<T,U>(T[] a, U[] b, string argumentNameA,
// ReSharper restore InconsistentNaming
string argumentNameB)
{
NullCheck(a, argumentNameA);
NullCheck(b, argumentNameB);
if (a.Length != b.Length)
{
throw new ArgumentOutOfRangeException("Length of array " + argumentNameA +
"[" + a.Length + "] must match array " + argumentNameB +
"[" + b.Length + "].");
}
}
/// <summary>
/// Throw if array lengths are not equal or if any of a, b, c are null.
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <param name="c"></param>
/// <param name="argumentNameA"></param>
/// <param name="argumentNameB"></param>
/// <param name="argumentNameC"></param>
// ReSharper disable InconsistentNaming
public static void CheckEqualLength<T, U, V>(T[] a, U[] b, V[] c, string argumentNameA,
// ReSharper restore InconsistentNaming
string argumentNameB, string argumentNameC)
{
NullCheck(a, argumentNameA);
NullCheck(b, argumentNameB);
NullCheck(c, argumentNameC);
if ((a.Length != b.Length) || (a.Length != c.Length))
{
throw new ArgumentOutOfRangeException("Length of arrays " +
argumentNameA + "[" + a.Length + "]," +
argumentNameB + "[" + b.Length + "]," +
argumentNameC + "[" + c.Length + "] must match");
}
}
/// <summary>
/// Throw if array lengths are not equal. Accepts null == null.
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <param name="argumentNameA"></param>
/// <param name="argumentNameB"></param>
public static T[] CheckEqualLengthNullOk<T>(T[] a, T[] b, string argumentNameA,
string argumentNameB)
{
int lengthA = (null == a) ? -1 : a.Length;
int lengthB = (null == b) ? -1 : b.Length;
if (lengthA != lengthB)
{
throw new ArgumentOutOfRangeException("Length of array " + argumentNameA +
((null == a) ? "null" : ("[" + lengthA + "]")) + " must match array " + argumentNameB +
((null == b) ? "null" : ("[" + lengthB + "]")) + ".");
}
return a;
}
/// <summary>
/// Parse and check IP address in string form. Requires ipAddressString is not null or empty (once trimmed).
/// IP address must be of form a.b.c.d (e.g. 192.168.1.234).
/// On success return the resulting IPAddress and set errorMessage = null.
/// On failure throw.
/// </summary>
public static IPAddress CheckIPv4Address(string ipAddressString, string argumentName)
{
ipAddressString = BaseStringUtils.TrimThrowIfNullOrEmpty(ipAddressString,
"Expected IPv4 address (example 192.168.10.4) in " + argumentName);
IPAddress ipAddress;
bool success;
try
{
success = IPAddress.TryParse(ipAddressString, out ipAddress);
}
catch (ArgumentException ex)
{
// Thrown for Unicode characters.
throw new ArgumentException("Invalid IPAddress in " + argumentName +
". Expected IPv4 address (example \"192.168.10.4\"), found \"" + ipAddressString +
"\". Only characters 0-9 and . are allowed.", ex);
}
if (!success)
{
throw new ArgumentException("Invalid IPAddress in " + argumentName +
". Expected IPv4 address (example \"192.168.10.4\"), found \"" + ipAddressString +
"\". Only characters 0-9 and . are allowed.");
}
return ipAddress;
}
/// <summary>
/// Return true if a == b, accepts null == null. Checks array lengths
/// and all elements.
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
public static bool EqualsNullOK(byte[] a, byte[] b)
{
bool aNull = null == a;
bool bNull = null == b;
bool equal = aNull == bNull;
if (!aNull && !bNull)
{
int aLength = a.Length;
int bLength = b.Length;
equal = aLength.Equals(bLength);
for (int index = 0; equal && (index < aLength); ++index)
{
equal = a[index] == b[index];
}
}
return equal;
}
/// <summary>
/// Throw if (byte[]) argument is null or not equal to required value.
/// passes if non-null and array lengths and contents are equal.
/// </summary>
/// <param name="value"></param>
/// <param name="requiredValue"></param>
/// <param name="argumentName"></param>
public static byte[] CheckEqual(byte[] value, byte[] requiredValue,
string argumentName)
{
NullCheck(value, "value");
NullCheck(requiredValue, "requiredValue");
int lengthValue = value.Length;
int lengthRequired = requiredValue.Length;
if (lengthValue != lengthRequired)
{
throw new ArgumentOutOfRangeException("length mismatch: " +
argumentName + "(byte[" + lengthValue +
"]) must be byte[" + lengthRequired + "])");
}
for (int index = 0; index < lengthRequired; ++index)
{
if (requiredValue[index] != value[index])
{
throw new ArgumentOutOfRangeException(argumentName + "[" +
index + "]=0x" + value[index].ToString("X") +
", must be 0X" + requiredValue[index].ToString("X") +
".");
}
}
return value;
}
/// <summary>
/// Get hashcode for byte[] array contents. Note: if array contents
/// will change, they don't belong in an object's hashcode!
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static int GetHashCode(byte[] value)
{
int hash = 0;
int length = value.Length;
hash = (hash * 17) + length;
for (int index = 0; index < length; ++index)
{
hash = hash * 17 + value[index];
}
return hash;
}
/// <summary>
/// Throw if (bool) argument is not equal to required value.
/// </summary>
/// <param name="value"></param>
/// <param name="requiredValue"></param>
/// <param name="argumentName"></param>
public static bool CheckEqual(bool value, bool requiredValue,
string argumentName)
{
if (requiredValue != value)
{
throw new ArgumentOutOfRangeException(argumentName + "(bool)=" +
value + ", must be " + requiredValue + ".");
}
return value;
}
/// <summary>
/// Throw if (int) argument is not equal to required value.
/// </summary>
/// <param name="value"></param>
/// <param name="requiredValue"></param>
/// <param name="argumentName"></param>
public static int CheckEqual(int value, int requiredValue, string argumentName)
{
if (requiredValue != value)
{
throw new ArgumentOutOfRangeException(argumentName + "(int)=" +
value + ", must be " + requiredValue + ".");
}
return value;
}
/// <summary>
/// Throw if (UInt16) argument is not equal to required value.
/// </summary>
/// <param name="value"></param>
/// <param name="requiredValue"></param>
/// <param name="argumentName"></param>
public static UInt16 CheckEqual(UInt16 value, UInt16 requiredValue,
string argumentName)
{
if (requiredValue != value)
{
throw new ArgumentOutOfRangeException(argumentName + "(UInt16)=" +
value + ", must be " + requiredValue + ".");
}
return value;
}
/// <summary>
/// Throw if (UInt32) argument is not equal to required value.
/// </summary>
/// <param name="value"></param>
/// <param name="requiredValue"></param>
/// <param name="argumentName"></param>
public static UInt32 CheckEqual(UInt32 value, UInt32 requiredValue,
string argumentName)
{
if (requiredValue != value)
{
throw new ArgumentOutOfRangeException(argumentName + "(UInt32)=" +
value + ", must be " + requiredValue + ".");
}
return value;
}
/// <summary>
/// Throw if (string) argument is not equal to required value.
/// </summary>
/// <param name="value"></param>
/// <param name="requiredValue"></param>
/// <param name="argumentName"></param>
public static string CheckEqual(string value, string requiredValue,
string argumentName)
{
bool nullValue = null == value;
bool nullRequired = null == requiredValue;
if ((nullValue != nullRequired) ||
(!nullValue && (!requiredValue.Equals(value))))
{
throw new ArgumentOutOfRangeException(argumentName + "(string)=\"" +
(nullValue ? "null" : value) +
"\", must be \"" + (nullRequired ? "null" : requiredValue) + "\".");
}
return value;
}
/// <summary>
/// Throw if (string) argument is not equal to required value.
/// </summary>
/// <param name="value"></param>
/// <param name="requiredValue"></param>
/// <param name="argumentName"></param>
public static string CheckEqualIgnoreCase(string value, string requiredValue,
string argumentName)
{
bool nullValue = null == value;
bool nullRequired = null == requiredValue;
if ((nullValue != nullRequired) ||
(!nullValue && (!requiredValue.Equals(value, StringComparison.InvariantCultureIgnoreCase))))
{
throw new ArgumentOutOfRangeException(argumentName + "(string)=\"" +
(nullValue ? "null" : value) +
"\", must be \"" + (nullRequired ? "null" : requiredValue) + "\" ignoring case.");
}
return value;
}
/// <summary>
/// Add error message to results and return true if (string) argument is not equal to required value,
/// else return false.
/// </summary>
/// <param name="value"></param>
/// <param name="requiredValue"></param>
/// <param name="argumentName"></param>
/// <param name="results"></param>
public static bool RecordIfMismatch(string value, string requiredValue,
string argumentName, InfoWarningErrorLists results)
{
NullCheck(results, "results");
bool nullValue = null == value;
bool nullRequired = null == requiredValue;
if ((nullValue != nullRequired) ||
(!nullValue && (!requiredValue.Equals(value))))
{
results.TryAddError(argumentName + " mismatch! Required=" + BaseStringUtils.QuotedOrNull(requiredValue) +
", value=" + BaseStringUtils.QuotedOrNull(value));
return true;
}
return false;
}
/// <summary>
/// Add error message to results and return true if (bool) argument is not equal to required value,
/// else return false.
/// </summary>
/// <param name="value"></param>
/// <param name="requiredValue"></param>
/// <param name="argumentName"></param>
/// <param name="results"></param>
public static bool RecordIfMismatch(bool value, bool requiredValue,
string argumentName, InfoWarningErrorLists results)
{
NullCheck(results, "results");
if (!requiredValue.Equals(value))
{
results.TryAddError(argumentName + " mismatch! Required=" + requiredValue +
", value=" + value);
return true;
}
return false;
}
/// <summary>
/// Add error message to results and return true if (int) argument is not equal to required value,
/// else return false.
/// </summary>
/// <param name="value"></param>
/// <param name="requiredValue"></param>
/// <param name="argumentName"></param>
/// <param name="results"></param>
public static bool RecordIfMismatch(int value, int requiredValue,
string argumentName, InfoWarningErrorLists results)
{
NullCheck(results, "results");
if (!requiredValue.Equals(value))
{
results.TryAddError(argumentName + " mismatch! Required=" + requiredValue +
", value=" + value);
return true;
}
return false;
}
/// <summary>
/// Add error message to results and return true if (int) argument is not within required value range,
/// else return false.
/// </summary>
/// <param name="value"></param>
/// <param name="valueRangeMin"></param>
/// <param name="valueRangeMax"></param>
/// <param name="argumentName"></param>
/// <param name="results"></param>
public static bool RecordIfMismatchRange(int value, int valueRangeMin, int valueRangeMax,
string argumentName, InfoWarningErrorLists results)
{
NullCheck(results, "results");
CheckMinLEMax(valueRangeMin, valueRangeMax, "valueRangeMin", "valueRangeMax");
if ((value < valueRangeMin) || (value > valueRangeMax))
{
results.TryAddError(argumentName + " mismatch! Range=" + valueRangeMin +
" to " + valueRangeMax + ", value=" + value);
return true;
}
return false;
}
/// <summary>
/// Throw if (byte) argument is equal to required value.
/// </summary>
/// <param name="value"></param>
/// <param name="bannedValue"></param>
/// <param name="argumentName"></param>
public static byte CheckNotEqual(byte value, byte bannedValue,
string argumentName)
{
if (bannedValue == value)
{
throw new ArgumentOutOfRangeException(argumentName + "(byte)=" +
value + ", must NOT be " + bannedValue + ".");
}
return value;
}
/// <summary>
/// Throw if double argument is above maximum value.
/// </summary>
/// <param name="value"></param>
/// <param name="maxValue"></param>
/// <param name="argumentName"></param>
public static double CheckMax(double value, double maxValue,
string argumentName)
{
if (maxValue < value)
{
throw new ArgumentOutOfRangeException(argumentName +
"(double)=" + value + ", above maximum expected " +
maxValue + ".");
}
return value;
}
/// <summary>
/// Throw if int argument is above maximum value.
/// </summary>
/// <param name="value"></param>
/// <param name="maxValue"></param>
/// <param name="argumentName"></param>
public static int CheckMax(int value, int maxValue, string argumentName)
{
if (maxValue < value)
{
throw new ArgumentOutOfRangeException(argumentName + "(int)=" +
value + ", above maximum expected " +
maxValue + ".");
}
return value;
}
/// <summary>
/// Throw if byte argument is above maximum value.
/// </summary>
/// <param name="value"></param>
/// <param name="maxValue"></param>
/// <param name="argumentName"></param>
public static byte CheckMax(byte value, byte maxValue, string argumentName)
{
if (maxValue < value)
{
throw new ArgumentOutOfRangeException(argumentName + "(byte)=" +
value + ", above maximum expected " +
maxValue + ".");
}
return value;
}
/// <summary>
/// Throw if UInt32 argument is above maximum value.
/// </summary>
/// <param name="value"></param>
/// <param name="maxValue"></param>
/// <param name="argumentName"></param>
public static UInt32 CheckMax(UInt32 value, UInt32 maxValue,
string argumentName)
{
if (maxValue < value)
{
throw new ArgumentOutOfRangeException(argumentName + "(UInt32)=" +
value + ", above maximum expected " +
maxValue + ".");
}
return value;
}
/// <summary>
/// Throw ArgumentOutOfRangeException with argumentName etc. in the
/// message if argument is below minimum value.
/// </summary>
/// <param name="value"></param>
/// <param name="minValue"></param>
/// <param name="argumentName"></param>
public static double CheckMin(double value, double minValue, string argumentName)
{
if (minValue > value)
{
throw new ArgumentOutOfRangeException(argumentName + "=" + value +
", below minimum expected " + minValue + ".");
}
return value;
}
/// <summary>
/// Throw ArgumentOutOfRangeException with argumentName etc. in the
/// message if argument is below minimum value.
/// </summary>
/// <param name="value"></param>
/// <param name="minValue"></param>
/// <param name="argumentName"></param>
public static Int16 CheckMin(Int16 value, Int16 minValue, string argumentName)
{
if (minValue > value)
{
throw new ArgumentOutOfRangeException(argumentName + "(Int16)=" +
value + ", below minimum expected " + minValue + ".");
}
return value;
}
/// <summary>
/// Throw ArgumentOutOfRangeException with argumentName etc. in the
/// message if argument is below minimum value.
/// </summary>
/// <param name="value"></param>
/// <param name="minValue"></param>
/// <param name="argumentName"></param>
public static int CheckMin(int value, int minValue, string argumentName)
{
if (minValue > value)
{
throw new ArgumentOutOfRangeException(argumentName + "(int)=" +
value + ", below minimum expected " + minValue + ".");
}
return value;
}
/// <summary>
/// Throw InvalidProgramException, calling getErrorMessage to get the
/// message, if value is below minValue.
/// </summary>
/// <param name="value"></param>
/// <param name="minValue"></param>
/// <param name="errorMessage"></param>
public static int ThrowIfBelowMin(int value, int minValue, string errorMessage)
{
if (minValue > value)
{
throw new ArgumentOutOfRangeException(errorMessage + "(int)=" +
value + " must be >= " + minValue + ".");
}
return value;
}
/// <summary>
/// Throw InvalidProgramException, calling getErrorMessage to get the
/// message, if value is below minValue.
/// </summary>
/// <param name="value"></param>
/// <param name="minValue"></param>
/// <param name="getErrorMessage"></param>
public static int ThrowIfBelowMin(int value, int minValue, GetErrorMessage getErrorMessage)
{
if (minValue > value)
{
throw new ArgumentOutOfRangeException(getErrorMessage(value) + "(int)=" +
value + " must be >= " + minValue + ".");
}
return value;
}
/// <summary>
/// Throw ArgumentOutOfRangeException with argumentName etc. in the
/// message if argument is below minimum value.
/// </summary>
/// <param name="value"></param>
/// <param name="minValue"></param>
/// <param name="argumentName"></param>
public static UInt16 CheckMin(UInt16 value, UInt16 minValue, string argumentName)
{
if (minValue > value)
{
throw new ArgumentOutOfRangeException(argumentName + "(UInt16)=" + value + ", below minimum expected " + minValue + ".");
}
return value;
}
/// <summary>
/// Throw ArgumentOutOfRangeException with argumentName etc. in the message if argument is below minimum value.
/// </summary>
/// <param name="value"></param>
/// <param name="minValue"></param>
/// <param name="argumentName"></param>
public static UInt32 CheckMin(UInt32 value, UInt32 minValue, string argumentName)
{
if (minValue > value)
{
throw new ArgumentOutOfRangeException(argumentName + "(UInt32)=" + value + ", below minimum expected " + minValue + ".");
}
return value;
}
/// <summary>
/// Givan an enum value, check whether it's valid. Throw ArgumentOutOfRangeException if not.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value"></param>
/// <returns></returns>
public static T EnumCheck<T>(T value)
{
if (!Enum.IsDefined(typeof(T), value))
{
throw new ArgumentOutOfRangeException("Expected value of type " + typeof(T) + ", found \"" + value + "\"");
}
return value;
}
public static T EnumCheck<T>(T value, string valueName)
{
if (!Enum.IsDefined(typeof(T), value))
{
throw new ArgumentOutOfRangeException("Expected " + typeof(T) + " value in field \"" + valueName + "\", found \"" + value +
"\"");
}
return value;
}
/// <summary>
/// If item is null, throw ArgumentNullException with name in the message.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="item"></param>
/// <param name="name"></param>
/// <returns></returns>
public static T NullCheck<T>(T item, string name) where T : class
{
if (null == item)
{
throw new ArgumentNullException(name);
}
return item;
}
/// <summary>
/// Use to prevent a value from being set to null or set non-null twice, important if e.g. we attach event
/// handlers.
/// If value is null, throw ArgumentNullException with name in the message.
/// If destination value is non-null (already set), throw InvalidOperationException with name in the message.
/// Set destination to value.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value"></param>
/// <param name="destination"></param>
/// <param name="name"></param>
/// <returns></returns>
public static void NullOrSetTwiceCheck<T>(T value, ref T destination, string name) where T : class
{
if (null == value)
{
throw new ArgumentNullException(name);
}
if (null != destination)
{
throw new InvalidOperationException(name);
}
destination = value;
//return value;
}
/// <summary>
/// If item is null, throw ArgumentNullException with name in the message.
/// If item is empty, throw ArgumentException with name in the message.
/// </summary>
/// <param name="item"></param>
/// <param name="name"></param>
/// <returns></returns>
public static string NullEmptyCheck(string item, string name)
{
if (null == item)
{
throw new ArgumentNullException(name);
}
if (0 == item.Length)
{
throw new ArgumentException("Need non-empty string for " + name);
}
return item;
}
/// <summary>
/// Use to prevent a value from being set twice. Use for setting values that are allowed to be null,
/// so an additional valueSet flag is used.
/// If valueSet is true, throw InvalidOperationException with name in the message.
/// Set destination to value, set valueSet to true.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value"></param>
/// <param name="valueSet"></param>
/// <param name="destination"></param>
/// <param name="name"></param>
/// <returns></returns>
public static T NullOrSetTwiceCheck<T>(T value, ref bool valueSet, ref T destination, string name) where T : class
{
if (valueSet)
{
throw new InvalidOperationException("Already set! " + name);
}
valueSet = true;
destination = value;
return value;
}
/// <summary>
/// If item is null, throw ArgumentNullException with destinationTypeName, valueName, recordNumber in the message.
/// </summary>
public static T NullCheck<T>(T item, string destinationTypeName, string valueName, uint recordNumber) where T : class
{
NullCheck(destinationTypeName, "destinationTypeName");
NullCheck(valueName, "valueName");
if (null == item)
{
throw new FormatException("Expected " + destinationTypeName + " value in field \"" +
valueName + "\" in record " + recordNumber + ", got null.");
}
return item;
}
}
}