New tests.
[mono.git] / mcs / class / System.ComponentModel.Composition / src / ComponentModel / Microsoft / Internal / Requires.cs
1 // -----------------------------------------------------------------------\r
2 // Copyright (c) Microsoft Corporation.  All rights reserved.\r
3 // -----------------------------------------------------------------------\r
4 using System;\r
5 using System.Collections;\r
6 using System.Collections.Generic;\r
7 using System.Diagnostics;\r
8 using System.Linq;\r
9 using System.Globalization;\r
10 using System.Reflection;\r
11 using System.ComponentModel.Composition;\r
12 using System.Text;\r
13 \r
14 namespace Microsoft.Internal\r
15 {\r
16     internal static class Requires\r
17     {\r
18         [DebuggerStepThrough]\r
19         public static void NotNull<T>(T value, string parameterName) \r
20             where T : class\r
21         {\r
22             if (value == null)\r
23             {\r
24                 throw new ArgumentNullException(parameterName);\r
25             }\r
26         }\r
27 \r
28         [DebuggerStepThrough]\r
29         public static void NotNullOrEmpty(string value, string parameterName)\r
30         {\r
31             NotNull(value, parameterName);\r
32 \r
33             if (value.Length == 0)\r
34             {\r
35                 throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.ArgumentException_EmptyString, parameterName), parameterName);\r
36             }\r
37         }\r
38 \r
39         [DebuggerStepThrough]\r
40         public static void NotNullOrNullElements<T>(IEnumerable<T> values, string parameterName)\r
41             where T : class\r
42         {\r
43             NotNull(values, parameterName);\r
44             NotNullElements(values, parameterName);\r
45         }\r
46 \r
47         [DebuggerStepThrough]\r
48         public static void NullOrNotNullElements<TKey, TValue>(IEnumerable<KeyValuePair<TKey, TValue>> values, string parameterName)\r
49             where TKey : class\r
50             where TValue : class\r
51         {\r
52             if (values != null)\r
53             {\r
54                 NotNullElements(values, parameterName);\r
55             }\r
56         }\r
57 \r
58         [DebuggerStepThrough]\r
59         public static void NullOrNotNullElements<T>(IEnumerable<T> values, string parameterName)\r
60             where T : class\r
61         {\r
62             if (values != null)\r
63             {\r
64                 NotNullElements(values, parameterName);\r
65             }\r
66         }\r
67 \r
68         private static void NotNullElements<T>(IEnumerable<T> values, string parameterName)\r
69             where T : class\r
70         {\r
71             foreach (T value in values)\r
72             {\r
73                 if (value == null)\r
74                 {\r
75                     throw ExceptionBuilder.CreateContainsNullElement(parameterName);\r
76                 }\r
77             }\r
78         }\r
79 \r
80         private static void NotNullElements<TKey, TValue>(IEnumerable<KeyValuePair<TKey, TValue>> values, string parameterName)\r
81             where TKey : class\r
82             where TValue : class\r
83         {\r
84             foreach (KeyValuePair<TKey, TValue> value in values)\r
85             {\r
86                 if ((value.Key == null) || (value.Value == null))\r
87                 {\r
88                     throw ExceptionBuilder.CreateContainsNullElement(parameterName);\r
89                 }\r
90             }\r
91         }\r
92         [DebuggerStepThrough]\r
93         public static void IsInMembertypeSet(MemberTypes value, string parameterName, MemberTypes enumFlagSet)\r
94         {\r
95             if ((value & enumFlagSet) != value || // Ensure the member is in the set\r
96                 (value & (value - 1)) != 0) // Ensure that there is only one flag in the value (i.e. value is a power of 2).\r
97             {\r
98                 throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.ArgumentOutOfRange_InvalidEnumInSet, parameterName, value, enumFlagSet.ToString()), parameterName);\r
99             }\r
100         }\r
101     }\r
102 }\r