This commit is contained in:
Jelle Luteijn 2022-05-15 11:19:49 +02:00
parent 16e76d6b31
commit 484dbfc9d9
529 changed files with 113694 additions and 0 deletions

View file

@ -0,0 +1,113 @@
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<T>(ICollection<T> collection, string paramName)
{
RequiresNotNull(collection, paramName);
if (collection.Count == 0)
{
throw new ArgumentException(Strings.NonEmptyCollectionRequired, paramName);
}
}
public static void RequiresArrayRange<T>(IList<T> 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<T>(IList<T> 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<T>(IEnumerable<T> 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);
}
}
}