OLD | NEW |
(Empty) | |
| 1 #region Copyright notice and license |
| 2 |
| 3 // Copyright 2015-2016, Google Inc. |
| 4 // All rights reserved. |
| 5 // |
| 6 // Redistribution and use in source and binary forms, with or without |
| 7 // modification, are permitted provided that the following conditions are |
| 8 // met: |
| 9 // |
| 10 // * Redistributions of source code must retain the above copyright |
| 11 // notice, this list of conditions and the following disclaimer. |
| 12 // * Redistributions in binary form must reproduce the above |
| 13 // copyright notice, this list of conditions and the following disclaimer |
| 14 // in the documentation and/or other materials provided with the |
| 15 // distribution. |
| 16 // * Neither the name of Google Inc. nor the names of its |
| 17 // contributors may be used to endorse or promote products derived from |
| 18 // this software without specific prior written permission. |
| 19 // |
| 20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 |
| 32 #endregion |
| 33 |
| 34 using System; |
| 35 |
| 36 namespace Grpc.Core.Utils |
| 37 { |
| 38 /// <summary> |
| 39 /// Utility methods to simplify checking preconditions in the code. |
| 40 /// </summary> |
| 41 public static class GrpcPreconditions |
| 42 { |
| 43 /// <summary> |
| 44 /// Throws <see cref="ArgumentException"/> if condition is false. |
| 45 /// </summary> |
| 46 /// <param name="condition">The condition.</param> |
| 47 public static void CheckArgument(bool condition) |
| 48 { |
| 49 if (!condition) |
| 50 { |
| 51 throw new ArgumentException(); |
| 52 } |
| 53 } |
| 54 |
| 55 /// <summary> |
| 56 /// Throws <see cref="ArgumentException"/> with given message if conditi
on is false. |
| 57 /// </summary> |
| 58 /// <param name="condition">The condition.</param> |
| 59 /// <param name="errorMessage">The error message.</param> |
| 60 public static void CheckArgument(bool condition, string errorMessage) |
| 61 { |
| 62 if (!condition) |
| 63 { |
| 64 throw new ArgumentException(errorMessage); |
| 65 } |
| 66 } |
| 67 |
| 68 /// <summary> |
| 69 /// Throws <see cref="ArgumentNullException"/> if reference is null. |
| 70 /// </summary> |
| 71 /// <param name="reference">The reference.</param> |
| 72 public static T CheckNotNull<T>(T reference) |
| 73 { |
| 74 if (reference == null) |
| 75 { |
| 76 throw new ArgumentNullException(); |
| 77 } |
| 78 return reference; |
| 79 } |
| 80 |
| 81 /// <summary> |
| 82 /// Throws <see cref="ArgumentNullException"/> if reference is null. |
| 83 /// </summary> |
| 84 /// <param name="reference">The reference.</param> |
| 85 /// <param name="paramName">The parameter name.</param> |
| 86 public static T CheckNotNull<T>(T reference, string paramName) |
| 87 { |
| 88 if (reference == null) |
| 89 { |
| 90 throw new ArgumentNullException(paramName); |
| 91 } |
| 92 return reference; |
| 93 } |
| 94 |
| 95 /// <summary> |
| 96 /// Throws <see cref="InvalidOperationException"/> if condition is false
. |
| 97 /// </summary> |
| 98 /// <param name="condition">The condition.</param> |
| 99 public static void CheckState(bool condition) |
| 100 { |
| 101 if (!condition) |
| 102 { |
| 103 throw new InvalidOperationException(); |
| 104 } |
| 105 } |
| 106 |
| 107 /// <summary> |
| 108 /// Throws <see cref="InvalidOperationException"/> with given message if
condition is false. |
| 109 /// </summary> |
| 110 /// <param name="condition">The condition.</param> |
| 111 /// <param name="errorMessage">The error message.</param> |
| 112 public static void CheckState(bool condition, string errorMessage) |
| 113 { |
| 114 if (!condition) |
| 115 { |
| 116 throw new InvalidOperationException(errorMessage); |
| 117 } |
| 118 } |
| 119 } |
| 120 } |
OLD | NEW |