PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms 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
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  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. #if !CLR2
  16. using System.Linq.Expressions;
  17. #else
  18. using Microsoft.Scripting.Ast;
  19. #endif
  20. using System;
  21. using System.Collections;
  22. using System.Collections.Generic;
  23. using System.Diagnostics;
  24. namespace Microsoft.Scripting.Utils {
  25. internal static class ContractUtils {
  26. public static void RequiresNotNull(object value, string paramName) {
  27. Assert.NotEmpty(paramName);
  28. if (value == null) {
  29. throw new ArgumentNullException(paramName);
  30. }
  31. }
  32. public static void Requires(bool precondition) {
  33. if (!precondition) {
  34. throw new ArgumentException(Strings.MethodPreconditionViolated);
  35. }
  36. }
  37. public static void Requires(bool precondition, string paramName) {
  38. Assert.NotEmpty(paramName);
  39. if (!precondition) {
  40. throw new ArgumentException(Strings.InvalidArgumentValue, paramName);
  41. }
  42. }
  43. public static void Requires(bool precondition, string paramName, string message) {
  44. Assert.NotEmpty(paramName);
  45. if (!precondition) {
  46. throw new ArgumentException(message, paramName);
  47. }
  48. }
  49. public static void RequiresNotEmpty(string str, string paramName) {
  50. RequiresNotNull(str, paramName);
  51. if (str.Length == 0) {
  52. throw new ArgumentException(Strings.NonEmptyStringRequired, paramName);
  53. }
  54. }
  55. public static void RequiresNotEmpty<T>(ICollection<T> collection, string paramName) {
  56. RequiresNotNull(collection, paramName);
  57. if (collection.Count == 0) {
  58. throw new ArgumentException(Strings.NonEmptyCollectionRequired, paramName);
  59. }
  60. }
  61. /// <summary>
  62. /// Requires the range [offset, offset + count] to be a subset of [0, array.Count].
  63. /// </summary>
  64. /// <exception cref="ArgumentOutOfRangeException">Offset or count are out of range.</exception>
  65. public static void RequiresArrayRange<T>(IList<T> array, int offset, int count, string offsetName, string countName) {
  66. Assert.NotNull(array);
  67. RequiresArrayRange(array.Count, offset, count, offsetName, countName);
  68. }
  69. /// <summary>
  70. /// Requires the range [offset, offset + count] to be a subset of [0, array.Count].
  71. /// </summary>
  72. /// <exception cref="ArgumentOutOfRangeException">Offset or count are out of range.</exception>
  73. public static void RequiresArrayRange(int arraySize, int offset, int count, string offsetName, string countName) {
  74. Assert.NotEmpty(offsetName);
  75. Assert.NotEmpty(countName);
  76. Debug.Assert(arraySize >= 0);
  77. if (count < 0) throw new ArgumentOutOfRangeException(countName);
  78. if (offset < 0 || arraySize - offset < count) throw new ArgumentOutOfRangeException(offsetName);
  79. }
  80. /// <summary>
  81. /// Requires the array and all its items to be non-null.
  82. /// </summary>
  83. public static void RequiresNotNullItems<T>(IList<T> array, string arrayName) {
  84. Assert.NotNull(arrayName);
  85. RequiresNotNull(array, arrayName);
  86. for (int i = 0; i < array.Count; i++) {
  87. if (array[i] == null) {
  88. throw ExceptionUtils.MakeArgumentItemNullException(i, arrayName);
  89. }
  90. }
  91. }
  92. /// <summary>
  93. /// Requires the enumerable collection and all its items to be non-null.
  94. /// </summary>
  95. public static void RequiresNotNullItems<T>(IEnumerable<T> collection, string collectionName) {
  96. Assert.NotNull(collectionName);
  97. RequiresNotNull(collection, collectionName);
  98. int i = 0;
  99. foreach (var item in collection) {
  100. if (item == null) {
  101. throw ExceptionUtils.MakeArgumentItemNullException(i, collectionName);
  102. }
  103. i++;
  104. }
  105. }
  106. /// <summary>
  107. /// Requires the range [offset, offset + count] to be a subset of [0, array.Count].
  108. /// </summary>
  109. /// <exception cref="ArgumentNullException">Array is <c>null</c>.</exception>
  110. /// <exception cref="ArgumentOutOfRangeException">Offset or count are out of range.</exception>
  111. public static void RequiresListRange(IList array, int offset, int count, string offsetName, string countName) {
  112. Assert.NotEmpty(offsetName);
  113. Assert.NotEmpty(countName);
  114. Assert.NotNull(array);
  115. if (count < 0) throw new ArgumentOutOfRangeException(countName);
  116. if (offset < 0 || array.Count - offset < count) throw new ArgumentOutOfRangeException(offsetName);
  117. }
  118. }
  119. }