//-----------------------------------------------------------------------
//
// Copyright (c) Outercurve Foundation. All rights reserved.
//
//-----------------------------------------------------------------------
namespace DotNetOpenAuth {
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Linq;
using System.Text;
using DotNetOpenAuth.Messaging;
///
/// Argument validation checks that throw some kind of ArgumentException when they fail (unless otherwise noted).
///
internal static class Requires {
///
/// Validates that a given parameter is not null.
///
/// The type of the parameter
/// The value.
/// Name of the parameter.
#if !CLR4
[ContractArgumentValidator]
#endif
[Pure, DebuggerStepThrough]
internal static void NotNull(T value, string parameterName) where T : class {
if (value == null) {
throw new ArgumentNullException(parameterName);
}
Contract.EndContractBlock();
}
///
/// Validates that a parameter is not null or empty.
///
/// The value.
/// Name of the parameter.
#if !CLR4
[ContractArgumentValidator]
#endif
[Pure, DebuggerStepThrough]
internal static void NotNullOrEmpty(string value, string parameterName) {
NotNull(value, parameterName);
True(value.Length > 0, parameterName, Strings.EmptyStringNotAllowed);
Contract.EndContractBlock();
}
///
/// Validates that an array is not null or empty.
///
/// The type of the elements in the sequence.
/// The value.
/// Name of the parameter.
#if !CLR4
[ContractArgumentValidator]
#endif
[Pure, DebuggerStepThrough]
internal static void NotNullOrEmpty(IEnumerable value, string parameterName) {
NotNull(value, parameterName);
True(value.Any(), parameterName, Strings.InvalidArgument);
Contract.EndContractBlock();
}
///
/// Validates that an argument is either null or is a sequence with no null elements.
///
/// The type of elements in the sequence.
/// The sequence.
/// Name of the parameter.
#if !CLR4
[ContractArgumentValidator]
#endif
[Pure, DebuggerStepThrough]
internal static void NullOrWithNoNullElements(IEnumerable sequence, string parameterName) where T : class {
if (sequence != null) {
if (sequence.Any(e => e == null)) {
throw new ArgumentException(MessagingStrings.SequenceContainsNullElement, parameterName);
}
}
}
///
/// Validates some expression describing the acceptable range for an argument evaluates to true.
///
/// The expression that must evaluate to true to avoid an .
/// Name of the parameter.
/// The message to include with the exception.
#if !CLR4
[ContractArgumentValidator]
#endif
[Pure, DebuggerStepThrough]
internal static void InRange(bool condition, string parameterName, string message = null) {
if (!condition) {
throw new ArgumentOutOfRangeException(parameterName, message);
}
Contract.EndContractBlock();
}
///
/// Validates some expression describing the acceptable condition for an argument evaluates to true.
///
/// The expression that must evaluate to true to avoid an .
/// Name of the parameter.
/// The message to include with the exception.
#if !CLR4
[ContractArgumentValidator]
#endif
[Pure, DebuggerStepThrough]
internal static void True(bool condition, string parameterName = null, string message = null) {
if (!condition) {
throw new ArgumentException(message ?? Strings.InvalidArgument, parameterName);
}
Contract.EndContractBlock();
}
///
/// Validates some expression describing the acceptable condition for an argument evaluates to true.
///
/// The expression that must evaluate to true to avoid an .
/// Name of the parameter.
/// The unformatted message.
/// Formatting arguments.
#if !CLR4
[ContractArgumentValidator]
#endif
[Pure, DebuggerStepThrough]
internal static void True(bool condition, string parameterName, string unformattedMessage, params object[] args) {
if (!condition) {
throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, unformattedMessage, args), parameterName);
}
Contract.EndContractBlock();
}
///
/// Validates some expression describing the acceptable condition for an argument evaluates to true.
///
/// The expression that must evaluate to true to avoid an .
#if !CLR4
[ContractArgumentValidator]
#endif
[Pure, DebuggerStepThrough]
internal static void ValidState(bool condition) {
if (!condition) {
throw new InvalidOperationException();
}
Contract.EndContractBlock();
}
///
/// Validates some expression describing the acceptable condition for an argument evaluates to true.
///
/// The expression that must evaluate to true to avoid an .
/// The message to include with the exception.
#if !CLR4
[ContractArgumentValidator]
#endif
[Pure, DebuggerStepThrough]
internal static void ValidState(bool condition, string message) {
if (!condition) {
throw new InvalidOperationException(message);
}
Contract.EndContractBlock();
}
///
/// Validates some expression describing the acceptable condition for an argument evaluates to true.
///
/// The expression that must evaluate to true to avoid an .
/// The unformatted message.
/// Formatting arguments.
#if !CLR4
[ContractArgumentValidator]
#endif
[Pure, DebuggerStepThrough]
internal static void ValidState(bool condition, string unformattedMessage, params object[] args) {
if (!condition) {
throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, unformattedMessage, args));
}
Contract.EndContractBlock();
}
///
/// Validates that some argument describes a type that is or derives from a required type.
///
/// The type that the argument must be or derive from.
/// The type given in the argument.
/// Name of the parameter.
#if !CLR4
[ContractArgumentValidator]
#endif
[Pure, DebuggerStepThrough]
internal static void NotNullSubtype(Type type, string parameterName) {
NotNull(type, parameterName);
True(typeof(T).IsAssignableFrom(type), parameterName, MessagingStrings.UnexpectedType, typeof(T).FullName, type.FullName);
Contract.EndContractBlock();
}
///
/// Validates some expression describing the acceptable condition for an argument evaluates to true.
///
/// The expression that must evaluate to true to avoid an .
/// The message.
#if !CLR4
[ContractArgumentValidator]
#endif
[Pure, DebuggerStepThrough]
internal static void Format(bool condition, string message) {
if (!condition) {
throw new FormatException(message);
}
Contract.EndContractBlock();
}
///
/// Throws an if a condition does not evaluate to true.
///
/// The expression that must evaluate to true to avoid an .
/// The message.
[Pure, DebuggerStepThrough]
internal static void Support(bool condition, string message) {
if (!condition) {
throw new NotSupportedException(message);
}
}
///
/// Throws an
///
/// Name of the parameter.
/// The message.
[Pure, DebuggerStepThrough]
internal static void Fail(string parameterName, string message) {
throw new ArgumentException(message, parameterName);
}
}
}