1 /* ****************************************************************************
2  *
3  * Copyright (c) Microsoft Corporation.
4  *
5  * This source code is subject to terms and conditions of the Apache License, Version 2.0. 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  Apache License, Version 2.0, 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 Apache License, Version 2.0.
10  *
11  * You must not remove this notice, or any other, from this software.
12  *
13  *
14  * ***************************************************************************/
15 
16 #if CLR2
17 using Microsoft.Scripting.Ast;
18 #else
19 using System.Linq.Expressions;
20 #endif
21 #if SILVERLIGHT
22 using System.Core;
23 #endif
24 
25 using System.Collections.Generic;
26 using System.Diagnostics;
27 
28 namespace System.Dynamic.Utils {
29 
30     // Will be replaced with CLRv4 managed contracts
31     internal static class ContractUtils {
32 
33         internal static Exception Unreachable {
34             get {
35                 Debug.Assert(false, "Unreachable");
36                 return new InvalidOperationException("Code supposed to be unreachable");
37             }
38         }
39 
Requires(bool precondition)40         internal static void Requires(bool precondition) {
41             if (!precondition) {
42                 throw new ArgumentException(Strings.MethodPreconditionViolated);
43             }
44         }
45 
Requires(bool precondition, string paramName)46         internal static void Requires(bool precondition, string paramName) {
47             Debug.Assert(!string.IsNullOrEmpty(paramName));
48 
49             if (!precondition) {
50                 throw new ArgumentException(Strings.InvalidArgumentValue, paramName);
51             }
52         }
53 
RequiresNotNull(object value, string paramName)54         internal static void RequiresNotNull(object value, string paramName) {
55             Debug.Assert(!string.IsNullOrEmpty(paramName));
56 
57             if (value == null) {
58                 throw new ArgumentNullException(paramName);
59             }
60         }
61 
RequiresNotEmpty(ICollection<T> collection, string paramName)62         internal static void RequiresNotEmpty<T>(ICollection<T> collection, string paramName) {
63             RequiresNotNull(collection, paramName);
64             if (collection.Count == 0) {
65                 throw new ArgumentException(Strings.NonEmptyCollectionRequired, paramName);
66             }
67         }
68 
69         /// <summary>
70         /// Requires the range [offset, offset + count] to be a subset of [0, array.Count].
71         /// </summary>
72         /// <exception cref="ArgumentNullException">Array is <c>null</c>.</exception>
73         /// <exception cref="ArgumentOutOfRangeException">Offset or count are out of range.</exception>
RequiresArrayRange(IList<T> array, int offset, int count, string offsetName, string countName)74         internal static void RequiresArrayRange<T>(IList<T> array, int offset, int count, string offsetName, string countName) {
75             Debug.Assert(!string.IsNullOrEmpty(offsetName));
76             Debug.Assert(!string.IsNullOrEmpty(countName));
77             Debug.Assert(array != null);
78 
79             if (count < 0) throw new ArgumentOutOfRangeException(countName);
80             if (offset < 0 || array.Count - offset < count) throw new ArgumentOutOfRangeException(offsetName);
81         }
82 
83         /// <summary>
84         /// Requires the array and all its items to be non-null.
85         /// </summary>
RequiresNotNullItems(IList<T> array, string arrayName)86         internal static void RequiresNotNullItems<T>(IList<T> array, string arrayName) {
87             Debug.Assert(arrayName != null);
88             RequiresNotNull(array, arrayName);
89 
90             for (int i = 0; i < array.Count; i++) {
91                 if (array[i] == null) {
92                     throw new ArgumentNullException(string.Format(System.Globalization.CultureInfo.CurrentCulture, "{0}[{1}]", arrayName, i));
93                 }
94             }
95         }
96     }
97 }
98