diff options
Diffstat (limited to 'src/DotNetOpenAuth.Core/Assumes.cs')
-rw-r--r-- | src/DotNetOpenAuth.Core/Assumes.cs | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/src/DotNetOpenAuth.Core/Assumes.cs b/src/DotNetOpenAuth.Core/Assumes.cs new file mode 100644 index 0000000..67205a2 --- /dev/null +++ b/src/DotNetOpenAuth.Core/Assumes.cs @@ -0,0 +1,97 @@ +//----------------------------------------------------------------------- +// <copyright file="Assumes.cs" company="Andrew Arnott"> +// Copyright (c) Andrew Arnott. All rights reserved. +// </copyright> +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth { + using System; + using System.Collections.Generic; + using System.Diagnostics; + using System.Diagnostics.Contracts; + using System.Linq; + using System.Text; + + /// <summary> + /// Internal state consistency checks that throw an internal error exception when they fail. + /// </summary> + internal static class Assumes { + /// <summary> + /// Validates some expression describing the acceptable condition evaluates to true. + /// </summary> + /// <param name="condition">The expression that must evaluate to true to avoid an internal error exception.</param> + /// <param name="message">The message to include with the exception.</param> + [Pure, DebuggerStepThrough] + internal static void True(bool condition, string message = null) { + if (!condition) { + Fail(message); + } + } + + /// <summary> + /// Validates some expression describing the acceptable condition evaluates to true. + /// </summary> + /// <param name="condition">The expression that must evaluate to true to avoid an internal error exception.</param> + /// <param name="unformattedMessage">The unformatted message.</param> + /// <param name="args">Formatting arguments.</param> + [Pure, DebuggerStepThrough] + internal static void True(bool condition, string unformattedMessage, params object[] args) { + if (!condition) { + Fail(String.Format(unformattedMessage, args)); + } + } + + /// <summary> + /// Throws an internal error exception. + /// </summary> + /// <param name="message">The message.</param> + [Pure, DebuggerStepThrough] + internal static void Fail(string message = null) { + if (message != null) { + throw new InternalErrorException(message); + } else { + throw new InternalErrorException(); + } + } + + /// <summary> + /// An internal error exception that should never be caught. + /// </summary> + [Serializable] + private class InternalErrorException : Exception { + /// <summary> + /// Initializes a new instance of the <see cref="InternalErrorException"/> class. + /// </summary> + internal InternalErrorException() { + } + + /// <summary> + /// Initializes a new instance of the <see cref="InternalErrorException"/> class. + /// </summary> + /// <param name="message">The message.</param> + internal InternalErrorException(string message) : base(message) { + } + + /// <summary> + /// Initializes a new instance of the <see cref="InternalErrorException"/> class. + /// </summary> + /// <param name="message">The message.</param> + /// <param name="inner">The inner exception.</param> + internal InternalErrorException(string message, Exception inner) : base(message, inner) { + } + + /// <summary> + /// Initializes a new instance of the <see cref="InternalErrorException"/> class. + /// </summary> + /// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo"/> that holds the serialized object data about the exception being thrown.</param> + /// <param name="context">The <see cref="T:System.Runtime.Serialization.StreamingContext"/> that contains contextual information about the source or destination.</param> + /// <exception cref="T:System.ArgumentNullException">The <paramref name="info"/> parameter is null. </exception> + /// <exception cref="T:System.Runtime.Serialization.SerializationException">The class name is null or <see cref="P:System.Exception.HResult"/> is zero (0). </exception> + protected InternalErrorException( + System.Runtime.Serialization.SerializationInfo info, + System.Runtime.Serialization.StreamingContext context) + : base(info, context) { + } + } + } +} |