@@ -27,6 +27,22 @@ namespace MongoDB.Driver.Core.Misc
2727 [ DebuggerStepThrough ]
2828 public static class Ensure
2929 {
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+
3046 /// <summary>
3147 /// Ensures that the value of a parameter is between a minimum and a maximum value.
3248 /// </summary>
@@ -65,34 +81,36 @@ public static T IsEqualTo<T>(T value, T comparand, string paramName)
6581 }
6682
6783 /// <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.
6985 /// </summary>
7086 /// <typeparam name="T">Type type of the value.</typeparam>
7187 /// <param name="value">The value of the parameter.</param>
7288 /// <param name="comparand">The comparand.</param>
7389 /// <param name="paramName">The name of the parameter.</param>
7490 /// <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 >
7692 {
77- if ( value . CompareTo ( comparand ) < 0 )
93+ if ( value . CompareTo ( comparand ) <= 0 )
7894 {
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 } .";
8096 throw new ArgumentOutOfRangeException ( paramName , message ) ;
8197 }
8298 return value ;
8399 }
84100
85101 /// <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 .
87103 /// </summary>
104+ /// <typeparam name="T">Type type of the value.</typeparam>
88105 /// <param name="value">The value of the parameter.</param>
106+ /// <param name="comparand">The comparand.</param>
89107 /// <param name="paramName">The name of the parameter.</param>
90108 /// <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 >
92110 {
93- if ( value < 0 )
111+ if ( value . CompareTo ( comparand ) < 0 )
94112 {
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 ) ;
96114 throw new ArgumentOutOfRangeException ( paramName , message ) ;
97115 }
98116 return value ;
@@ -104,79 +122,62 @@ public static int IsGreaterThanOrEqualToZero(int value, string paramName)
104122 /// <param name="value">The value of the parameter.</param>
105123 /// <param name="paramName">The name of the parameter.</param>
106124 /// <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 ) ;
116127
117128 /// <summary>
118129 /// Ensures that the value of a parameter is greater than or equal to zero.
119130 /// </summary>
120131 /// <param name="value">The value of the parameter.</param>
121132 /// <param name="paramName">The name of the parameter.</param>
122133 /// <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 ) ;
132145
133146 /// <summary>
134147 /// Ensures that the value of a parameter is greater than zero.
135148 /// </summary>
136149 /// <param name="value">The value of the parameter.</param>
137150 /// <param name="paramName">The name of the parameter.</param>
138151 /// <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 ) ;
148154
149155 /// <summary>
150156 /// Ensures that the value of a parameter is greater than zero.
151157 /// </summary>
152158 /// <param name="value">The value of the parameter.</param>
153159 /// <param name="paramName">The name of the parameter.</param>
154160 /// <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 ) ;
164163
165164 /// <summary>
166165 /// Ensures that the value of a parameter is greater than zero.
167166 /// </summary>
168167 /// <param name="value">The value of the parameter.</param>
169168 /// <param name="paramName">The name of the parameter.</param>
170169 /// <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 ) ;
180181
181182 /// <summary>
182183 /// 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
247248 return values ;
248249 }
249250
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-
266251 /// <summary>
267252 /// Ensures that the value of a parameter is not null or empty.
268253 /// </summary>
@@ -298,6 +283,24 @@ public static T IsNull<T>(T value, string paramName) where T : class
298283 return value ;
299284 }
300285
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+
301304 /// <summary>
302305 /// Ensures that the value of a parameter is null or greater than or equal to zero.
303306 /// </summary>
0 commit comments