// Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; namespace SixLabors { /// /// Provides methods to protect against invalid parameters. /// [DebuggerStepThrough] internal static class Guard { /// /// 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 `target` span has the length of 'minLength', or longer. /// /// The element type of the spans /// The target span. /// The minimum length. /// The name of the parameter that is to be checked. /// /// The length of is less than . /// public static void MustBeSizedAtLeast(T[] value, int minLength, string parameterName) { if (value.Length < minLength) { throw new ArgumentException($"The size must be at least {minLength}.", parameterName); } } } }