|
|
@ -1,16 +1,8 @@ |
|
|
// --------------------------------------------------------------------------------------------------------------------
|
|
|
// <copyright file="Guard.cs" company="James Jackson-South">
|
|
|
// <copyright file="Guard.cs" company="James Jackson-South">
|
|
|
// Copyright (c) James Jackson-South and contributors.
|
|
|
// Copyright (c) James Jackson-South and contributors.
|
|
|
// Licensed under the Apache License, Version 2.0.
|
|
|
// Licensed under the Apache License, Version 2.0.
|
|
|
|
|
|
// </copyright>
|
|
|
// </copyright>
|
|
|
// <summary>
|
|
|
|
|
|
// Provides methods to protect against invalid parameters.
|
|
|
|
|
|
// </summary>
|
|
|
|
|
|
// --------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
using System.Runtime.CompilerServices; |
|
|
|
|
|
|
|
|
|
|
|
[assembly: InternalsVisibleTo("ImageProcessorCore.Tests")] |
|
|
|
|
|
namespace ImageProcessorCore |
|
|
namespace ImageProcessorCore |
|
|
{ |
|
|
{ |
|
|
using System; |
|
|
using System; |
|
|
@ -26,18 +18,10 @@ namespace ImageProcessorCore |
|
|
/// Verifies, that the method parameter with specified object value is not null
|
|
|
/// Verifies, that the method parameter with specified object value is not null
|
|
|
/// and throws an exception if it is found to be so.
|
|
|
/// and throws an exception if it is found to be so.
|
|
|
/// </summary>
|
|
|
/// </summary>
|
|
|
/// <param name="target">
|
|
|
/// <param name="target">The target object, which cannot be null.</param>
|
|
|
/// The target object, which cannot be null.
|
|
|
/// <param name="parameterName">The name of the parameter that is to be checked.</param>
|
|
|
/// </param>
|
|
|
/// <param name="message">The error message, if any to add to the exception.</param>
|
|
|
/// <param name="parameterName">
|
|
|
/// <exception cref="ArgumentNullException"><paramref name="target"/> is null</exception>
|
|
|
/// 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="System.ArgumentNullException">
|
|
|
|
|
|
/// <paramref name="target"/> is null
|
|
|
|
|
|
/// </exception>
|
|
|
|
|
|
public static void NotNull(object target, string parameterName, string message = "") |
|
|
public static void NotNull(object target, string parameterName, string message = "") |
|
|
{ |
|
|
{ |
|
|
if (target == null) |
|
|
if (target == null) |
|
|
@ -58,13 +42,8 @@ namespace ImageProcessorCore |
|
|
/// </summary>
|
|
|
/// </summary>
|
|
|
/// <param name="target">The target string, which should be checked against being null or empty.</param>
|
|
|
/// <param name="target">The target string, which should be checked against being null or empty.</param>
|
|
|
/// <param name="parameterName">Name of the parameter.</param>
|
|
|
/// <param name="parameterName">Name of the parameter.</param>
|
|
|
/// <exception cref="System.ArgumentNullException">
|
|
|
/// <exception cref="ArgumentNullException"><paramref name="target"/> is null.</exception>
|
|
|
/// <paramref name="target"/> is null.
|
|
|
/// <exception cref="ArgumentException"><paramref name="target"/> is empty or contains only blanks.</exception>
|
|
|
/// </exception>
|
|
|
|
|
|
/// <exception cref="ArgumentException">
|
|
|
|
|
|
/// <paramref name="target"/> is
|
|
|
|
|
|
/// empty or contains only blanks.
|
|
|
|
|
|
/// </exception>
|
|
|
|
|
|
public static void NotNullOrEmpty(string target, string parameterName) |
|
|
public static void NotNullOrEmpty(string target, string parameterName) |
|
|
{ |
|
|
{ |
|
|
if (target == null) |
|
|
if (target == null) |
|
|
@ -94,9 +73,7 @@ namespace ImageProcessorCore |
|
|
{ |
|
|
{ |
|
|
if (value.CompareTo(max) >= 0) |
|
|
if (value.CompareTo(max) >= 0) |
|
|
{ |
|
|
{ |
|
|
throw new ArgumentOutOfRangeException( |
|
|
throw new ArgumentOutOfRangeException(parameterName, $"Value must be less than {max}."); |
|
|
parameterName, |
|
|
|
|
|
$"Value must be less than {max}."); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -116,9 +93,7 @@ namespace ImageProcessorCore |
|
|
{ |
|
|
{ |
|
|
if (value.CompareTo(max) > 0) |
|
|
if (value.CompareTo(max) > 0) |
|
|
{ |
|
|
{ |
|
|
throw new ArgumentOutOfRangeException( |
|
|
throw new ArgumentOutOfRangeException(parameterName, $"Value must be less than or equal to {max}."); |
|
|
parameterName, |
|
|
|
|
|
$"Value must be less than or equal to {max}."); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -134,7 +109,7 @@ namespace ImageProcessorCore |
|
|
/// <paramref name="value"/> is less than the minimum value.
|
|
|
/// <paramref name="value"/> is less than the minimum value.
|
|
|
/// </exception>
|
|
|
/// </exception>
|
|
|
public static void MustBeGreaterThan<TValue>(TValue value, TValue min, string parameterName) |
|
|
public static void MustBeGreaterThan<TValue>(TValue value, TValue min, string parameterName) |
|
|
where TValue : IComparable<TValue> |
|
|
where TValue : IComparable<TValue> |
|
|
{ |
|
|
{ |
|
|
if (value.CompareTo(min) <= 0) |
|
|
if (value.CompareTo(min) <= 0) |
|
|
{ |
|
|
{ |
|
|
@ -156,13 +131,11 @@ namespace ImageProcessorCore |
|
|
/// <paramref name="value"/> is less than the minimum value.
|
|
|
/// <paramref name="value"/> is less than the minimum value.
|
|
|
/// </exception>
|
|
|
/// </exception>
|
|
|
public static void MustBeGreaterThanOrEqualTo<TValue>(TValue value, TValue min, string parameterName) |
|
|
public static void MustBeGreaterThanOrEqualTo<TValue>(TValue value, TValue min, string parameterName) |
|
|
where TValue : IComparable<TValue> |
|
|
where TValue : IComparable<TValue> |
|
|
{ |
|
|
{ |
|
|
if (value.CompareTo(min) < 0) |
|
|
if (value.CompareTo(min) < 0) |
|
|
{ |
|
|
{ |
|
|
throw new ArgumentOutOfRangeException( |
|
|
throw new ArgumentOutOfRangeException(parameterName, $"Value must be greater than or equal to {min}."); |
|
|
parameterName, |
|
|
|
|
|
$"Value must be greater than or equal to {min}."); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -175,17 +148,15 @@ namespace ImageProcessorCore |
|
|
/// <param name="max">The maximum value.</param>
|
|
|
/// <param name="max">The maximum value.</param>
|
|
|
/// <param name="parameterName">The name of the parameter that is to be checked.</param>
|
|
|
/// <param name="parameterName">The name of the parameter that is to be checked.</param>
|
|
|
/// <typeparam name="TValue">The type of the value.</typeparam>
|
|
|
/// <typeparam name="TValue">The type of the value.</typeparam>
|
|
|
/// <exception cref="ArgumentException">
|
|
|
/// <exception cref="System.ArgumentException">
|
|
|
/// <paramref name="value"/> is less than the minimum value of greater than the maximum value.
|
|
|
/// <paramref name="value"/> is less than the minimum value of greater than the maximum value.
|
|
|
/// </exception>
|
|
|
/// </exception>
|
|
|
public static void MustBeBetweenOrEqualTo<TValue>(TValue value, TValue min, TValue max, string parameterName) |
|
|
public static void MustBeBetweenOrEqualTo<TValue>(TValue value, TValue min, TValue max, string parameterName) |
|
|
where TValue : IComparable<TValue> |
|
|
where TValue : IComparable<TValue> |
|
|
{ |
|
|
{ |
|
|
if (value.CompareTo(min) < 0 || value.CompareTo(max) > 0) |
|
|
if (value.CompareTo(min) < 0 || value.CompareTo(max) > 0) |
|
|
{ |
|
|
{ |
|
|
throw new ArgumentOutOfRangeException( |
|
|
throw new ArgumentOutOfRangeException(parameterName, $"Value must be greater than or equal to {min} and less than or equal to {max}."); |
|
|
parameterName, |
|
|
|
|
|
$"Value must be greater than or equal to {min} and less than or equal to {max}."); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -202,7 +173,7 @@ namespace ImageProcessorCore |
|
|
/// <param name="message">
|
|
|
/// <param name="message">
|
|
|
/// The error message, if any to add to the exception.
|
|
|
/// The error message, if any to add to the exception.
|
|
|
/// </param>
|
|
|
/// </param>
|
|
|
/// <exception cref="ArgumentException">
|
|
|
/// <exception cref="System.ArgumentException">
|
|
|
/// <paramref name="target"/> is null
|
|
|
/// <paramref name="target"/> is null
|
|
|
/// </exception>
|
|
|
/// </exception>
|
|
|
public static void IsTrue(bool target, string parameterName, string message = "") |
|
|
public static void IsTrue(bool target, string parameterName, string message = "") |
|
|
@ -222,15 +193,9 @@ namespace ImageProcessorCore |
|
|
/// Verifies, that the method parameter with specified target value is false
|
|
|
/// Verifies, that the method parameter with specified target value is false
|
|
|
/// and throws an exception if it is found to be so.
|
|
|
/// and throws an exception if it is found to be so.
|
|
|
/// </summary>
|
|
|
/// </summary>
|
|
|
/// <param name="target">
|
|
|
/// <param name="target">The target value, which cannot be true.</param>
|
|
|
/// The target value, which cannot be true.
|
|
|
/// <param name="parameterName">The name of the parameter that is to be checked.</param>
|
|
|
/// </param>
|
|
|
/// <param name="message">The error message, if any to add to the exception.</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="ArgumentException">
|
|
|
/// <exception cref="ArgumentException">
|
|
|
/// <paramref name="target"/> is null
|
|
|
/// <paramref name="target"/> is null
|
|
|
/// </exception>
|
|
|
/// </exception>
|
|
|
|