// // Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. // using System; using System.Collections.Generic; namespace Microsoft.SqlTools.Hosting.Utility { /// /// Provides common validation methods to simplify method /// parameter checks. /// public static class Validate { /// /// Throws ArgumentNullException if value is null. /// /// The name of the parameter being validated. /// The value of the parameter being validated. public static void IsNotNull(string parameterName, object valueToCheck) { if (valueToCheck == null) { throw new ArgumentNullException(parameterName); } } /// /// Throws ArgumentOutOfRangeException if the value is outside /// of the given lower and upper limits. /// /// The name of the parameter being validated. /// The value of the parameter being validated. /// The lower limit which the value should not be less than. /// The upper limit which the value should not be greater than. public static void IsWithinRange( string parameterName, long valueToCheck, long lowerLimit, long upperLimit) { // TODO: Debug assert here if lowerLimit >= upperLimit if (valueToCheck < lowerLimit || valueToCheck > upperLimit) { throw new ArgumentOutOfRangeException( parameterName, valueToCheck, string.Format( "Value is not between {0} and {1}", lowerLimit, upperLimit)); } } /// /// Throws ArgumentOutOfRangeException if the value is greater than or equal /// to the given upper limit. /// /// The name of the parameter being validated. /// The value of the parameter being validated. /// The upper limit which the value should be less than. public static void IsLessThan( string parameterName, long valueToCheck, long upperLimit) { if (valueToCheck >= upperLimit) { throw new ArgumentOutOfRangeException( parameterName, valueToCheck, string.Format( "Value is greater than or equal to {0}", upperLimit)); } } /// /// Throws ArgumentOutOfRangeException if the value is less than or equal /// to the given lower limit. /// /// The name of the parameter being validated. /// The value of the parameter being validated. /// The lower limit which the value should be greater than. public static void IsGreaterThan( string parameterName, long valueToCheck, long lowerLimit) { if (valueToCheck < lowerLimit) { throw new ArgumentOutOfRangeException( parameterName, valueToCheck, string.Format( "Value is less than or equal to {0}", lowerLimit)); } } /// /// Throws ArgumentException if the value is equal to the undesired value. /// /// The type of value to be validated. /// The name of the parameter being validated. /// The value that valueToCheck should not equal. /// The value of the parameter being validated. public static void IsNotEqual( string parameterName, TValue valueToCheck, TValue undesiredValue) { if (EqualityComparer.Default.Equals(valueToCheck, undesiredValue)) { throw new ArgumentException( string.Format( "The given value '{0}' should not equal '{1}'", valueToCheck, undesiredValue), parameterName); } } /// /// Throws ArgumentException if the value is null or an empty string. /// /// The name of the parameter being validated. /// The value of the parameter being validated. public static void IsNotNullOrEmptyString(string parameterName, string valueToCheck) { if (string.IsNullOrEmpty(valueToCheck)) { throw new ArgumentException( "Parameter contains a null, empty, or whitespace string.", parameterName); } } /// /// Throws ArgumentException if the value is null, an empty string, /// or a string containing only whitespace. /// /// The name of the parameter being validated. /// The value of the parameter being validated. public static void IsNotNullOrWhitespaceString(string parameterName, string valueToCheck) { if (string.IsNullOrWhiteSpace(valueToCheck)) { throw new ArgumentException( "Parameter contains a null, empty, or whitespace string.", parameterName); } } } }