Skip to content

Commit 22f04f1

Browse files
authored
Improvements and bug fixes to Ensure (#132)
Fixes a couple of bugs and adds NotNullOrEmpty<T>(IEnumerable<T> argument, string argumentName) to the Ensure class.
1 parent 7c89ec0 commit 22f04f1

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/ReactiveDomain.Core/Util/Ensure.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ public static void NotNull<T>(T argument, string argumentName) where T : class
2626
public static void NotNullOrEmpty(string argument, string argumentName)
2727
{
2828
if (string.IsNullOrEmpty(argument))
29-
throw new ArgumentNullException(argument, argumentName);
29+
throw new ArgumentNullException(argumentName, argument);
3030
}
31+
3132
/// <summary>
3233
/// Ensure that the argument (ICollection) is neither null nor empty (throw an exception if it is)
3334
/// </summary>
@@ -41,6 +42,19 @@ public static void NotNullOrEmpty<T>(ICollection<T> argument, string argumentNam
4142
throw new ArgumentOutOfRangeException(argumentName, argumentName + " must have items.");
4243
}
4344

45+
/// <summary>
46+
/// Ensure that the argument (IEnumerable) is neither null nor empty (throw an exception if it is)
47+
/// </summary>
48+
/// <param name="argument"></param>
49+
/// <param name="argumentName"></param>
50+
public static void NotNullOrEmpty<T>(IEnumerable<T> argument, string argumentName)
51+
{
52+
if (argument == null)
53+
throw new ArgumentNullException(argumentName);
54+
if (!argument.Any())
55+
throw new ArgumentOutOfRangeException(argumentName, argumentName + " must have items.");
56+
}
57+
4458
/// <summary>
4559
/// Ensure that the argument (an int) is &gt;0 (throw an exception if it is &lt;=0)
4660
/// </summary>
@@ -115,7 +129,7 @@ public static void Nonnegative(decimal number, string argumentName)
115129
public static void NotEmptyGuid(Guid guid, string argumentName)
116130
{
117131
if (Guid.Empty == guid)
118-
throw new ArgumentException(argumentName, argumentName + " shoud be non-empty GUID.");
132+
throw new ArgumentException(argumentName + " should be non-empty GUID.", argumentName);
119133
}
120134

121135
/// <summary>
@@ -173,7 +187,7 @@ public static void Equal(Guid expected, Guid actual, string argumentName)
173187
/// <param name="argumentName"></param>
174188
public static void PowerOf2(int argument, string argumentName)
175189
{
176-
if ((argument <= 0) || (((uint)argument) & ((uint)argument - 1)) != 0)
190+
if (argument <= 0 || ((uint)argument & ((uint)argument - 1)) != 0)
177191
throw new ArgumentException($"{argumentName}: {argument} is not a power of 2");
178192
}
179193

0 commit comments

Comments
 (0)