using System; using System.Collections; using System.Collections.Generic; namespace AspClassic.Scripting.Utils; internal static class ContractUtils { public static void RequiresNotNull(object value, string paramName) { if (value == null) { throw new ArgumentNullException(paramName); } } public static void Requires(bool precondition) { if (!precondition) { throw new ArgumentException(Strings.MethodPreconditionViolated); } } public static void Requires(bool precondition, string paramName) { if (!precondition) { throw new ArgumentException(Strings.InvalidArgumentValue, paramName); } } public static void Requires(bool precondition, string paramName, string message) { if (!precondition) { throw new ArgumentException(message, paramName); } } public static void RequiresNotEmpty(string str, string paramName) { RequiresNotNull(str, paramName); if (str.Length == 0) { throw new ArgumentException(Strings.NonEmptyStringRequired, paramName); } } public static void RequiresNotEmpty(ICollection collection, string paramName) { RequiresNotNull(collection, paramName); if (collection.Count == 0) { throw new ArgumentException(Strings.NonEmptyCollectionRequired, paramName); } } public static void RequiresArrayRange(IList array, int offset, int count, string offsetName, string countName) { RequiresArrayRange(array.Count, offset, count, offsetName, countName); } public static void RequiresArrayRange(int arraySize, int offset, int count, string offsetName, string countName) { if (count < 0) { throw new ArgumentOutOfRangeException(countName); } if (offset < 0 || arraySize - offset < count) { throw new ArgumentOutOfRangeException(offsetName); } } public static void RequiresNotNullItems(IList array, string arrayName) { RequiresNotNull(array, arrayName); for (int i = 0; i < array.Count; i++) { if (array[i] == null) { throw ExceptionUtils.MakeArgumentItemNullException(i, arrayName); } } } public static void RequiresNotNullItems(IEnumerable collection, string collectionName) { RequiresNotNull(collection, collectionName); int num = 0; foreach (T item in collection) { if (item == null) { throw ExceptionUtils.MakeArgumentItemNullException(num, collectionName); } num++; } } public static void RequiresListRange(IList array, int offset, int count, string offsetName, string countName) { if (count < 0) { throw new ArgumentOutOfRangeException(countName); } if (offset < 0 || array.Count - offset < count) { throw new ArgumentOutOfRangeException(offsetName); } } }