PageRenderTime 21ms CodeModel.GetById 9ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/IronPython_Main/Runtime/Microsoft.Scripting/Utils/ContractUtils.cs

#
C# | 139 lines | 86 code | 20 blank | 33 comment | 20 complexity | b08d56a434895ea3c016abb4f8f2d08c MD5 | raw file
  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
 17using System.Linq.Expressions;
 18#else
 19using Microsoft.Scripting.Ast;
 20#endif
 21
 22using System;
 23using System.Collections;
 24using System.Collections.Generic;
 25using System.Diagnostics;
 26
 27namespace Microsoft.Scripting.Utils {
 28    internal static class ContractUtils {
 29        public static void RequiresNotNull(object value, string paramName) {
 30            Assert.NotEmpty(paramName);
 31
 32            if (value == null) {
 33                throw new ArgumentNullException(paramName);
 34            }
 35        }
 36
 37        public static void Requires(bool precondition) {
 38            if (!precondition) {
 39                throw new ArgumentException(Strings.MethodPreconditionViolated);
 40            }
 41        }
 42
 43        public static void Requires(bool precondition, string paramName) {
 44            Assert.NotEmpty(paramName);
 45
 46            if (!precondition) {
 47                throw new ArgumentException(Strings.InvalidArgumentValue, paramName);
 48            }
 49        }
 50
 51        public static void Requires(bool precondition, string paramName, string message) {
 52            Assert.NotEmpty(paramName);
 53
 54            if (!precondition) {
 55                throw new ArgumentException(message, paramName);
 56            }
 57        }
 58
 59        public static void RequiresNotEmpty(string str, string paramName) {
 60            RequiresNotNull(str, paramName);
 61            if (str.Length == 0) {
 62                throw new ArgumentException(Strings.NonEmptyStringRequired, paramName);
 63            }
 64        }
 65
 66        public static void RequiresNotEmpty<T>(ICollection<T> collection, string paramName) {
 67            RequiresNotNull(collection, paramName);
 68            if (collection.Count == 0) {
 69                throw new ArgumentException(Strings.NonEmptyCollectionRequired, paramName);
 70            }
 71        }
 72
 73        /// <summary>
 74        /// Requires the range [offset, offset + count] to be a subset of [0, array.Count].
 75        /// </summary>
 76        /// <exception cref="ArgumentOutOfRangeException">Offset or count are out of range.</exception>
 77        public static void RequiresArrayRange<T>(IList<T> array, int offset, int count, string offsetName, string countName) {
 78            Assert.NotNull(array);
 79            RequiresArrayRange(array.Count, offset, count, offsetName, countName);
 80        }
 81
 82        /// <summary>
 83        /// Requires the range [offset, offset + count] to be a subset of [0, array.Count].
 84        /// </summary>
 85        /// <exception cref="ArgumentOutOfRangeException">Offset or count are out of range.</exception>
 86        public static void RequiresArrayRange(int arraySize, int offset, int count, string offsetName, string countName) {
 87            Assert.NotEmpty(offsetName);
 88            Assert.NotEmpty(countName);
 89            Debug.Assert(arraySize >= 0);
 90
 91            if (count < 0) throw new ArgumentOutOfRangeException(countName);
 92            if (offset < 0 || arraySize - offset < count) throw new ArgumentOutOfRangeException(offsetName);
 93        }
 94
 95        /// <summary>
 96        /// Requires the array and all its items to be non-null.
 97        /// </summary>
 98        public static void RequiresNotNullItems<T>(IList<T> array, string arrayName) {
 99            Assert.NotNull(arrayName);
100            RequiresNotNull(array, arrayName);
101
102            for (int i = 0; i < array.Count; i++) {
103                if (array[i] == null) {
104                    throw ExceptionUtils.MakeArgumentItemNullException(i, arrayName);
105                }
106            }
107        }
108
109        /// <summary>
110        /// Requires the enumerable collection and all its items to be non-null.
111        /// </summary>
112        public static void RequiresNotNullItems<T>(IEnumerable<T> collection, string collectionName) {
113            Assert.NotNull(collectionName);
114            RequiresNotNull(collection, collectionName);
115
116            int i = 0;
117            foreach (var item in collection) {
118                if (item == null) {
119                    throw ExceptionUtils.MakeArgumentItemNullException(i, collectionName);
120                }
121                i++;
122            }
123        }
124
125        /// <summary>
126        /// Requires the range [offset, offset + count] to be a subset of [0, array.Count].
127        /// </summary>
128        /// <exception cref="ArgumentNullException">Array is <c>null</c>.</exception>
129        /// <exception cref="ArgumentOutOfRangeException">Offset or count are out of range.</exception>
130        public static void RequiresListRange(IList array, int offset, int count, string offsetName, string countName) {
131            Assert.NotEmpty(offsetName);
132            Assert.NotEmpty(countName);
133            Assert.NotNull(array);
134
135            if (count < 0) throw new ArgumentOutOfRangeException(countName);
136            if (offset < 0 || array.Count - offset < count) throw new ArgumentOutOfRangeException(offsetName);
137        }
138    }
139}