// --------------------------------------------------------------------------------------------------------------------
//
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
//
//
// Provides methods to protect against invalid parameters.
//
// --------------------------------------------------------------------------------------------------------------------
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("ImageProcessorCore.Tests")]
namespace ImageProcessorCore
{
using System;
using System.Diagnostics;
///
/// Provides methods to protect against invalid parameters.
///
[DebuggerStepThrough]
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("Value 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 MustBeLessThan(TValue value, TValue max, string parameterName)
where TValue : IComparable
{
if (value.CompareTo(max) >= 0)
{
throw new ArgumentOutOfRangeException(
parameterName,
$"Value must be less than {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 MustBeLessThanOrEqualTo(TValue value, TValue max, string parameterName)
where TValue : IComparable
{
if (value.CompareTo(max) > 0)
{
throw new ArgumentOutOfRangeException(
parameterName,
$"Value must be less than or equal to {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 MustBeGreaterThan(TValue value, TValue min, string parameterName)
where TValue : IComparable
{
if (value.CompareTo(min) <= 0)
{
throw new ArgumentOutOfRangeException(
parameterName,
$"Value must be greater than {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 MustBeGreaterThanOrEqualTo(TValue value, TValue min, string parameterName)
where TValue : IComparable
{
if (value.CompareTo(min) < 0)
{
throw new ArgumentOutOfRangeException(
parameterName,
$"Value must be greater than or equal to {min}.");
}
}
///
/// Verifies that the specified value is greater than or equal to a minimum value and less than
/// or equal to a maximum value and throws an exception if it is not.
///
/// The target value, which should be validated.
/// The minimum value.
/// The maximum value.
/// The name of the parameter that is to be checked.
/// The type of the value.
///
/// is less than the minimum value of greater than the maximum value.
///
public static void MustBeBetweenOrEqualTo(TValue value, TValue min, TValue max, string parameterName)
where TValue : IComparable
{
if (value.CompareTo(min) < 0 || value.CompareTo(max) > 0)
{
throw new ArgumentOutOfRangeException(
parameterName,
$"Value must be greater than or equal to {min} and less than or equal to {max}.");
}
}
///
/// Verifies, that the method parameter with specified target value is true
/// and throws an exception if it is found to be so.
///
///
/// The target value, which cannot be false.
///
///
/// 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 IsTrue(bool target, string parameterName, string message = "")
{
if (!target)
{
if (string.IsNullOrWhiteSpace(message))
{
throw new ArgumentException(parameterName, message);
}
throw new ArgumentException(parameterName);
}
}
///
/// Verifies, that the method parameter with specified target value is false
/// and throws an exception if it is found to be so.
///
///
/// The target value, which cannot be true.
///
///
/// 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 IsFalse(bool target, string parameterName, string message = "")
{
if (target)
{
if (string.IsNullOrWhiteSpace(message))
{
throw new ArgumentException(parameterName, message);
}
throw new ArgumentException(parameterName);
}
}
}
}