@@ -27,6 +27,22 @@ namespace MongoDB.Driver.Core.Misc
27
27
[ DebuggerStepThrough ]
28
28
public static class Ensure
29
29
{
30
+ /// <summary>
31
+ /// Ensures that the value of a parameter is not null.
32
+ /// </summary>
33
+ /// <typeparam name="T">Type type of the value.</typeparam>
34
+ /// <param name="value">The value of the parameter.</param>
35
+ /// <param name="paramName">The name of the parameter.</param>
36
+ /// <returns>The value of the parameter.</returns>
37
+ public static Nullable < T > HasValue < T > ( Nullable < T > value , string paramName ) where T : struct
38
+ {
39
+ if ( ! value . HasValue )
40
+ {
41
+ throw new ArgumentException ( "The Nullable parameter must have a value." , paramName ) ;
42
+ }
43
+ return value ;
44
+ }
45
+
30
46
/// <summary>
31
47
/// Ensures that the value of a parameter is between a minimum and a maximum value.
32
48
/// </summary>
@@ -65,34 +81,36 @@ public static T IsEqualTo<T>(T value, T comparand, string paramName)
65
81
}
66
82
67
83
/// <summary>
68
- /// Ensures that the value of a parameter is greater than or equal to a comparand.
84
+ /// Ensures that the value of a parameter is greater than a comparand.
69
85
/// </summary>
70
86
/// <typeparam name="T">Type type of the value.</typeparam>
71
87
/// <param name="value">The value of the parameter.</param>
72
88
/// <param name="comparand">The comparand.</param>
73
89
/// <param name="paramName">The name of the parameter.</param>
74
90
/// <returns>The value of the parameter.</returns>
75
- public static T IsGreaterThanOrEqualTo < T > ( T value , T comparand , string paramName ) where T : IComparable < T >
91
+ public static T IsGreaterThan < T > ( T value , T comparand , string paramName ) where T : IComparable < T >
76
92
{
77
- if ( value . CompareTo ( comparand ) < 0 )
93
+ if ( value . CompareTo ( comparand ) <= 0 )
78
94
{
79
- var message = string . Format ( "Value is not greater than or equal to {1 }: {0 }." , value , comparand ) ;
95
+ var message = $ "Value is not greater than { comparand } : { value } .";
80
96
throw new ArgumentOutOfRangeException ( paramName , message ) ;
81
97
}
82
98
return value ;
83
99
}
84
100
85
101
/// <summary>
86
- /// Ensures that the value of a parameter is greater than or equal to zero .
102
+ /// Ensures that the value of a parameter is greater than or equal to a comparand .
87
103
/// </summary>
104
+ /// <typeparam name="T">Type type of the value.</typeparam>
88
105
/// <param name="value">The value of the parameter.</param>
106
+ /// <param name="comparand">The comparand.</param>
89
107
/// <param name="paramName">The name of the parameter.</param>
90
108
/// <returns>The value of the parameter.</returns>
91
- public static int IsGreaterThanOrEqualToZero ( int value , string paramName )
109
+ public static T IsGreaterThanOrEqualTo < T > ( T value , T comparand , string paramName ) where T : IComparable < T >
92
110
{
93
- if ( value < 0 )
111
+ if ( value . CompareTo ( comparand ) < 0 )
94
112
{
95
- var message = string . Format ( "Value is not greater than or equal to 0 : {0}." , value ) ;
113
+ var message = string . Format ( "Value is not greater than or equal to {1} : {0}." , value , comparand ) ;
96
114
throw new ArgumentOutOfRangeException ( paramName , message ) ;
97
115
}
98
116
return value ;
@@ -104,79 +122,62 @@ public static int IsGreaterThanOrEqualToZero(int value, string paramName)
104
122
/// <param name="value">The value of the parameter.</param>
105
123
/// <param name="paramName">The name of the parameter.</param>
106
124
/// <returns>The value of the parameter.</returns>
107
- public static long IsGreaterThanOrEqualToZero ( long value , string paramName )
108
- {
109
- if ( value < 0 )
110
- {
111
- var message = string . Format ( "Value is not greater than or equal to 0: {0}." , value ) ;
112
- throw new ArgumentOutOfRangeException ( paramName , message ) ;
113
- }
114
- return value ;
115
- }
125
+ public static int IsGreaterThanOrEqualToZero ( int value , string paramName ) =>
126
+ IsGreaterThanOrEqualTo ( value , 0 , paramName ) ;
116
127
117
128
/// <summary>
118
129
/// Ensures that the value of a parameter is greater than or equal to zero.
119
130
/// </summary>
120
131
/// <param name="value">The value of the parameter.</param>
121
132
/// <param name="paramName">The name of the parameter.</param>
122
133
/// <returns>The value of the parameter.</returns>
123
- public static TimeSpan IsGreaterThanOrEqualToZero ( TimeSpan value , string paramName )
124
- {
125
- if ( value < TimeSpan . Zero )
126
- {
127
- var message = string . Format ( "Value is not greater than or equal to zero: {0}." , TimeSpanParser . ToString ( value ) ) ;
128
- throw new ArgumentOutOfRangeException ( paramName , message ) ;
129
- }
130
- return value ;
131
- }
134
+ public static long IsGreaterThanOrEqualToZero ( long value , string paramName ) =>
135
+ IsGreaterThanOrEqualTo ( value , 0 , paramName ) ;
136
+
137
+ /// <summary>
138
+ /// Ensures that the value of a parameter is greater than or equal to zero.
139
+ /// </summary>
140
+ /// <param name="value">The value of the parameter.</param>
141
+ /// <param name="paramName">The name of the parameter.</param>
142
+ /// <returns>The value of the parameter.</returns>
143
+ public static TimeSpan IsGreaterThanOrEqualToZero ( TimeSpan value , string paramName ) =>
144
+ IsGreaterThanOrEqualTo ( value , TimeSpan . Zero , paramName ) ;
132
145
133
146
/// <summary>
134
147
/// Ensures that the value of a parameter is greater than zero.
135
148
/// </summary>
136
149
/// <param name="value">The value of the parameter.</param>
137
150
/// <param name="paramName">The name of the parameter.</param>
138
151
/// <returns>The value of the parameter.</returns>
139
- public static int IsGreaterThanZero ( int value , string paramName )
140
- {
141
- if ( value <= 0 )
142
- {
143
- var message = string . Format ( "Value is not greater than zero: {0}." , value ) ;
144
- throw new ArgumentOutOfRangeException ( paramName , message ) ;
145
- }
146
- return value ;
147
- }
152
+ public static int IsGreaterThanZero ( int value , string paramName ) =>
153
+ IsGreaterThan ( value , 0 , paramName ) ;
148
154
149
155
/// <summary>
150
156
/// Ensures that the value of a parameter is greater than zero.
151
157
/// </summary>
152
158
/// <param name="value">The value of the parameter.</param>
153
159
/// <param name="paramName">The name of the parameter.</param>
154
160
/// <returns>The value of the parameter.</returns>
155
- public static long IsGreaterThanZero ( long value , string paramName )
156
- {
157
- if ( value <= 0 )
158
- {
159
- var message = string . Format ( "Value is not greater than zero: {0}." , value ) ;
160
- throw new ArgumentOutOfRangeException ( paramName , message ) ;
161
- }
162
- return value ;
163
- }
161
+ public static long IsGreaterThanZero ( long value , string paramName ) =>
162
+ IsGreaterThan ( value , 0 , paramName ) ;
164
163
165
164
/// <summary>
166
165
/// Ensures that the value of a parameter is greater than zero.
167
166
/// </summary>
168
167
/// <param name="value">The value of the parameter.</param>
169
168
/// <param name="paramName">The name of the parameter.</param>
170
169
/// <returns>The value of the parameter.</returns>
171
- public static TimeSpan IsGreaterThanZero ( TimeSpan value , string paramName )
172
- {
173
- if ( value <= TimeSpan . Zero )
174
- {
175
- var message = string . Format ( "Value is not greater than zero: {0}." , value ) ;
176
- throw new ArgumentOutOfRangeException ( paramName , message ) ;
177
- }
178
- return value ;
179
- }
170
+ public static double IsGreaterThanZero ( double value , string paramName ) =>
171
+ IsGreaterThan ( value , 0 , paramName ) ;
172
+
173
+ /// <summary>
174
+ /// Ensures that the value of a parameter is greater than zero.
175
+ /// </summary>
176
+ /// <param name="value">The value of the parameter.</param>
177
+ /// <param name="paramName">The name of the parameter.</param>
178
+ /// <returns>The value of the parameter.</returns>
179
+ public static TimeSpan IsGreaterThanZero ( TimeSpan value , string paramName ) =>
180
+ IsGreaterThan ( value , TimeSpan . Zero , paramName ) ;
180
181
181
182
/// <summary>
182
183
/// Ensures that the value of a parameter is infinite or greater than or equal to zero.
@@ -247,22 +248,6 @@ public static IEnumerable<T> IsNotNullAndDoesNotContainAnyNulls<T>(IEnumerable<T
247
248
return values ;
248
249
}
249
250
250
- /// <summary>
251
- /// Ensures that the value of a parameter is not null.
252
- /// </summary>
253
- /// <typeparam name="T">Type type of the value.</typeparam>
254
- /// <param name="value">The value of the parameter.</param>
255
- /// <param name="paramName">The name of the parameter.</param>
256
- /// <returns>The value of the parameter.</returns>
257
- public static Nullable < T > HasValue < T > ( Nullable < T > value , string paramName ) where T : struct
258
- {
259
- if ( ! value . HasValue )
260
- {
261
- throw new ArgumentException ( "The Nullable parameter must have a value." , paramName ) ;
262
- }
263
- return value ;
264
- }
265
-
266
251
/// <summary>
267
252
/// Ensures that the value of a parameter is not null or empty.
268
253
/// </summary>
@@ -298,6 +283,24 @@ public static T IsNull<T>(T value, string paramName) where T : class
298
283
return value ;
299
284
}
300
285
286
+ /// <summary>
287
+ /// Ensures that the value of a parameter is null or is between a minimum and a maximum value.
288
+ /// </summary>
289
+ /// <typeparam name="T">Type type of the value.</typeparam>
290
+ /// <param name="value">The value of the parameter.</param>
291
+ /// <param name="min">The minimum value.</param>
292
+ /// <param name="max">The maximum value.</param>
293
+ /// <param name="paramName">The name of the parameter.</param>
294
+ /// <returns>The value of the parameter.</returns>
295
+ public static T ? IsNullOrBetween < T > ( T ? value , T min , T max , string paramName ) where T : struct , IComparable < T >
296
+ {
297
+ if ( value != null )
298
+ {
299
+ IsBetween ( value . Value , min , max , paramName ) ;
300
+ }
301
+ return value ;
302
+ }
303
+
301
304
/// <summary>
302
305
/// Ensures that the value of a parameter is null or greater than or equal to zero.
303
306
/// </summary>
0 commit comments