|
5 | 5 | using System.Collections; |
6 | 6 | using System.ComponentModel; |
7 | 7 |
|
8 | | -namespace PdfFileTypePlugin |
| 8 | +namespace PdfFileTypePlugin; |
| 9 | + |
| 10 | +internal static class Ensure |
9 | 11 | { |
10 | | - internal static class Ensure |
| 12 | + public static T IsNotNull<T>(T param, string paramName) where T : class |
11 | 13 | { |
12 | | - public static T IsNotNull<T>(T param, string paramName) where T : class |
13 | | - => param ?? throw new ArgumentNullException(paramName); |
| 14 | + return param ?? throw new ArgumentNullException(paramName); |
| 15 | + } |
14 | 16 |
|
15 | | - public static IntPtr IsNotNull(IntPtr param, string paramName) |
16 | | - => param != IntPtr.Zero ? param : throw new ArgumentNullException(paramName); |
| 17 | + public static IntPtr IsNotNull(IntPtr param, string paramName) |
| 18 | + { |
| 19 | + return param != IntPtr.Zero ? param : throw new ArgumentNullException(paramName); |
| 20 | + } |
17 | 21 |
|
18 | | - public static T IsNotNullOrEmpty<T>(T param, string paramName) where T : class, ICollection |
19 | | - { |
20 | | - IsNotNull(param, paramName); |
21 | | - return param.Count == 0 ? throw new ArgumentException("Collection is empty.", paramName) : param; |
22 | | - } |
| 22 | + public static T IsNotNullOrEmpty<T>(T param, string paramName) where T : class, ICollection |
| 23 | + { |
| 24 | + IsNotNull(param, paramName); |
| 25 | + return param.Count == 0 ? throw new ArgumentException("Collection is empty.", paramName) : param; |
| 26 | + } |
23 | 27 |
|
24 | | - public static string IsNotNullOrEmpty(string param, string paramName) |
25 | | - { |
26 | | - IsNotNull(param, paramName); |
27 | | - return param.Length == 0 ? throw new ArgumentException("String is empty.", paramName) : param; |
28 | | - } |
| 28 | + public static string IsNotNullOrEmpty(string param, string paramName) |
| 29 | + { |
| 30 | + IsNotNull(param, paramName); |
| 31 | + return param.Length == 0 ? throw new ArgumentException("String is empty.", paramName) : param; |
| 32 | + } |
29 | 33 |
|
30 | | - public static string IsNotNullOrWhiteSpace(string param, string paramName) |
31 | | - { |
32 | | - IsNotNull(param, paramName); |
33 | | - return param.Trim().Length == 0 ? throw new ArgumentException("String is empty.", paramName) : param; |
34 | | - } |
| 34 | + public static string IsNotNullOrWhiteSpace(string param, string paramName) |
| 35 | + { |
| 36 | + IsNotNull(param, paramName); |
| 37 | + return param.Trim().Length == 0 ? throw new ArgumentException("String is empty.", paramName) : param; |
| 38 | + } |
35 | 39 |
|
36 | | - public static T IsInRange<T>(T param, T min, T max, string paramName) where T : IComparable<T> |
37 | | - => param.CompareTo(min) >= 0 && param.CompareTo(max) <= 0 ? param : throw new ArgumentOutOfRangeException(paramName, $"Value must be between {min} and {max}."); |
| 40 | + public static T IsInRange<T>(T param, T min, T max, string paramName) where T : IComparable<T> |
| 41 | + { |
| 42 | + return param.CompareTo(min) >= 0 && param.CompareTo(max) <= 0 ? param : throw new ArgumentOutOfRangeException(paramName, $"Value must be between {min} and {max}."); |
| 43 | + } |
38 | 44 |
|
39 | | - public static T IsGreaterThan<T>(T param, T comparand, string paramName) where T : IComparable<T> |
40 | | - => param.CompareTo(comparand) > 0 ? param : throw new ArgumentOutOfRangeException(paramName, $"Value must be greater than {comparand}."); |
| 45 | + public static T IsGreaterThan<T>(T param, T comparand, string paramName) where T : IComparable<T> |
| 46 | + { |
| 47 | + return param.CompareTo(comparand) > 0 ? param : throw new ArgumentOutOfRangeException(paramName, $"Value must be greater than {comparand}."); |
| 48 | + } |
41 | 49 |
|
42 | | - public static T IsGreaterThanOrEqualTo<T>(T param, T comparand, string paramName) where T : IComparable<T> |
43 | | - => param.CompareTo(comparand) >= 0 ? param : throw new ArgumentOutOfRangeException(paramName, $"Value must be greater than or equal to {comparand}."); |
| 50 | + public static T IsGreaterThanOrEqualTo<T>(T param, T comparand, string paramName) where T : IComparable<T> |
| 51 | + { |
| 52 | + return param.CompareTo(comparand) >= 0 ? param : throw new ArgumentOutOfRangeException(paramName, $"Value must be greater than or equal to {comparand}."); |
| 53 | + } |
44 | 54 |
|
45 | | - public static T IsLessThan<T>(T param, T comparand, string paramName) where T : IComparable<T> |
46 | | - => param.CompareTo(comparand) < 0 ? param : throw new ArgumentOutOfRangeException(paramName, $"Value must be less than {comparand}."); |
| 55 | + public static T IsLessThan<T>(T param, T comparand, string paramName) where T : IComparable<T> |
| 56 | + { |
| 57 | + return param.CompareTo(comparand) < 0 ? param : throw new ArgumentOutOfRangeException(paramName, $"Value must be less than {comparand}."); |
| 58 | + } |
47 | 59 |
|
48 | | - public static T IsLessThanOrEqualTo<T>(T param, T comparand, string paramName) where T : IComparable<T> |
49 | | - => param.CompareTo(comparand) <= 0 ? param : throw new ArgumentOutOfRangeException(paramName, $"Value must be less than or equal to {comparand}."); |
| 60 | + public static T IsLessThanOrEqualTo<T>(T param, T comparand, string paramName) where T : IComparable<T> |
| 61 | + { |
| 62 | + return param.CompareTo(comparand) <= 0 ? param : throw new ArgumentOutOfRangeException(paramName, $"Value must be less than or equal to {comparand}."); |
| 63 | + } |
50 | 64 |
|
51 | | - public static T IsEqualTo<T>(T param, T comparand, string paramName) |
52 | | - => param.Equals(comparand) ? param : throw new ArgumentException($"Value must be equal to {comparand}.", paramName); |
| 65 | + public static T IsEqualTo<T>(T param, T comparand, string paramName) where T: notnull |
| 66 | + { |
| 67 | + return param.Equals(comparand) ? param : throw new ArgumentException($"Value must be equal to {comparand}.", paramName); |
| 68 | + } |
53 | 69 |
|
54 | | - public static void IsTrue(bool condition, Action throwException) |
| 70 | + public static void IsTrue(bool condition, Action throwException) |
| 71 | + { |
| 72 | + if (!condition) |
55 | 73 | { |
56 | | - if (!condition) |
57 | | - { |
58 | | - throwException(); |
59 | | - } |
| 74 | + throwException(); |
60 | 75 | } |
| 76 | + } |
61 | 77 |
|
62 | | - public static T Test<T>(Func<T> action, string message) |
| 78 | + public static T Test<T>(Func<T> action, string message) |
| 79 | + { |
| 80 | + try |
| 81 | + { |
| 82 | + return action(); |
| 83 | + } |
| 84 | + catch (Exception ex) |
63 | 85 | { |
64 | | - try |
65 | | - { |
66 | | - return action(); |
67 | | - } |
68 | | - catch (Exception ex) |
69 | | - { |
70 | | - throw new WarningException(message, ex); |
71 | | - } |
| 86 | + throw new WarningException(message, ex); |
72 | 87 | } |
73 | 88 | } |
74 | 89 | } |
0 commit comments