using System; using System.Diagnostics.CodeAnalysis; using System.Text.RegularExpressions; namespace InfluxDB.Client.Core { /// /// Functions for parameter validation. /// /// /// Inspiration from InfluxDB java - thanks /// /// [SuppressMessage("ReSharper", "ParameterOnlyUsedForPreconditionCheck.Global")] public static class Arguments { private const string DurationPattern = @"([-+]?)([0-9]+(\\.[0-9]*)?[a-z]+)+|inf|-inf"; /// /// Enforces that the string is not empty. /// /// the string to test /// the variable name for reporting /// if the string is empty public static void CheckNonEmptyString(string value, string name) { if (string.IsNullOrEmpty(value)) { throw new ArgumentException("Expecting a non-empty string for " + name); } } /// /// Enforces that the string is duration literal. /// /// the string to test /// the variable name for reporting /// if the string is not duration literal public static void CheckDuration(string value, string name) { if (string.IsNullOrEmpty(value) || !Regex.Match(value, DurationPattern).Success) { throw new ArgumentException("Expecting a duration string for " + name + ". But got: " + value); } } /// /// Enforces that the number is larger than 0. /// /// the number to test /// the variable name for reporting /// if the number is less or equal to 0 public static void CheckPositiveNumber(int number, string name) { if (number <= 0) { throw new ArgumentException("Expecting a positive number for " + name); } } /// /// Enforces that the number is not negative. /// /// the number to test /// the variable name for reporting /// if the number is less or equal to 0 public static void CheckNotNegativeNumber(int number, string name) { if (number < 0) { throw new ArgumentException("Expecting a positive or zero number for " + name); } } /// /// Checks that the specified object reference is not null. /// /// the object to test /// the variable name for reporting /// if the object is null public static void CheckNotNull(object obj, string name) { if (obj == null) { throw new NullReferenceException("Expecting a not null reference for " + name); } } } }