All files
[pithos-ms-client] / trunk / Libraries / Json40r2 / Source / Src / Newtonsoft.Json / Utilities / ValidationUtils.cs
1 #region License
2 // Copyright (c) 2007 James Newton-King
3 //
4 // Permission is hereby granted, free of charge, to any person
5 // obtaining a copy of this software and associated documentation
6 // files (the "Software"), to deal in the Software without
7 // restriction, including without limitation the rights to use,
8 // copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the
10 // Software is furnished to do so, subject to the following
11 // conditions:
12 //
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 // OTHER DEALINGS IN THE SOFTWARE.
24 #endregion
25
26 using System;
27 using System.Collections;
28 using System.Collections.Generic;
29 using System.Text;
30 using System.Globalization;
31
32 namespace Newtonsoft.Json.Utilities
33 {
34   internal static class ValidationUtils
35   {
36     public const string EmailAddressRegex = @"^([a-zA-Z0-9_'+*$%\^&!\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9:]{2,4})+$";
37     public const string CurrencyRegex = @"(^\$?(?!0,?\d)\d{1,3}(,?\d{3})*(\.\d\d)?)$";
38     public const string DateRegex =
39         @"^(((0?[1-9]|[12]\d|3[01])[\.\-\/](0?[13578]|1[02])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}|\d))|((0?[1-9]|[12]\d|30)[\.\-\/](0?[13456789]|1[012])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}|\d))|((0?[1-9]|1\d|2[0-8])[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}|\d))|(29[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00|[048])))$";
40     public const string NumericRegex = @"\d*";
41
42     public static void ArgumentNotNullOrEmpty(string value, string parameterName)
43     {
44       if (value == null)
45         throw new ArgumentNullException(parameterName);
46
47       if (value.Length == 0)
48         throw new ArgumentException("'{0}' cannot be empty.".FormatWith(CultureInfo.InvariantCulture, parameterName), parameterName);
49     }
50
51     public static void ArgumentNotNullOrEmptyOrWhitespace(string value, string parameterName)
52     {
53       ArgumentNotNullOrEmpty(value, parameterName);
54
55       if (StringUtils.IsWhiteSpace(value))
56         throw new ArgumentException("'{0}' cannot only be whitespace.".FormatWith(CultureInfo.InvariantCulture, parameterName), parameterName);
57     }
58
59     public static void ArgumentTypeIsEnum(Type enumType, string parameterName)
60     {
61       ArgumentNotNull(enumType, "enumType");
62
63       if (!enumType.IsEnum)
64         throw new ArgumentException("Type {0} is not an Enum.".FormatWith(CultureInfo.InvariantCulture, enumType), parameterName);
65     }
66
67     public static void ArgumentNotNullOrEmpty<T>(ICollection<T> collection, string parameterName)
68     {
69       ArgumentNotNullOrEmpty<T>(collection, parameterName, "Collection '{0}' cannot be empty.".FormatWith(CultureInfo.InvariantCulture, parameterName));
70     }
71
72     public static void ArgumentNotNullOrEmpty<T>(ICollection<T> collection, string parameterName, string message)
73     {
74       if (collection == null)
75         throw new ArgumentNullException(parameterName);
76
77       if (collection.Count == 0)
78         throw new ArgumentException(message, parameterName);
79     }
80
81     public static void ArgumentNotNullOrEmpty(ICollection collection, string parameterName)
82     {
83       ArgumentNotNullOrEmpty(collection, parameterName, "Collection '{0}' cannot be empty.".FormatWith(CultureInfo.InvariantCulture, parameterName));
84     }
85
86     public static void ArgumentNotNullOrEmpty(ICollection collection, string parameterName, string message)
87     {
88       if (collection == null)
89         throw new ArgumentNullException(parameterName);
90
91       if (collection.Count == 0)
92         throw new ArgumentException(message, parameterName);
93     }
94
95     public static void ArgumentNotNull(object value, string parameterName)
96     {
97       if (value == null)
98         throw new ArgumentNullException(parameterName);
99     }
100
101     public static void ArgumentNotNegative(int value, string parameterName)
102     {
103       if (value <= 0)
104         throw MiscellaneousUtils.CreateArgumentOutOfRangeException(parameterName, value, "Argument cannot be negative.");
105     }
106
107     public static void ArgumentNotNegative(int value, string parameterName, string message)
108     {
109       if (value <= 0)
110         throw MiscellaneousUtils.CreateArgumentOutOfRangeException(parameterName, value, message);
111     }
112
113     public static void ArgumentNotZero(int value, string parameterName)
114     {
115       if (value == 0)
116         throw MiscellaneousUtils.CreateArgumentOutOfRangeException(parameterName, value, "Argument cannot be zero.");
117     }
118
119     public static void ArgumentNotZero(int value, string parameterName, string message)
120     {
121       if (value == 0)
122         throw MiscellaneousUtils.CreateArgumentOutOfRangeException(parameterName, value, message);
123     }
124
125     public static void ArgumentIsPositive<T>(T value, string parameterName) where T : struct, IComparable<T>
126     {
127       if (value.CompareTo(default(T)) != 1)
128         throw MiscellaneousUtils.CreateArgumentOutOfRangeException(parameterName, value, "Positive number required.");
129     }
130
131     public static void ArgumentIsPositive(int value, string parameterName, string message)
132     {
133       if (value > 0)
134         throw MiscellaneousUtils.CreateArgumentOutOfRangeException(parameterName, value, message);
135     }
136
137     public static void ObjectNotDisposed(bool disposed, Type objectType)
138     {
139       if (disposed)
140         throw new ObjectDisposedException(objectType.Name);
141     }
142
143     public static void ArgumentConditionTrue(bool condition, string parameterName, string message)
144     {
145       if (!condition)
146         throw new ArgumentException(message, parameterName);
147     }
148   }
149 }