// --------------------------------------------------------------------------------------------------------------------
//
// Copyright © James South and contributors.
// Licensed under the Apache License, Version 2.0.
//
//
// Provides methods to protect against invalid parameters.
//
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor
{
using System;
using System.Globalization;
///
/// Provides methods to protect against invalid parameters.
///
internal static class Guard
{
///
/// Verifies, that the method parameter with specified object value is not null
/// and throws an exception if it is found to be so.
///
///
/// The target object, which cannot be null.
///
///
/// The name of the parameter that is to be checked.
///
///
/// The error message, if any to add to the exception.
///
///
/// is null
///
public static void NotNull(object target, string parameterName, string message = "")
{
if (target == null)
{
if (string.IsNullOrWhiteSpace(message))
{
throw new ArgumentNullException(parameterName, message);
}
throw new ArgumentNullException(parameterName);
}
}
///
/// 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.
///
/// The target string, which should be checked against being null or empty.
/// Name of the parameter.
///
/// is null.
///
///
/// is
/// empty or contains only blanks.
///
public static void NotNullOrEmpty(string target, string parameterName)
{
if (target == null)
{
throw new ArgumentNullException(parameterName);
}
if (string.IsNullOrWhiteSpace(target))
{
throw new ArgumentException("String parameter cannot be null or empty and cannot contain only blanks.", parameterName);
}
}
///
/// Verifies that the specified value is less than a maximum value
/// and throws an exception if it is not.
///
/// The target value, which should be validated.
/// The maximum value.
/// The name of the parameter that is to be checked.
/// The type of the value.
///
/// is greater than the maximum value.
///
public static void LessThan(TValue value, TValue max, string parameterName) where TValue : IComparable
{
if (value.CompareTo(max) >= 0)
{
throw new ArgumentOutOfRangeException(
parameterName,
string.Format(CultureInfo.CurrentCulture, "Value must be less than {0}", max));
}
}
///
/// Verifies that the specified value is less than or equal to a maximum value
/// and throws an exception if it is not.
///
/// The target value, which should be validated.
/// The maximum value.
/// The name of the parameter that is to be checked.
/// The type of the value.
///
/// is greater than the maximum value.
///
public static void LessEquals(TValue value, TValue max, string parameterName) where TValue : IComparable
{
if (value.CompareTo(max) > 0)
{
throw new ArgumentOutOfRangeException(
parameterName,
string.Format(CultureInfo.CurrentCulture, "Value must be less than or equal to {0}", max));
}
}
///
/// Verifies that the specified value is greater than a minimum value
/// and throws an exception if it is not.
///
/// The target value, which should be validated.
/// The minimum value.
/// The name of the parameter that is to be checked.
/// The type of the value.
///
/// is less than the minimum value.
///
public static void GreaterThan(TValue value, TValue min, string parameterName) where TValue : IComparable
{
if (value.CompareTo(min) <= 0)
{
throw new ArgumentOutOfRangeException(
parameterName,
string.Format(CultureInfo.CurrentCulture, "Value must be greater than {0}", min));
}
}
///
/// Verifies that the specified value is greater than or equal to a minimum value
/// and throws an exception if it is not.
///
/// The target value, which should be validated.
/// The minimum value.
/// The name of the parameter that is to be checked.
/// The type of the value.
///
/// is less than the minimum value.
///
public static void GreaterEquals(TValue value, TValue min, string parameterName) where TValue : IComparable
{
if (value.CompareTo(min) < 0)
{
throw new ArgumentOutOfRangeException(
parameterName,
string.Format(CultureInfo.CurrentCulture, "Value must be greater than or equal to {0}", min));
}
}
}
}