| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | #if AZURE_NULLABLE |
| | 5 | | #nullable enable |
| | 6 | | #endif |
| | 7 | |
|
| | 8 | | using System; |
| | 9 | | using System.Collections; |
| | 10 | | using System.Collections.Generic; |
| | 11 | | using System.Diagnostics.CodeAnalysis; |
| | 12 | |
|
| | 13 | | namespace Azure.Core |
| | 14 | | { |
| | 15 | | /// <summary> |
| | 16 | | /// Argument validation. |
| | 17 | | /// </summary> |
| | 18 | | /// <remarks> |
| | 19 | | /// <para>This class should be shared via source using Azure.Core.props and contain only common argument validatio |
| | 20 | | /// It is declared partial so that you can use the same familiar class name but extend it with project-specific |
| | 21 | | /// To extend the functionality of this class, just declare your own partial <see cref="Argument"/> class with p |
| | 22 | | /// </para> |
| | 23 | | /// <para> |
| | 24 | | /// Be sure to document exceptions thrown by these methods on your public methods. |
| | 25 | | /// </para> |
| | 26 | | /// </remarks> |
| | 27 | | internal static partial class Argument |
| | 28 | | { |
| | 29 | | // TODO: Add nullability attributes as needed when possible. |
| | 30 | |
|
| | 31 | | /// <summary> |
| | 32 | | /// Throws if <paramref name="value"/> is null. |
| | 33 | | /// </summary> |
| | 34 | | /// <param name="value">The value to validate.</param> |
| | 35 | | /// <param name="name">The name of the parameter.</param> |
| | 36 | | /// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception> |
| | 37 | | #if AZURE_NULLABLE |
| | 38 | | public static void AssertNotNull<T>([AllowNull, NotNull] T value, string name) where T : class? |
| | 39 | | #else |
| | 40 | | public static void AssertNotNull<T>(T value, string name) where T : class |
| | 41 | | #endif |
| | 42 | | { |
| 500 | 43 | | if (value is null) |
| | 44 | | { |
| 16 | 45 | | throw new ArgumentNullException(name); |
| | 46 | | } |
| 484 | 47 | | } |
| | 48 | |
|
| | 49 | | /// <summary> |
| | 50 | | /// Throws if <paramref name="value"/> has not been initialized. |
| | 51 | | /// </summary> |
| | 52 | | /// <param name="value">The value to validate.</param> |
| | 53 | | /// <param name="name">The name of the parameter.</param> |
| | 54 | | /// <exception cref="ArgumentNullException"><paramref name="value"/> has not been initialized.</exception> |
| | 55 | | public static void AssertNotNull<T>(T? value, string name) where T : struct |
| | 56 | | { |
| 0 | 57 | | if (!value.HasValue) |
| | 58 | | { |
| 0 | 59 | | throw new ArgumentNullException(name); |
| | 60 | | } |
| 0 | 61 | | } |
| | 62 | |
|
| | 63 | | /// <summary> |
| | 64 | | /// Throws if <paramref name="value"/> is null or an empty collection. |
| | 65 | | /// </summary> |
| | 66 | | /// <param name="value">The value to validate.</param> |
| | 67 | | /// <param name="name">The name of the parameter.</param> |
| | 68 | | /// <exception cref="ArgumentException"><paramref name="value"/> is an empty collection.</exception> |
| | 69 | | /// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception> |
| | 70 | | #if AZURE_NULLABLE |
| | 71 | | public static void AssertNotNullOrEmpty<T>([AllowNull, NotNull] IEnumerable<T> value, string name) |
| | 72 | | #else |
| | 73 | | public static void AssertNotNullOrEmpty<T>(IEnumerable<T> value, string name) |
| | 74 | | #endif |
| | 75 | | { |
| 0 | 76 | | if (value is null) |
| | 77 | | { |
| 0 | 78 | | throw new ArgumentNullException(name); |
| | 79 | | } |
| | 80 | |
|
| | 81 | | // .NET Framework's Enumerable.Any() always allocates an enumerator, so we optimize for collections here. |
| 0 | 82 | | if (value is ICollection<T> collectionOfT && collectionOfT.Count == 0) |
| | 83 | | { |
| 0 | 84 | | throw new ArgumentException("Value cannot be an empty collection.", name); |
| | 85 | | } |
| | 86 | |
|
| 0 | 87 | | if (value is ICollection collection && collection.Count == 0) |
| | 88 | | { |
| 0 | 89 | | throw new ArgumentException("Value cannot be an empty collection.", name); |
| | 90 | | } |
| | 91 | |
|
| 0 | 92 | | using IEnumerator<T> e = value.GetEnumerator(); |
| 0 | 93 | | if (!e.MoveNext()) |
| | 94 | | { |
| 0 | 95 | | throw new ArgumentException("Value cannot be an empty collection.", name); |
| | 96 | | } |
| 0 | 97 | | } |
| | 98 | |
|
| | 99 | | /// <summary> |
| | 100 | | /// Throws if <paramref name="value"/> is null or an empty string. |
| | 101 | | /// </summary> |
| | 102 | | /// <param name="value">The value to validate.</param> |
| | 103 | | /// <param name="name">The name of the parameter.</param> |
| | 104 | | /// <exception cref="ArgumentException"><paramref name="value"/> is an empty string.</exception> |
| | 105 | | /// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception> |
| | 106 | | #if AZURE_NULLABLE |
| | 107 | | public static void AssertNotNullOrEmpty([AllowNull, NotNull] string value, string name) |
| | 108 | | #else |
| | 109 | | public static void AssertNotNullOrEmpty(string value, string name) |
| | 110 | | #endif |
| | 111 | | { |
| 1058 | 112 | | if (value is null) |
| | 113 | | { |
| 30 | 114 | | throw new ArgumentNullException(name); |
| | 115 | | } |
| | 116 | |
|
| 1028 | 117 | | if (value.Length == 0) |
| | 118 | | { |
| 30 | 119 | | throw new ArgumentException("Value cannot be an empty string.", name); |
| | 120 | | } |
| 998 | 121 | | } |
| | 122 | |
|
| | 123 | | /// <summary> |
| | 124 | | /// Throws if <paramref name="value"/> is null, an empty string, or consists only of white-space characters. |
| | 125 | | /// </summary> |
| | 126 | | /// <param name="value">The value to validate.</param> |
| | 127 | | /// <param name="name">The name of the parameter.</param> |
| | 128 | | /// <exception cref="ArgumentException"><paramref name="value"/> is an empty string or consists only of white-sp |
| | 129 | | /// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception> |
| | 130 | | #if AZURE_NULLABLE |
| | 131 | | public static void AssertNotNullOrWhiteSpace([AllowNull, NotNull] string value, string name) |
| | 132 | | #else |
| | 133 | | public static void AssertNotNullOrWhiteSpace(string value, string name) |
| | 134 | | #endif |
| | 135 | | { |
| 0 | 136 | | if (value is null) |
| | 137 | | { |
| 0 | 138 | | throw new ArgumentNullException(name); |
| | 139 | | } |
| | 140 | |
|
| 0 | 141 | | if (string.IsNullOrWhiteSpace(value)) |
| | 142 | | { |
| 0 | 143 | | throw new ArgumentException("Value cannot be empty or contain only white-space characters.", name); |
| | 144 | | } |
| 0 | 145 | | } |
| | 146 | |
|
| | 147 | | /// <summary> |
| | 148 | | /// Throws if <paramref name="value"/> is the default value for type <typeparamref name="T"/>. |
| | 149 | | /// </summary> |
| | 150 | | /// <typeparam name="T">The type of structure to validate which implements <see cref="IEquatable{T}"/>.</typepar |
| | 151 | | /// <param name="value">The value to validate.</param> |
| | 152 | | /// <param name="name">The name of the parameter.</param> |
| | 153 | | /// <exception cref="ArgumentException"><paramref name="value"/> is the default value for type <typeparamref nam |
| | 154 | | public static void AssertNotDefault<T>(ref T value, string name) where T : struct, IEquatable<T> |
| | 155 | | { |
| 0 | 156 | | if (value.Equals(default)) |
| | 157 | | { |
| 0 | 158 | | throw new ArgumentException("Value cannot be empty.", name); |
| | 159 | | } |
| 0 | 160 | | } |
| | 161 | |
|
| | 162 | | /// <summary> |
| | 163 | | /// Throws if <paramref name="value"/> is less than the <paramref name="minimum"/> or greater than the <paramref |
| | 164 | | /// </summary> |
| | 165 | | /// <typeparam name="T">The type of to validate which implements <see cref="IComparable{T}"/>.</typeparam> |
| | 166 | | /// <param name="value">The value to validate.</param> |
| | 167 | | /// <param name="minimum">The minimum value to compare.</param> |
| | 168 | | /// <param name="maximum">The maximum value to compare.</param> |
| | 169 | | /// <param name="name">The name of the parameter.</param> |
| | 170 | | public static void AssertInRange<T>(T value, T minimum, T maximum, string name) where T : notnull, IComparable<T |
| | 171 | | { |
| 0 | 172 | | if (minimum.CompareTo(value) > 0) |
| | 173 | | { |
| 0 | 174 | | throw new ArgumentOutOfRangeException(name, "Value is less than the minimum allowed."); |
| | 175 | | } |
| | 176 | |
|
| 0 | 177 | | if (maximum.CompareTo(value) < 0) |
| | 178 | | { |
| 0 | 179 | | throw new ArgumentOutOfRangeException(name, "Value is greater than the maximum allowed."); |
| | 180 | | } |
| 0 | 181 | | } |
| | 182 | | } |
| | 183 | | } |