#region Copyright 2008-2014 by Roger Knapp, Licensed under the Apache License, Version 2.0 /* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #endregion using System; using System.Reflection; /// /// provides a set of runtime validations for inputs /// [System.Diagnostics.DebuggerNonUserCode] static partial class Check { /// /// Verifies that the condition is true and if it fails constructs the specified type of /// exception and throws. /// public static void Assert(bool condition) where TException : Exception, new() { if (!condition) throw new TException(); } /// /// Verifies that the condition is true and if it fails constructs the specified type of /// exception with any arguments provided and throws. /// public static void Assert(bool condition, string message) where TException : Exception, new() { if (!condition) { ConstructorInfo ci = typeof (TException).GetConstructor(new Type[] {typeof (string)}); if (ci != null) { TException e = (TException) ci.Invoke(new object[] {message}); throw e; } throw new TException(); } } /// /// Used to delay creation of the excpetion until the condition fails. /// public delegate Exception ExceptionBuilder(); /// /// Verifies that the condition is true and if it fails throws the execption returned /// by fnExceptionBuilder() /// public static void Assert(bool condition, ExceptionBuilder fnExceptionBuilder) { if (!condition) { throw fnExceptionBuilder(); } } /// /// Verifies that the condition is true and if it fails constructs the specified type of /// exception with any arguments provided and throws. /// public static void Assert(bool condition, string message, Exception innerException) where TException : Exception, new() { if (!condition) { ConstructorInfo ci = typeof (TException).GetConstructor(new Type[] {typeof (string), typeof (Exception)}); if (ci != null) { TException e = (TException) ci.Invoke(new object[] {message, innerException}); throw e; } throw new TException(); } } /// /// Verifies that value is not null and returns the value or throws ArgumentNullException /// public static T NotNull(T value) { if (value == null) throw new ArgumentNullException(); return value; } /// /// Verfies that the string is not null and not empty and returns the string. /// throws ArgumentNullException, ArgumentOutOfRangeException /// public static string NotEmpty(string value) { if (value == null) throw new ArgumentNullException(); if (value.Length == 0) throw new ArgumentOutOfRangeException(); return value; } /// /// Verfies that the Guid is not empty. /// throws ArgumentOutOfRangeException /// public static Guid NotEmpty(Guid value) { if (value == Guid.Empty) throw new ArgumentOutOfRangeException(); return value; } /// /// Verfies that the collection is not null and not empty and returns the collection. /// throws ArgumentNullException, ArgumentOutOfRangeException /// public static T NotEmpty(T value) where T : System.Collections.IEnumerable { if (value == null) throw new ArgumentNullException(); if (!value.GetEnumerator().MoveNext()) throw new ArgumentOutOfRangeException(); return value; } /// /// Verifies that the two values are the same /// throws ArgumentException /// public static void IsEqual(T a, T b) where T : IEquatable { if (false == a.Equals(b)) throw new ArgumentException(); } /// /// Verifies that the two values are NOT the same /// throws ArgumentException /// public static void NotEqual(T a, T b) where T : IEquatable { if (true == a.Equals(b)) throw new ArgumentException(); } /// /// Verifies that the array is not empty and has at least min, but not more than max items. /// throws ArgumentNullExcpetion /// throws ArgumentOutOfRangeException /// public static T[] ArraySize(T[] value, int min, int max) { if (value == null) throw new ArgumentNullException(); if (value.Length < min || value.Length > max) throw new ArgumentOutOfRangeException(); return value; } /// /// Verifies that the value is min, max, or between the two. /// throws ArgumentOutOfRangeException /// public static T InRange(T value, T min, T max) where T : IComparable { if (value == null) throw new ArgumentNullException(); if (value.CompareTo(min) < 0) throw new ArgumentOutOfRangeException(); if (value.CompareTo(max) > 0) throw new ArgumentOutOfRangeException(); return value; } /// /// Returns (T)value if the object provided can be assinged to a variable of type T /// throws ArgumentException /// public static T IsAssignable(object value) { return (T) IsAssignable(typeof (T), value); } /// /// Returns value if the object provided can be assinged to a variable of type toType /// throws ArgumentException /// public static object IsAssignable(Type toType, object fromValue) { Check.NotNull(toType); if (fromValue == null) { if (toType.IsValueType) throw new ArgumentException(String.Format("Can not set value of type {0} to null.", toType)); } else IsAssignable(toType, fromValue.GetType()); return fromValue; } /// /// Throws ArgumentException if the type fromType cannot be assigned to variable of type toType /// public static void IsAssignable(Type toType, Type fromType) { if (!Check.NotNull(toType).IsAssignableFrom(Check.NotNull(fromType))) throw new ArgumentException(String.Format("Can not set value of type {0} to a value of type {1}.", toType, fromType)); } }