2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / dlr / Runtime / Microsoft.Scripting.Core / Utils / ContractUtils.cs
1 /* ****************************************************************************
2  *
3  * Copyright (c) Microsoft Corporation. 
4  *
5  * This source code is subject to terms and conditions of the Microsoft Public License. A 
6  * copy of the license can be found in the License.html file at the root of this distribution. If 
7  * you cannot locate the  Microsoft Public License, please send an email to 
8  * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
9  * by the terms of the Microsoft Public License.
10  *
11  * You must not remove this notice, or any other, from this software.
12  *
13  *
14  * ***************************************************************************/
15 using System; using Microsoft;
16
17
18 using System.Collections.Generic;
19 using System.Diagnostics;
20 #if CODEPLEX_40
21 using System.Linq.Expressions;
22 #else
23 using Microsoft.Linq.Expressions;
24 #endif
25
26 #if CODEPLEX_40
27 namespace System.Dynamic.Utils {
28 #else
29 namespace Microsoft.Scripting.Utils {
30 #endif
31
32     // Will be replaced with CLRv4 managed contracts
33     internal static class ContractUtils {
34
35         internal static Exception Unreachable {
36             get {
37                 Debug.Assert(false, "Unreachable");
38                 return new InvalidOperationException("Code supposed to be unreachable");
39             }
40         }
41
42         internal static void Requires(bool precondition) {
43             if (!precondition) {
44                 throw new ArgumentException(Strings.MethodPreconditionViolated);
45             }
46         }
47
48         internal static void Requires(bool precondition, string paramName) {
49             Debug.Assert(!string.IsNullOrEmpty(paramName));
50
51             if (!precondition) {
52                 throw new ArgumentException(Strings.InvalidArgumentValue, paramName);
53             }
54         }
55
56         internal static void Requires(bool precondition, string paramName, string message) {
57             Debug.Assert(!string.IsNullOrEmpty(paramName));
58
59             if (!precondition) {
60                 throw new ArgumentException(message, paramName);
61             }
62         }
63
64         internal static void RequiresNotNull(object value, string paramName) {
65             Debug.Assert(!string.IsNullOrEmpty(paramName));
66
67             if (value == null) {
68                 throw new ArgumentNullException(paramName);
69             }
70         }
71
72         internal static void RequiresNotEmpty<T>(ICollection<T> collection, string paramName) {
73             RequiresNotNull(collection, paramName);
74             if (collection.Count == 0) {
75                 throw new ArgumentException(Strings.NonEmptyCollectionRequired, paramName);
76             }
77         }
78
79         /// <summary>
80         /// Requires the range [offset, offset + count] to be a subset of [0, array.Count].
81         /// </summary>
82         /// <exception cref="ArgumentNullException">Array is <c>null</c>.</exception>
83         /// <exception cref="ArgumentOutOfRangeException">Offset or count are out of range.</exception>
84         internal static void RequiresArrayRange<T>(IList<T> array, int offset, int count, string offsetName, string countName) {
85             Debug.Assert(!string.IsNullOrEmpty(offsetName));
86             Debug.Assert(!string.IsNullOrEmpty(countName));
87             Debug.Assert(array != null);
88
89             if (count < 0) throw new ArgumentOutOfRangeException(countName);
90             if (offset < 0 || array.Count - offset < count) throw new ArgumentOutOfRangeException(offsetName);
91         }
92
93         /// <summary>
94         /// Requires the array and all its items to be non-null.
95         /// </summary>
96         internal static void RequiresNotNullItems<T>(IList<T> array, string arrayName) {
97             Debug.Assert(arrayName != null);
98             RequiresNotNull(array, arrayName);
99
100             for (int i = 0; i < array.Count; i++) {
101                 if (array[i] == null) {
102                     throw new ArgumentNullException(string.Format(System.Globalization.CultureInfo.CurrentCulture, "{0}[{1}]", arrayName, i));
103                 }
104             }
105         }
106     }
107 }