|
|
|
@ -15,16 +15,15 @@ namespace SixLabors.ImageSharp |
|
|
|
internal static class Guard |
|
|
|
{ |
|
|
|
/// <summary>
|
|
|
|
/// Verifies, that the method parameter with specified object value is not null
|
|
|
|
/// and throws an exception if it is found to be so.
|
|
|
|
/// Ensures that the value is not null.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="target">The target object, which cannot be null.</param>
|
|
|
|
/// <param name="value">The target object, which cannot be null.</param>
|
|
|
|
/// <param name="parameterName">The name of the parameter that is to be checked.</param>
|
|
|
|
/// <param name="message">The error message, if any to add to the exception.</param>
|
|
|
|
/// <exception cref="ArgumentNullException"><paramref name="target"/> is null</exception>
|
|
|
|
public static void NotNull(object target, string parameterName, string message = "") |
|
|
|
/// <exception cref="ArgumentNullException"><paramref name="value"/> is null</exception>
|
|
|
|
public static void NotNull(object value, string parameterName, string message = "") |
|
|
|
{ |
|
|
|
if (target == null) |
|
|
|
if (value == null) |
|
|
|
{ |
|
|
|
if (!string.IsNullOrWhiteSpace(message)) |
|
|
|
{ |
|
|
|
@ -36,57 +35,49 @@ namespace SixLabors.ImageSharp |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Verifies, that the string method parameter with specified object value and message
|
|
|
|
/// is not null, not empty and does not contain only blanks and throws an exception
|
|
|
|
/// if the object is null.
|
|
|
|
/// Ensures that the target value is not null, empty, or whitespace.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="target">The target string, which should be checked against being null or empty.</param>
|
|
|
|
/// <param name="value">The target string, which should be checked against being null or empty.</param>
|
|
|
|
/// <param name="parameterName">Name of the parameter.</param>
|
|
|
|
/// <param name="message">The error message, if any to add to the exception.</param>
|
|
|
|
/// <exception cref="ArgumentNullException"><paramref name="target"/> is null.</exception>
|
|
|
|
/// <exception cref="ArgumentException"><paramref name="target"/> is empty or contains only blanks.</exception>
|
|
|
|
public static void NotNullOrEmpty(string target, string parameterName, string message = "") |
|
|
|
/// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception>
|
|
|
|
/// <exception cref="ArgumentException"><paramref name="value"/> is empty or contains only blanks.</exception>
|
|
|
|
public static void NotNullOrWhiteSpace(string value, string parameterName) |
|
|
|
{ |
|
|
|
NotNull(target, parameterName, message); |
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(target)) |
|
|
|
if (value == null) |
|
|
|
{ |
|
|
|
if (!string.IsNullOrWhiteSpace(message)) |
|
|
|
{ |
|
|
|
throw new ArgumentException(message, parameterName); |
|
|
|
} |
|
|
|
throw new ArgumentNullException(parameterName); |
|
|
|
} |
|
|
|
|
|
|
|
throw new ArgumentException("Value cannot be null or empty and cannot contain only blanks.", parameterName); |
|
|
|
if (string.IsNullOrWhiteSpace(value)) |
|
|
|
{ |
|
|
|
throw new ArgumentException("Must not be empty or whitespace.", parameterName); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Verifies, that the enumeration is not null and not empty.
|
|
|
|
/// Ensures that the enumeration is not null or empty.
|
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="T">The type of objects in the <paramref name="target"/></typeparam>
|
|
|
|
/// <param name="target">The target enumeration, which should be checked against being null or empty.</param>
|
|
|
|
/// <typeparam name="T">The type of objects in the <paramref name="value"/></typeparam>
|
|
|
|
/// <param name="value">The target enumeration, which should be checked against being null or empty.</param>
|
|
|
|
/// <param name="parameterName">Name of the parameter.</param>
|
|
|
|
/// <param name="message">The error message, if any to add to the exception.</param>
|
|
|
|
/// <exception cref="ArgumentNullException"><paramref name="target"/> is null.</exception>
|
|
|
|
/// <exception cref="ArgumentException"><paramref name="target"/> is empty.</exception>
|
|
|
|
public static void NotNullOrEmpty<T>(IEnumerable<T> target, string parameterName, string message = "") |
|
|
|
/// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception>
|
|
|
|
/// <exception cref="ArgumentException"><paramref name="value"/> is empty.</exception>
|
|
|
|
public static void NotNullOrEmpty<T>(IEnumerable<T> value, string parameterName) |
|
|
|
{ |
|
|
|
NotNull(target, parameterName, message); |
|
|
|
|
|
|
|
if (!target.Any()) |
|
|
|
if (value == null) |
|
|
|
{ |
|
|
|
if (!string.IsNullOrWhiteSpace(message)) |
|
|
|
{ |
|
|
|
throw new ArgumentException(message, parameterName); |
|
|
|
} |
|
|
|
throw new ArgumentNullException(parameterName); |
|
|
|
} |
|
|
|
|
|
|
|
throw new ArgumentException("Value cannot be empty.", parameterName); |
|
|
|
if (!value.Any()) |
|
|
|
{ |
|
|
|
throw new ArgumentException("Must not be empty.", parameterName); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Verifies that the specified value is less than a maximum value
|
|
|
|
/// and throws an exception if it is not.
|
|
|
|
/// Ensures that the specified value is less than a maximum value.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="value">The target value, which should be validated.</param>
|
|
|
|
/// <param name="max">The maximum value.</param>
|
|
|
|
|