My favorites | Sign in
Project Home Downloads Issues Source
Repository:
Checkout   Browse   Changes   Clones    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#region Copyright 2008-2011 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;

/// <summary>
/// provides a set of runtime validations for inputs
/// </summary>
[System.Diagnostics.DebuggerNonUserCode]
static partial class Check
{
/// <summary>
/// Verifies that the condition is true and if it fails constructs the specified type of
/// exception and throws.
/// </summary>
public static void Assert<TException>(bool condition)
where TException : Exception, new()
{
if (!condition)
throw new TException();
}

/// <summary>
/// Verifies that the condition is true and if it fails constructs the specified type of
/// exception with any arguments provided and throws.
/// </summary>
public static void Assert<TException>(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();
}
}


/// <summary>
/// Used to delay creation of the excpetion until the condition fails.
/// </summary>
public delegate Exception ExceptionBuilder();

/// <summary>
/// Verifies that the condition is true and if it fails throws the execption returned
/// by fnExceptionBuilder()
/// </summary>
public static void Assert(bool condition, ExceptionBuilder fnExceptionBuilder)
{
if (!condition)
{
throw fnExceptionBuilder();
}
}

/// <summary>
/// Verifies that the condition is true and if it fails constructs the specified type of
/// exception with any arguments provided and throws.
/// </summary>
public static void Assert<TException>(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();
}
}

/// <summary>
/// Verifies that value is not null and returns the value or throws ArgumentNullException
/// </summary>
public static T NotNull<T>(T value)
{
if (value == null) throw new ArgumentNullException();
return value;
}

/// <summary>
/// Verfies that the string is not null and not empty and returns the string.
/// throws ArgumentNullException, ArgumentOutOfRangeException
/// </summary>
public static string NotEmpty(string value)
{
if (value == null) throw new ArgumentNullException();
if (value.Length == 0) throw new ArgumentOutOfRangeException();
return value;
}

/// <summary>
/// Verfies that the Guid is not empty.
/// throws ArgumentOutOfRangeException
/// </summary>
public static Guid NotEmpty(Guid value)
{
if (value == Guid.Empty)
throw new ArgumentOutOfRangeException();
return value;
}

/// <summary>
/// Verfies that the collection is not null and not empty and returns the collection.
/// throws ArgumentNullException, ArgumentOutOfRangeException
/// </summary>
public static T NotEmpty<T>(T value) where T : System.Collections.IEnumerable
{
if (value == null) throw new ArgumentNullException();
if (!value.GetEnumerator().MoveNext()) throw new ArgumentOutOfRangeException();
return value;
}

/// <summary>
/// Verifies that the two values are the same
/// throws ArgumentException
/// </summary>
public static void IsEqual<T>(T a, T b) where T : IEquatable<T>
{
if (false == a.Equals(b))
throw new ArgumentException();
}

/// <summary>
/// Verifies that the two values are NOT the same
/// throws ArgumentException
/// </summary>
public static void NotEqual<T>(T a, T b) where T : IEquatable<T>
{
if (true == a.Equals(b))
throw new ArgumentException();
}

/// <summary>
/// Verifies that the array is not empty and has at least min, but not more than max items.
/// throws ArgumentNullExcpetion
/// throws ArgumentOutOfRangeException
/// </summary>
public static T[] ArraySize<T>(T[] value, int min, int max)
{
if (value == null) throw new ArgumentNullException();
if (value.Length < min || value.Length > max)
throw new ArgumentOutOfRangeException();
return value;
}

/// <summary>
/// Verifies that the value is min, max, or between the two.
/// throws ArgumentOutOfRangeException
/// </summary>
public static T InRange<T>(T value, T min, T max) where T : IComparable<T>
{
if (value == null) throw new ArgumentNullException();
if (value.CompareTo(min) < 0)
throw new ArgumentOutOfRangeException();
if (value.CompareTo(max) > 0)
throw new ArgumentOutOfRangeException();
return value;
}

/// <summary>
/// Returns (T)value if the object provided can be assinged to a variable of type T
/// throws ArgumentException
/// </summary>
public static T IsAssignable<T>(object value)
{
return (T) IsAssignable(typeof (T), value);
}

/// <summary>
/// Returns value if the object provided can be assinged to a variable of type toType
/// throws ArgumentException
/// </summary>
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;
}

/// <summary>
/// Throws ArgumentException if the type fromType cannot be assigned to variable of type toType
/// </summary>
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));
}
}

Change log

59104bd26e63 by rogerk on Apr 26, 2011   Diff
Release version 1.11.426.305
Go to: 
Project members, sign in to write a code review

Older revisions

be7965ddd079 by Grigand on Nov 29, 2010   Diff
Release v1.10.1124.358

* Introduction of the RpcLibrary
* Added GeneratedCode attributes to
nested classes within the resource
...
a9af4ee4f5f9 by Grigand on Oct 24, 2010   Diff
*v1.10.1024.336*

  # Added
CSharpTest.Net.Generators.exe to
integrate with the CmdTool's VS
...
9f48ec19f8e3 by Grigand on Sep 13, 2010   Diff
Release version 1.10.913.269
All revisions of this file

File info

Size: 7563 bytes, 215 lines
Powered by Google Project Hosting