forked from DotNetOpenAuth/DotNetOpenAuth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRequires.cs
255 lines (231 loc) · 9.22 KB
/
Requires.cs
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
//-----------------------------------------------------------------------
// <copyright file="Requires.cs" company="Outercurve Foundation">
// Copyright (c) Outercurve Foundation. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace DotNetOpenAuth {
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Linq;
using System.Text;
using DotNetOpenAuth.Messaging;
/// <summary>
/// Argument validation checks that throw some kind of ArgumentException when they fail (unless otherwise noted).
/// </summary>
internal static class Requires {
/// <summary>
/// Validates that a given parameter is not null.
/// </summary>
/// <typeparam name="T">The type of the parameter</typeparam>
/// <param name="value">The value.</param>
/// <param name="parameterName">Name of the parameter.</param>
/// <returns>The tested value, guaranteed to not be null.</returns>
#if !CLR4
[ContractArgumentValidator]
#endif
[Pure, DebuggerStepThrough]
internal static T NotNull<T>(T value, string parameterName) where T : class {
if (value == null) {
throw new ArgumentNullException(parameterName);
}
Contract.EndContractBlock();
return value;
}
/// <summary>
/// Validates that a parameter is not null or empty.
/// </summary>
/// <param name="value">The value.</param>
/// <param name="parameterName">Name of the parameter.</param>
/// <returns>The validated value.</returns>
#if !CLR4
[ContractArgumentValidator]
#endif
[Pure, DebuggerStepThrough]
internal static string NotNullOrEmpty(string value, string parameterName) {
NotNull(value, parameterName);
True(value.Length > 0, parameterName, Strings.EmptyStringNotAllowed);
Contract.Ensures(Contract.Result<string>() == value);
Contract.EndContractBlock();
return value;
}
/// <summary>
/// Validates that an array is not null or empty.
/// </summary>
/// <typeparam name="T">The type of the elements in the sequence.</typeparam>
/// <param name="value">The value.</param>
/// <param name="parameterName">Name of the parameter.</param>
#if !CLR4
[ContractArgumentValidator]
#endif
[Pure, DebuggerStepThrough]
internal static void NotNullOrEmpty<T>(IEnumerable<T> value, string parameterName) {
NotNull(value, parameterName);
True(value.Any(), parameterName, Strings.InvalidArgument);
Contract.EndContractBlock();
}
/// <summary>
/// Validates that an argument is either null or is a sequence with no null elements.
/// </summary>
/// <typeparam name="T">The type of elements in the sequence.</typeparam>
/// <param name="sequence">The sequence.</param>
/// <param name="parameterName">Name of the parameter.</param>
#if !CLR4
[ContractArgumentValidator]
#endif
[Pure, DebuggerStepThrough]
internal static void NullOrWithNoNullElements<T>(IEnumerable<T> sequence, string parameterName) where T : class {
if (sequence != null) {
if (sequence.Any(e => e == null)) {
throw new ArgumentException(MessagingStrings.SequenceContainsNullElement, parameterName);
}
}
}
/// <summary>
/// Validates some expression describing the acceptable range for an argument evaluates to true.
/// </summary>
/// <param name="condition">The expression that must evaluate to true to avoid an <see cref="ArgumentOutOfRangeException"/>.</param>
/// <param name="parameterName">Name of the parameter.</param>
/// <param name="message">The message to include with the exception.</param>
#if !CLR4
[ContractArgumentValidator]
#endif
[Pure, DebuggerStepThrough]
internal static void InRange(bool condition, string parameterName, string message = null) {
if (!condition) {
throw new ArgumentOutOfRangeException(parameterName, message);
}
Contract.EndContractBlock();
}
/// <summary>
/// Validates some expression describing the acceptable condition for an argument evaluates to true.
/// </summary>
/// <param name="condition">The expression that must evaluate to true to avoid an <see cref="ArgumentException"/>.</param>
/// <param name="parameterName">Name of the parameter.</param>
/// <param name="message">The message to include with the exception.</param>
#if !CLR4
[ContractArgumentValidator]
#endif
[Pure, DebuggerStepThrough]
internal static void True(bool condition, string parameterName = null, string message = null) {
if (!condition) {
throw new ArgumentException(message ?? Strings.InvalidArgument, parameterName);
}
Contract.EndContractBlock();
}
/// <summary>
/// Validates some expression describing the acceptable condition for an argument evaluates to true.
/// </summary>
/// <param name="condition">The expression that must evaluate to true to avoid an <see cref="ArgumentException"/>.</param>
/// <param name="parameterName">Name of the parameter.</param>
/// <param name="unformattedMessage">The unformatted message.</param>
/// <param name="args">Formatting arguments.</param>
#if !CLR4
[ContractArgumentValidator]
#endif
[Pure, DebuggerStepThrough]
internal static void True(bool condition, string parameterName, string unformattedMessage, params object[] args) {
if (!condition) {
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, unformattedMessage, args), parameterName);
}
Contract.EndContractBlock();
}
/// <summary>
/// Validates some expression describing the acceptable condition for an argument evaluates to true.
/// </summary>
/// <param name="condition">The expression that must evaluate to true to avoid an <see cref="InvalidOperationException"/>.</param>
#if !CLR4
[ContractArgumentValidator]
#endif
[Pure, DebuggerStepThrough]
internal static void ValidState(bool condition) {
if (!condition) {
throw new InvalidOperationException();
}
Contract.EndContractBlock();
}
/// <summary>
/// Validates some expression describing the acceptable condition for an argument evaluates to true.
/// </summary>
/// <param name="condition">The expression that must evaluate to true to avoid an <see cref="InvalidOperationException"/>.</param>
/// <param name="message">The message to include with the exception.</param>
#if !CLR4
[ContractArgumentValidator]
#endif
[Pure, DebuggerStepThrough]
internal static void ValidState(bool condition, string message) {
if (!condition) {
throw new InvalidOperationException(message);
}
Contract.EndContractBlock();
}
/// <summary>
/// Validates some expression describing the acceptable condition for an argument evaluates to true.
/// </summary>
/// <param name="condition">The expression that must evaluate to true to avoid an <see cref="InvalidOperationException"/>.</param>
/// <param name="unformattedMessage">The unformatted message.</param>
/// <param name="args">Formatting arguments.</param>
#if !CLR4
[ContractArgumentValidator]
#endif
[Pure, DebuggerStepThrough]
internal static void ValidState(bool condition, string unformattedMessage, params object[] args) {
if (!condition) {
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, unformattedMessage, args));
}
Contract.EndContractBlock();
}
/// <summary>
/// Validates that some argument describes a type that is or derives from a required type.
/// </summary>
/// <typeparam name="T">The type that the argument must be or derive from.</typeparam>
/// <param name="type">The type given in the argument.</param>
/// <param name="parameterName">Name of the parameter.</param>
#if !CLR4
[ContractArgumentValidator]
#endif
[Pure, DebuggerStepThrough]
internal static void NotNullSubtype<T>(Type type, string parameterName) {
NotNull(type, parameterName);
True(typeof(T).IsAssignableFrom(type), parameterName, MessagingStrings.UnexpectedType, typeof(T).FullName, type.FullName);
Contract.EndContractBlock();
}
/// <summary>
/// Validates some expression describing the acceptable condition for an argument evaluates to true.
/// </summary>
/// <param name="condition">The expression that must evaluate to true to avoid an <see cref="FormatException"/>.</param>
/// <param name="message">The message.</param>
#if !CLR4
[ContractArgumentValidator]
#endif
[Pure, DebuggerStepThrough]
internal static void Format(bool condition, string message) {
if (!condition) {
throw new FormatException(message);
}
Contract.EndContractBlock();
}
/// <summary>
/// Throws an <see cref="NotSupportedException"/> if a condition does not evaluate to <c>true</c>.
/// </summary>
/// <param name="condition">The expression that must evaluate to true to avoid an <see cref="NotSupportedException"/>.</param>
/// <param name="message">The message.</param>
[Pure, DebuggerStepThrough]
internal static void Support(bool condition, string message) {
if (!condition) {
throw new NotSupportedException(message);
}
}
/// <summary>
/// Throws an <see cref="ArgumentException"/>
/// </summary>
/// <param name="parameterName">Name of the parameter.</param>
/// <param name="message">The message.</param>
[Pure, DebuggerStepThrough]
internal static void Fail(string parameterName, string message) {
throw new ArgumentException(message, parameterName);
}
}
}