1
0
mirror of https://github.com/ncblakely/GiantsTools synced 2024-11-24 07:05:37 +01:00
GiantsTools/Giants.Services/Utility/ArgumentUtility.cs

143 lines
5.3 KiB
C#
Raw Normal View History

2020-08-09 02:52:26 +02:00
namespace Giants.Services
{
using System;
using System.Collections;
using System.ComponentModel;
using System.Runtime.CompilerServices;
[EditorBrowsable(EditorBrowsableState.Never)]
public static class ArgumentUtility
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2022-09-05 22:42:41 +02:00
public static void CheckForNull(object var, [CallerArgumentExpression("var")] string varName = null)
2020-08-09 02:52:26 +02:00
{
if (var == null)
throw new ArgumentNullException(varName);
}
public static void CheckStringForNullOrEmpty(
string stringVar,
2022-09-05 22:42:41 +02:00
[CallerArgumentExpression("stringVar")] string stringVarName = null)
2020-08-09 02:52:26 +02:00
{
2022-09-05 22:42:41 +02:00
ArgumentUtility.CheckStringForNullOrEmpty(stringVar, false, stringVarName);
2020-08-09 02:52:26 +02:00
}
2022-09-05 22:42:41 +02:00
public static void CheckForNonnegativeInt(int var, [CallerArgumentExpression("var")] string varName = null)
2020-08-09 02:52:26 +02:00
{
if (var < 0)
throw new ArgumentOutOfRangeException(varName);
}
2022-09-05 22:42:41 +02:00
public static void CheckForNonPositiveInt(int var, [CallerArgumentExpression("var")] string varName = null)
2020-08-09 02:52:26 +02:00
{
if (var <= 0)
throw new ArgumentOutOfRangeException(varName);
}
public static void CheckStringForNullOrEmpty(
string stringVar,
2022-09-05 22:42:41 +02:00
bool trim,
[CallerArgumentExpression("stringVar")] string stringVarName = null)
2020-08-09 02:52:26 +02:00
{
ArgumentUtility.CheckForNull((object)stringVar, stringVarName);
if (trim)
stringVar = stringVar.Trim();
if (stringVar.Length == 0)
throw new ArgumentException("Empty string not allowed.", stringVarName);
}
public static void CheckStringLength(
string stringVar,
int maxLength,
2022-09-05 22:42:41 +02:00
int minLength = 0,
[CallerArgumentExpression("stringVar")] string stringVarName = null)
2020-08-09 02:52:26 +02:00
{
ArgumentUtility.CheckForNull((object)stringVar, stringVarName);
if (stringVar.Length < minLength || stringVar.Length > maxLength)
throw new ArgumentException("String length not allowed.", stringVarName);
}
public static void CheckEnumerableForNullOrEmpty(
IEnumerable enumerable,
2022-09-05 22:42:41 +02:00
[CallerArgumentExpression("enumerable")] string enumerableName = null)
2020-08-09 02:52:26 +02:00
{
ArgumentUtility.CheckForNull((object)enumerable, enumerableName);
if (!enumerable.GetEnumerator().MoveNext())
throw new ArgumentException("Collection cannot be null or empty.", enumerableName);
}
public static void CheckEnumerableForNullElement(
IEnumerable enumerable,
2022-09-05 22:42:41 +02:00
[CallerArgumentExpression("enumerable")] string enumerableName = null)
2020-08-09 02:52:26 +02:00
{
ArgumentUtility.CheckForNull((object)enumerable, enumerableName);
foreach (object obj in enumerable)
{
if (obj == null)
throw new ArgumentException("NullElementNotAllowedInCollection", enumerableName);
}
}
2022-09-05 22:42:41 +02:00
public static void CheckForEmptyGuid(Guid guid, [CallerArgumentExpression("guid")] string varName = null)
2020-08-09 02:52:26 +02:00
{
if (guid.Equals(Guid.Empty))
throw new ArgumentException("EmptyGuidNotAllowed", varName);
}
public static void CheckBoundsInclusive(
int value,
int minValue,
int maxValue,
2022-09-05 22:42:41 +02:00
[CallerArgumentExpression("value")] string varName = null)
2020-08-09 02:52:26 +02:00
{
if (value < minValue || value > maxValue)
throw new ArgumentOutOfRangeException(varName, "ValueOutOfRange");
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void CheckForOutOfRange<T>(
T var,
2022-09-05 22:42:41 +02:00
T minimum,
[CallerArgumentExpression("var")] string varName = null)
2020-08-09 02:52:26 +02:00
where T : IComparable<T>
{
ArgumentUtility.CheckForNull((object)var, varName);
if (var.CompareTo(minimum) < 0)
throw new ArgumentOutOfRangeException(varName, (object)var, "OutOfRange");
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void CheckForOutOfRange(
int var,
int minimum,
2022-09-05 22:42:41 +02:00
int maximum,
[CallerArgumentExpression("var")] string varName = null)
2020-08-09 02:52:26 +02:00
{
if (var < minimum || var > maximum)
throw new ArgumentOutOfRangeException(varName, (object)var, "OutOfRange");
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void CheckForOutOfRange(
long var,
long minimum,
2022-09-05 22:42:41 +02:00
long maximum,
[CallerArgumentExpression("var")] string varName = null)
2020-08-09 02:52:26 +02:00
{
if (var < minimum || var > maximum)
throw new ArgumentOutOfRangeException(varName, (object)var, "OutOfRange");
}
public static void CheckForDateTimeRange(
DateTime var,
DateTime minimum,
2022-09-05 22:42:41 +02:00
DateTime maximum,
[CallerArgumentExpression("var")] string varName = null)
2020-08-09 02:52:26 +02:00
{
if (var < minimum || var > maximum)
throw new ArgumentOutOfRangeException(varName, (object)var, "OutOfRange");
}
}
}