using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using System.Collections;
using System.Diagnostics;
namespace Cosmos.Kernel
{
///
/// Common guard clauses
///
public static class Guard
{
///
/// Checks a string argument to ensure it isn't null or empty
///
/// The argument value to check.
/// The name of the argument.
[DebuggerStepThrough]
public static void ArgumentNotNullOrEmptyString(string argumentValue, string argumentName)
{
ArgumentNotNull(argumentValue, argumentName);
if (argumentValue.Length == 0)
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "String cannot be empty", argumentName));
}
///
/// Checks an argument to ensure it isn't null
///
/// The argument value to check.
/// The name of the argument.
[DebuggerStepThrough]
public static void ArgumentNotNull(object argumentValue, string argumentName)
{
if (argumentValue == null)
throw new ArgumentNullException(argumentName + " Is null" );
}
///
/// Checks an Enum argument to ensure that its value is defined by the specified Enum type.
///
/// The Enum type the value should correspond to.
/// The value to check for.
/// The name of the argument holding the value.
[DebuggerStepThrough]
public static void EnumValueIsDefined(Type enumType, object value, string argumentName)
{
if (Enum.IsDefined(enumType, value) == false)
throw new ArgumentException(String.Format(CultureInfo.CurrentCulture,
"Invalid enum value" ,
argumentName, enumType.ToString()));
}
///
/// Verifies that an argument type is assignable from the provided type (meaning
/// interfaces are implemented, or classes exist in the base class hierarchy).
///
/// The argument type.
/// The type it must be assignable from.
/// The argument name.
[DebuggerStepThrough]
public static void TypeIsAssignableFromType(Type assignee, Type providedType, string argumentName)
{
if (!providedType.IsAssignableFrom(assignee))
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,
"Type not compatible", assignee, providedType), argumentName);
}
[DebuggerStepThrough]
public static void CollectionNotNullOrEmpty(ICollection collection ,string argumentName)
{
ArgumentNotNull(collection, argumentName);
if (collection.Count == 0)
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "Collection Cannot Be Null Or Empty", argumentName));
}
[DebuggerStepThrough]
public static void ArrayNotNullOrEmpty(Array collection, string argumentName)
{
ArgumentNotNull(collection, argumentName);
if (collection.Length == 0)
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "Collection Cannot Be Null Or Empty", argumentName));
}
///
/// If the 'truth' isn't true, throw an empty CommonException.
///
/// The 'truth' to evaluate.
[DebuggerStepThrough]
public static void Require(bool truth)
{
if (!truth)
{
throw new Exception(string.Empty);
}
}
///
/// If the 'truth' isn't true, throw a CommonException with the provided message.
///
/// The 'truth' to evaluate.
/// The contents of the CommonException thrown if the 'truth' is false.
[DebuggerStepThrough]
public static void Require(bool truth, string message)
{
ArgumentNotNullOrEmptyString(message, "message");
if (!truth)
{
throw new Exception(message);
}
}
///
/// If the 'truth' isn't true, throw the provided exception.
///
/// The 'truth' to evaluate.
/// The Exception thrown if the 'truth' is false.
[DebuggerStepThrough]
public static void Require(bool truth, Exception e)
{
ArgumentNotNull(e, "e");
if (!truth)
{
throw e;
}
}
/////
///// Throws an if the
///// provided string is null.
///// Throws an if the
///// provided string is empty.
/////
///// The object to test for null and empty.
///// The string for the ArgumentException parameter, if thrown.
//[DebuggerStepThrough]
//public static void RequireNotNullOrEmpty(string stringParameter, string parameterName)
//{
// if (stringParameter == null)
// {
// throw new ArgumentNullException(parameterName);
// }
// else if (stringParameter.Length == 0)
// {
// throw new ArgumentOutOfRangeException(parameterName);
// }
//}
/////
///// Throws an if the
///// provided object is null.
/////
///// The object to test for null.
///// The string for the ArgumentNullException parameter, if thrown.
//[DebuggerStepThrough]
//public static void RequireNotNull(object obj, string parameterName)
//{
// if (obj == null)
// {
// throw new ArgumentNullException(parameterName);
// }
//}
///
/// Throws an if the provided truth is false.
///
/// The value assumed to be true.
/// The string for , if thrown.
[DebuggerStepThrough]
public static void RequireArgument(bool truth, string parameterName)
{
Guard.ArgumentNotNullOrEmptyString(parameterName, "parameterName");
if (!truth)
{
throw new ArgumentException(parameterName);
}
}
///
/// Throws an if the provided truth is false.
///
/// The value assumed to be true.
/// The paramName for the , if thrown.
/// The message for , if thrown.
[DebuggerStepThrough]
public static void RequireArgument(bool truth, string paramName, string message)
{
Guard.ArgumentNotNullOrEmptyString(paramName, "paramName");
Guard.ArgumentNotNullOrEmptyString(message, "message");
if (!truth)
{
throw new ArgumentException(message, paramName);
}
}
///
/// Throws an if the provided truth is false.
///
/// The value assumed to be true.
/// The string for , if thrown.
[DebuggerStepThrough]
public static void RequireArgumentRange(bool truth, string parameterName)
{
Guard.ArgumentNotNullOrEmptyString(parameterName, "parameterName");
if (!truth)
{
throw new ArgumentOutOfRangeException(parameterName);
}
}
///
/// Throws an if the provided truth is false.
///
/// The value assumed to be true.
/// The paramName for the , if thrown.
/// The message for , if thrown.
[DebuggerStepThrough]
public static void RequireArgumentRange(bool truth, string paramName, string message)
{
Guard.ArgumentNotNullOrEmptyString(paramName, "paramName");
Guard.ArgumentNotNullOrEmptyString(message, "message");
if (!truth)
{
throw new ArgumentOutOfRangeException(message, paramName);
}
}
} // Guard
}