andrew4582

Argument

Jun 5th, 2012
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.28 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4.  
  5. namespace Core {
  6.    
  7.     /// <summary>
  8.     /// Argument validation helper
  9.     /// </summary>
  10.     [DebuggerStepThrough]
  11.     public static class Argument {
  12.  
  13.         public static void IsNotEmpty(Guid argument,string argumentName) {
  14.             if(argument == Guid.Empty) {
  15.                 throw new ArgumentException("\"{0}\" cannot be empty guid.".FormatString(argumentName),argumentName);
  16.             }
  17.  
  18.         }
  19.  
  20.         public static void IsNotEmpty(string argument,string argumentName) {
  21.             if(string.IsNullOrWhiteSpace((argument ?? string.Empty))) {
  22.                 throw new ArgumentException("\"{0}\" cannot be empty.".FormatString(argumentName),argumentName);
  23.             }
  24.         }
  25.                
  26.         public static void IsNotOutOfLength(string argument,int length,string argumentName) {
  27.             if(argument.Trim().Length > length) {
  28.                 throw new ArgumentException("\"{0}\" cannot be more than {1} character.".FormatString(argumentName,length),argumentName);
  29.             }
  30.         }
  31.                
  32.         public static void IsNotNull(object argument,string argumentName) {
  33.             if(argument == null) {
  34.                 throw new ArgumentNullException(argumentName);
  35.             }
  36.         }
  37.                
  38.         public static void IsNotNegative(int argument,string argumentName) {
  39.             if(argument < 0) {
  40.                 throw new ArgumentOutOfRangeException(argumentName);
  41.             }
  42.         }
  43.                
  44.         public static void IsNotNegativeOrZero(int argument,string argumentName) {
  45.             if(argument <= 0) {
  46.                 throw new ArgumentOutOfRangeException(argumentName);
  47.             }
  48.         }
  49.                
  50.         public static void IsNotNegative(long argument,string argumentName) {
  51.             if(argument < 0) {
  52.                 throw new ArgumentOutOfRangeException(argumentName);
  53.             }
  54.         }
  55.  
  56.         public static void IsNotNegativeOrZero(long argument,string argumentName) {
  57.             if(argument <= 0) {
  58.                 throw new ArgumentOutOfRangeException(argumentName);
  59.             }
  60.         }
  61.                
  62.         public static void IsNotNegative(float argument,string argumentName) {
  63.             if(argument < 0) {
  64.                 throw new ArgumentOutOfRangeException(argumentName);
  65.             }
  66.         }
  67.  
  68.         public static void IsNotNegativeOrZero(float argument,string argumentName) {
  69.             if(argument <= 0) {
  70.                 throw new ArgumentOutOfRangeException(argumentName);
  71.             }
  72.         }
  73.                
  74.         public static void IsNotNegative(decimal argument,string argumentName) {
  75.             if(argument < 0) {
  76.                 throw new ArgumentOutOfRangeException(argumentName);
  77.             }
  78.         }
  79.  
  80.         public static void IsNotNegativeOrZero(decimal argument,string argumentName) {
  81.             if(argument <= 0) {
  82.                 throw new ArgumentOutOfRangeException(argumentName);
  83.             }
  84.         }
  85.        
  86.         public static void IsNotInPast(DateTime argument,string argumentName) {
  87.             if(argument < DateTime.Now) {
  88.                 throw new ArgumentOutOfRangeException(argumentName);
  89.             }
  90.         }
  91.  
  92.        
  93.         public static void IsNotInFuture(DateTime argument,string argumentName) {
  94.             if(argument > DateTime.Now) {
  95.                 throw new ArgumentOutOfRangeException(argumentName);
  96.             }
  97.         }
  98.  
  99.        
  100.         public static void IsNotNegative(TimeSpan argument,string argumentName) {
  101.             if(argument < TimeSpan.Zero) {
  102.                 throw new ArgumentOutOfRangeException(argumentName);
  103.             }
  104.         }
  105.  
  106.        
  107.         public static void IsNotNegativeOrZero(TimeSpan argument,string argumentName) {
  108.             if(argument <= TimeSpan.Zero) {
  109.                 throw new ArgumentOutOfRangeException(argumentName);
  110.             }
  111.         }
  112.  
  113.        
  114.         public static void IsNotEmpty<T>(ICollection<T> argument,string argumentName) {
  115.             IsNotNull(argument,argumentName);
  116.  
  117.             if(argument.Count == 0) {
  118.                 throw new ArgumentException("Collection cannot be empty.",argumentName);
  119.             }
  120.         }
  121.  
  122.        
  123.         public static void IsNotOutOfRange(int argument,int min,int max,string argumentName) {
  124.             if((argument < min) || (argument > max)) {
  125.                 throw new ArgumentOutOfRangeException(argumentName,"{0} must be between \"{1}\"-\"{2}\".".FormatString(argumentName,min,max));
  126.             }
  127.         }
  128.  
  129.        
  130.         public static void IsNotInvalidEmail(string argument,string argumentName) {
  131.             IsNotEmpty(argument,argumentName);
  132.  
  133.             if(!argument.IsEmail()) {
  134.                 throw new ArgumentException("\"{0}\" is not a valid email address.".FormatString(argumentName),argumentName);
  135.             }
  136.         }
  137.  
  138.        
  139.         public static void IsNotInvalidWebUrl(string argument,string argumentName) {
  140.             IsNotEmpty(argument,argumentName);
  141.  
  142.             if(!argument.IsWebUrl()) {
  143.                 throw new ArgumentException("\"{0}\" is not a valid web url.".FormatString(argumentName),argumentName);
  144.             }
  145.         }
  146.     }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment