PageRenderTime 48ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/Trunk/Sources/MvcEngine.Core/Utils/Guard.cs

#
C# | 230 lines | 87 code | 17 blank | 126 comment | 20 complexity | 455c47d13e9705e8bb24d9dc97c394e9 MD5 | raw file
Possible License(s): LGPL-2.1
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5. using System.Globalization;
  6. namespace MvcEngine.Core.Helpers
  7. {
  8. /// <summary>
  9. /// This utility class can be used to easily check parameter input and throw exceptions if they do not meet the set
  10. /// expectations.
  11. /// </summary>
  12. public class Guard
  13. {
  14. /// <summary>
  15. /// Private constructor to prevent creating an instance of this class.
  16. /// </summary>
  17. private Guard()
  18. {
  19. }
  20. /// <summary>
  21. /// Throws an <see cref="ArgumentOutOfRangeException"/> if the given parameter is a negative number.
  22. /// </summary>
  23. /// <typeparam name="T">The type of value to check. Should implement <see cref="IComparable{T}"/> interface.</typeparam>
  24. /// <param name="value">The value to check.</param>
  25. /// <param name="message">Exception message to give when value is invalid.</param>
  26. /// <param name="parameterName">Name of the parameter.</param>
  27. /// <exception cref="ArgumentOutOfRangeException">
  28. /// The value is less than zero.
  29. /// </exception>
  30. public static void ThrowIfNegative<T>(T value, string message, string parameterName)
  31. where T : IComparable<T>
  32. {
  33. ThrowIfLessThan(value, default(T), message, parameterName);
  34. }
  35. /// <summary>
  36. /// Throws an <see cref="ArgumentOutOfRangeException"/> if the given parameter is a number below the given minimun value.
  37. /// </summary>
  38. /// <typeparam name="T">The type of value to check. Should implement <see cref="IComparable{T}"/> interface.</typeparam>
  39. /// <param name="value">The value to check.</param>
  40. /// <param name="minValue">The minimum value allowed.</param>
  41. /// <param name="message">Exception message to give when value is invalid.</param>
  42. /// <param name="parameterName">Name of the parameter.</param>
  43. /// <exception cref="ArgumentOutOfRangeException">
  44. /// The value is less than zero.
  45. /// </exception>
  46. public static void ThrowIfLessThan<T>(T value, T minValue, string message, string parameterName)
  47. where T : IComparable<T>
  48. {
  49. if (value.CompareTo(minValue) < 0)
  50. throw new ArgumentOutOfRangeException(parameterName, message);
  51. }
  52. /// <summary>
  53. /// Throws an <see cref="ArgumentNullException"/> exception if the passed object equals null.
  54. /// </summary>
  55. /// <param name="obj">The object to check.</param>
  56. /// <exception cref="ArgumentNullException">
  57. /// The supplied object <paramref name="obj"/> equals to <see langword="null"/>.
  58. /// </exception>
  59. public static void ThrowIfNull(object obj)
  60. {
  61. if (obj == null)
  62. throw new ArgumentNullException(string.Empty);
  63. }
  64. /// <summary>
  65. /// Throws an <see cref="ArgumentNullException"/> exception if the passed object equals null.
  66. /// </summary>
  67. /// <param name="obj">>The object to check.</param>
  68. /// <param name="parameterName">Name of the parameter. Will be displayed with the exception details.</param>
  69. /// <exception cref="ArgumentNullException">
  70. /// The supplied object <paramref name="obj"/> equals to <see langword="null"/>.
  71. /// </exception>
  72. public static void ThrowIfNull(object obj, string parameterName)
  73. {
  74. if (obj == null)
  75. throw new ArgumentNullException(parameterName);
  76. }
  77. /// <summary>
  78. /// Throws an <see cref="ArgumentNullException"/> exception if the passed object equals null.
  79. /// </summary>
  80. /// <param name="obj">>The object to check.</param>
  81. /// <param name="parameterName">Name of the parameter. Will be displayed with the exception details.</param>
  82. /// <param name="message">Exception message to give when value is invalid.</param>
  83. /// <exception cref="ArgumentNullException">
  84. /// The supplied object <paramref name="obj"/> equals to <see langword="null"/>.
  85. /// </exception>
  86. public static void ThrowIfNull(object obj, string parameterName, string message)
  87. {
  88. if (obj == null)
  89. throw new ArgumentNullException(parameterName, message);
  90. }
  91. /// <summary>
  92. /// Throws an <see cref="ArgumentException"/> if the passed string is an emtpy string.
  93. /// </summary>
  94. /// <param name="str">The string to check.</param>
  95. /// <exception cref="ArgumentException">
  96. /// The <paramref name="str"/> is <see langword="null"/> or empty.
  97. /// </exception>
  98. public static void ThrowIfEmptyString(string str)
  99. {
  100. if (string.IsNullOrEmpty(str))
  101. throw new ArgumentException(string.Empty);
  102. }
  103. /// <summary>
  104. /// Throws an <see cref="ArgumentException"/> if the passed string is an emtpy string.
  105. /// </summary>
  106. /// <param name="str">The string to check.</param>
  107. /// <param name="message">Exception message to give when value is invalid.</param>
  108. /// <exception cref="ArgumentException">
  109. /// The <paramref name="str"/> is <see langword="null"/> or empty.
  110. /// </exception>
  111. public static void ThrowIfEmptyString(string str, string message)
  112. {
  113. if (string.IsNullOrEmpty(str))
  114. throw new ArgumentException(message);
  115. }
  116. /// <summary>
  117. /// Throws an <see cref="ArgumentException"/> if the passed GUID is emtpy.
  118. /// </summary>
  119. /// <param name="guid">The GUID to check.</param>
  120. /// <param name="message">Exception message to give when value is invalid.</param>
  121. /// <exception cref="ArgumentException">
  122. /// The <paramref name="guid"/> is <see langword="null"/> or empty.
  123. /// </exception>
  124. public static void ThrowIfEmptyGuid(Guid guid, string message)
  125. {
  126. if (guid == Guid.Empty)
  127. throw new ArgumentException(message);
  128. }
  129. public static void ThrowIfEmptyGuid(Guid guid)
  130. {
  131. if (guid == Guid.Empty)
  132. throw new ArgumentException("Argument can't be an empty Guid.");
  133. }
  134. /// <summary>
  135. /// Throws an <see cref="ArgumentException"/> if two values are not equal.
  136. /// </summary>
  137. /// <typeparam name="T"></typeparam>
  138. /// <param name="value1">The first value to compare.</param>
  139. /// <param name="value2">The second value to compare.</param>
  140. /// <param name="message">Exception message to give when values are not equal.</param>
  141. /// <exception cref="ArgumentException">
  142. /// Values are not equal.
  143. /// </exception>
  144. public static void ThrowIfNotEqual<T>(T value1, T value2, string message)
  145. {
  146. if (!EqualityComparer<T>.Default.Equals(value1, value2))
  147. throw new ArgumentException(message);
  148. }
  149. /// <summary>
  150. /// Throws an <see cref="ArgumentException"/> if the passed string is an emtpy string.
  151. /// </summary>
  152. /// <param name="str">The string to check.</param>
  153. /// <param name="message">Exception message to give when value is invalid.</param>
  154. /// <param name="parameterName">Name of the parameter. Will be displayed with the exception details.</param>
  155. /// <exception cref="ArgumentException">
  156. /// The <paramref name="str"/> is <see langword="null"/> or empty.
  157. /// </exception>
  158. public static void ThrowIfEmptyString(string str, string message, string parameterName)
  159. {
  160. if (string.IsNullOrEmpty(str))
  161. throw new ArgumentException(message, parameterName);
  162. }
  163. /// <summary>
  164. /// Thows an <see cref="ArgumentNullException"/> if the passed collection is emtpy or a <see cref="ArgumentException"/>
  165. /// if the collection does not contain any elements.
  166. /// </summary>
  167. /// <typeparam name="T">Types of objects.</typeparam>
  168. /// <param name="collection">Collection to check.</param>
  169. /// <param name="parameterName">Name of the parameter. Will be displayed with the exception details.</param>
  170. /// <param name="message">Exception message to give when value is invalid.</param>
  171. /// <exception cref="ArgumentNullException">
  172. /// The supplied collection is <see langword="null"/>.
  173. /// </exception>
  174. /// <exception cref="ArgumentException">
  175. /// The supplied collection has no items.
  176. /// </exception>
  177. public static void ThrowIfEmpty<T>(IEnumerable<T> collection, string parameterName, string message)
  178. {
  179. if (collection == null)
  180. throw new ArgumentNullException(parameterName, message);
  181. if (!collection.Any())
  182. throw new ArgumentException(message, parameterName);
  183. }
  184. /// <summary>
  185. /// Throws an <see cref="ArgumentException"/> if the supplied string has a length longer than the specified length.
  186. /// </summary>
  187. /// <param name="str">String to check.</param>
  188. /// <param name="maxLength">The maximum string length allowed.</param>
  189. /// <param name="parameterName">Name of the parameter. Will be displayed with the exception details.</param>
  190. /// <exception cref="ArgumentOutOfRangeException">
  191. /// The supplied string is longer than the given <paramref name="maxLength"/>.
  192. /// </exception>
  193. public static void ThrowIfLonger(string str, int maxLength, string parameterName)
  194. {
  195. if (str.Length > maxLength)
  196. throw new ArgumentOutOfRangeException(parameterName, String.Format(CultureInfo.CurrentCulture, "{0} cannot be longer than {1} characters.", parameterName, maxLength));
  197. }
  198. /// <summary>
  199. /// Throws an <see cref="ArgumentException"/> if input string does not match regex pattern.
  200. /// </summary>
  201. /// <param name="str">The input string.</param>
  202. /// <param name="regexPattern">The regex pattern.</param>
  203. /// <param name="parameterName">Name of the parameter being validated.</param>
  204. /// <exception cref="ArgumentException">
  205. /// The <paramref name="str"/> does not match the <paramref name="regexPattern"/>.
  206. /// </exception>
  207. public static void ThrowIfDoNotMatch(string str, string regexPattern, string parameterName)
  208. {
  209. if (!Regex.IsMatch(str, regexPattern))
  210. throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "String '{0}' does not match the regex pattern '{1}'.", str, regexPattern), parameterName);
  211. }
  212. }
  213. }