Argument
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace Azure.Storage.Common
{
internal static class Argument
{
public static void AssertNotNull<T>(T value, string name)
{
if (value == null)
throw new ArgumentNullException(name);
}
public static void AssertNotNull<T>(T? value, string name) where T : struct
{
if (!value.HasValue)
throw new ArgumentNullException(name);
}
public static void AssertNotNullOrEmpty<T>(IEnumerable<T> value, string name)
{
if (value == null)
throw new ArgumentNullException(name);
ICollection<T> collection = value as ICollection<T>;
if (collection != null && collection.Count == 0)
throw new ArgumentException("Value cannot be an empty collection.", name);
ICollection collection2 = value as ICollection;
if (collection2 != null && collection2.Count == 0)
throw new ArgumentException("Value cannot be an empty collection.", name);
using (IEnumerator<T> enumerator = value.GetEnumerator()) {
if (!enumerator.MoveNext())
throw new ArgumentException("Value cannot be an empty collection.", name);
}
}
public static void AssertNotNullOrEmpty(string value, string name)
{
if (value == null)
throw new ArgumentNullException(name);
if (value.Length == 0)
throw new ArgumentException("Value cannot be an empty string.", name);
}
public static void AssertNotNullOrWhiteSpace(string value, string name)
{
if (value == null)
throw new ArgumentNullException(name);
if (string.IsNullOrWhiteSpace(value))
throw new ArgumentException("Value cannot be empty or contain only white-space characters.", name);
}
public static void AssertNotDefault<T>(ref T value, string name) where T : struct, IEquatable<T>
{
if (((IEquatable<T>)value).Equals(default(T)))
throw new ArgumentException("Value cannot be empty.", name);
}
public static void AssertInRange<[System.Runtime.CompilerServices.Nullable(1)] T>(T value, T minimum, T maximum, string name) where T : IComparable<T>
{
if (((IComparable<T>)minimum).CompareTo(value) > 0)
throw new ArgumentOutOfRangeException(name, "Value is less than the minimum allowed.");
if (((IComparable<T>)maximum).CompareTo(value) < 0)
throw new ArgumentOutOfRangeException(name, "Value is greater than the maximum allowed.");
}
public static void AssertEnumDefined(Type enumType, object value, string name)
{
if (!Enum.IsDefined(enumType, value))
throw new ArgumentException("Value not defined for " + enumType.FullName + ".", name);
}
public static T CheckNotNull<T>(T value, string name) where T : class
{
AssertNotNull(value, name);
return value;
}
public static string CheckNotNullOrEmpty(string value, string name)
{
AssertNotNullOrEmpty(value, name);
return value;
}
public static void AssertNull<T>(T value, string name, string message = null)
{
if (value != null)
throw new ArgumentException(message ?? "Value must be null.", name);
}
}
}