namespace Kit.Helpers
{
using System;
using System.Diagnostics;
///
/// a static class that checks if parameter is invalid and raises an exception in this case
///
public static class Check
{
[DebuggerStepThrough]
public static void ArgumentIsNotNull(object argument, string name)
{
if (argument == null)
{
throw new ArgumentNullException(name);
}
}
[DebuggerStepThrough]
public static void ArgumentIsNotNull(object argument, string name, string message)
{
if (argument == null)
{
throw new ArgumentException(message, name);
}
}
[DebuggerStepThrough]
public static void IntegerArgumentPositive(Int32 value, string name)
{
if (value < 1) throw new ArgumentOutOfRangeException(name, value, string.Empty);
}
[DebuggerStepThrough]
public static void IntegerArgumentPositive(Int32? value, string name)
{
if (!value.HasValue) throw new ArgumentNullException(name);
if (value.Value < 1) throw new ArgumentOutOfRangeException(name, value.Value, string.Empty);
}
[DebuggerStepThrough]
public static void IntegerArgumentPositive(Int64 value, string name)
{
if (value < 1) throw new ArgumentOutOfRangeException(name, value, string.Empty);
}
[DebuggerStepThrough]
public static void IntegerArgumentPositive(Int64? value, string name)
{
if (!value.HasValue) throw new ArgumentNullException(name);
if (value.Value < 1) throw new ArgumentOutOfRangeException(name, value.Value, string.Empty);
}
[DebuggerStepThrough]
public static void StringArgumentIsNotNullOrEmpty(string str, string name)
{
if (string.IsNullOrEmpty(str))
throw new ArgumentNullException(name);
str = str.Trim();
if (string.IsNullOrEmpty(str))
throw new ArgumentNullException(name);
}
[DebuggerStepThrough]
public static void IsTrue(bool argument, string message)
{
if (argument == false)
{
throw new InvalidOperationException(message);
}
}
[DebuggerStepThrough]
public static void IsNotTrue(bool argument, string message)
{
if (argument == true)
{
throw new InvalidOperationException(message);
}
}
[DebuggerStepThrough]
public static void IsNotNull(object argument, string message)
{
if (argument == null)
{
throw new InvalidOperationException(message);
}
}
///
/// Checks whether argument is of specified type
/// Throws InvalidOperationException if argument is of wrong type
///
/// the argument to be checked
/// the Type
/// Implied to contain a {0} placeholder within in order to be replaced with string representation of the type param
[DebuggerStepThrough]
public static void ArgumentIsOfType(object argument, Type type, string message)
{
Check.ArgumentIsNotNull(argument, "argument");
Check.ArgumentIsNotNull(type, "type");
if (type != argument.GetType())
{
throw new InvalidOperationException(string.Format(message, type.ToString()));
}
}
[DebuggerStepThrough]
public static void StringArgumentIsNotNullOrEmpty(string str, string paramName, string message)
{
if (string.IsNullOrEmpty(str))
throw new ArgumentNullException(paramName: paramName, message: message);
str = str.Trim();
if (string.IsNullOrEmpty(str))
throw new ArgumentNullException(paramName: paramName, message: message);
}
}
}