/// <summary>
        /// Get the argument value or default value.
        /// </summary>
        /// <typeparam name="TArgument">Type of the argument.</typeparam>
        /// <param name="argument">Argument wrapper.</param>
        /// <param name="defaultValue">Default value to return if argument is default.</param>
        /// <returns>Argument value.</returns>
        public static TArgument GetOrDefault <TArgument>(
            this ArgumentWrapper <TArgument> argument,
            TArgument defaultValue = default)
        {
            if (argument == null || EqualityComparer <TArgument> .Default.Equals(argument.Value, default))
            {
                return(defaultValue);
            }

            return(argument);
        }
        /// <summary>
        /// Check argument is not default.
        /// </summary>
        /// <typeparam name="TArgument">Type of the argument.</typeparam>
        /// <param name="argument">Argument wrapper.</param>
        /// <param name="message">Message.</param>
        /// <returns>Argument wrapper.</returns>
        public static ArgumentWrapper <TArgument> IsNotDefault <TArgument>(
            this ArgumentWrapper <TArgument> argument,
            string message = null)
        {
            if (EqualityComparer <TArgument> .Default.Equals(argument.Value, default))
            {
                throw new ArgumentException(
                          paramName: argument.Name,
                          message: message ?? $"Argument value shall not be default.");
            }

            return(argument);
        }
예제 #3
0
        /// <summary>
        /// Check argument is not only white space.
        /// </summary>
        /// <param name="argument">Argument wrapper.</param>
        /// <param name="defaultValue">Default value to return if argument is default.</param>
        /// <returns>Argument value.</returns>
        public static ArgumentWrapper <string> IsNotWhiteSpace(
            this ArgumentWrapper <string> argument,
            string message = null)
        {
            if (argument.Value.Trim().Length == 0)
            {
                throw new ArgumentException(
                          paramName: argument.Name,
                          message: message ?? $"Argument value shall not only consists of white-space characters.");
            }

            return(argument);
        }
예제 #4
0
        /// <summary>
        /// Check string is not empty.
        /// </summary>
        /// <param name="argument">Argument wrapper.</param>
        /// <param name="message">Message.</param>
        /// <returns>Argument wrapper.</returns>
        public static ArgumentWrapper <string> IsNotEmpty(
            this ArgumentWrapper <string> argument,
            string message = null)
        {
            if (argument.Value.Length == 0)
            {
                throw new ArgumentException(
                          paramName: argument.Name,
                          message: message ?? $"Argument value shall not be an empty string.");
            }

            return(argument);
        }
예제 #5
0
        /// <summary>
        /// Check the argument enumerable is not empty.
        /// </summary>
        /// <param name="argument">Argument wrapper.</param>
        /// <param name="message">Optional message.</param>
        /// <returns>Argument wrapper.</returns>
        public static ArgumentWrapper <IEnumerable <TArgument> > IsNotEmpty <TArgument>(
            this ArgumentWrapper <IEnumerable <TArgument> > argument,
            string message = null)
        {
            if (argument.Value.Skip(1).Any())
            {
                throw new ArgumentException(
                          paramName: argument.Name,
                          message: message ?? $"Enumerable shall not be empty.");
            }

            return(argument);
        }
예제 #6
0
        /// <summary>
        /// Check the argument enumerable has a certain number of elements.
        /// </summary>
        /// <param name="argument">Argument wrapper.</param>
        /// <param name="count">Number of elements the enumerable shall have.</param>
        /// <param name="message">Optional message.</param>
        /// <returns>Argument wrapper.</returns>
        public static ArgumentWrapper <IEnumerable <TArgument> > HasCount <TArgument>(
            this ArgumentWrapper <IEnumerable <TArgument> > argument,
            int count,
            string message = null)
        {
            if (argument.Value.Count() != count)
            {
                throw new ArgumentException(
                          paramName: argument.Name,
                          message: message ?? $"Enumerable shall have {count} elements.");
            }

            return(argument);
        }
        /// <summary>
        /// Check argument is not null.
        /// </summary>
        /// <typeparam name="TArgument">Type of the argument.</typeparam>
        /// <param name="argument">Argument wrapper.</param>
        /// <param name="message">Message.</param>
        /// <returns>Argument wrapper.</returns>
        public static ArgumentWrapper <TArgument> IsNotNull <TArgument>(
            this ArgumentWrapper <TArgument> argument,
            string message = null)
        {
            if (argument == null)
            {
                throw new ArgumentNullException(nameof(argument));
            }

            if (argument.Value == null)
            {
                throw new ArgumentNullException(
                          paramName: argument.Name,
                          message: message ?? $"Argument value shall not be null.");
            }

            return(argument);
        }
        /// <summary>
        /// Check if the given predicate returns true for the argument value.
        /// </summary>
        /// <typeparam name="TArgument">Type of the argument.</typeparam>
        /// <param name="argument">Argument wrapper.</param>
        /// <param name="predicate">Predicate.</param>
        /// <param name="message">Message.</param>
        /// <returns>Argument wrapper.</returns>
        public static ArgumentWrapper <TArgument> Is <TArgument>(
            this ArgumentWrapper <TArgument> argument,
            Predicate <TArgument> predicate,
            string message = null)
        {
            if (argument == null)
            {
                throw new ArgumentNullException(nameof(argument));
            }

            if (predicate == null)
            {
                throw new ArgumentNullException(nameof(predicate));
            }

            if (!predicate(argument.Value))
            {
                throw new ArgumentException(
                          paramName: argument.Name,
                          message: message ?? $"Predicate shall return true for the value {argument.Value}.");
            }

            return(argument);
        }