• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #region Copyright notice and license
2 
3 // Copyright 2015 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #endregion
18 
19 using System;
20 
21 namespace Grpc.Core.Utils
22 {
23     /// <summary>
24     /// Utility methods to simplify checking preconditions in the code.
25     /// </summary>
26     public static class GrpcPreconditions
27     {
28         /// <summary>
29         /// Throws <see cref="ArgumentException"/> if condition is false.
30         /// </summary>
31         /// <param name="condition">The condition.</param>
CheckArgument(bool condition)32         public static void CheckArgument(bool condition)
33         {
34             if (!condition)
35             {
36                 throw new ArgumentException();
37             }
38         }
39 
40         /// <summary>
41         /// Throws <see cref="ArgumentException"/> with given message if condition is false.
42         /// </summary>
43         /// <param name="condition">The condition.</param>
44         /// <param name="errorMessage">The error message.</param>
CheckArgument(bool condition, string errorMessage)45         public static void CheckArgument(bool condition, string errorMessage)
46         {
47             if (!condition)
48             {
49                 throw new ArgumentException(errorMessage);
50             }
51         }
52 
53         /// <summary>
54         /// Throws <see cref="ArgumentNullException"/> if reference is null.
55         /// </summary>
56         /// <param name="reference">The reference.</param>
CheckNotNull(T reference)57         public static T CheckNotNull<T>(T reference)
58         {
59             if (reference == null)
60             {
61                 throw new ArgumentNullException();
62             }
63             return reference;
64         }
65 
66         /// <summary>
67         /// Throws <see cref="ArgumentNullException"/> if reference is null.
68         /// </summary>
69         /// <param name="reference">The reference.</param>
70         /// <param name="paramName">The parameter name.</param>
CheckNotNull(T reference, string paramName)71         public static T CheckNotNull<T>(T reference, string paramName)
72         {
73             if (reference == null)
74             {
75                 throw new ArgumentNullException(paramName);
76             }
77             return reference;
78         }
79 
80         /// <summary>
81         /// Throws <see cref="InvalidOperationException"/> if condition is false.
82         /// </summary>
83         /// <param name="condition">The condition.</param>
CheckState(bool condition)84         public static void CheckState(bool condition)
85         {
86             if (!condition)
87             {
88                 throw new InvalidOperationException();
89             }
90         }
91 
92         /// <summary>
93         /// Throws <see cref="InvalidOperationException"/> with given message if condition is false.
94         /// </summary>
95         /// <param name="condition">The condition.</param>
96         /// <param name="errorMessage">The error message.</param>
CheckState(bool condition, string errorMessage)97         public static void CheckState(bool condition, string errorMessage)
98         {
99             if (!condition)
100             {
101                 throw new InvalidOperationException(errorMessage);
102             }
103         }
104     }
105 }
106