Example #1
0
 // TODO: Put strings into the resources.
 //
 public static ArgumentUtilitiesHandle <string> EnsureMatchRegex(this ArgumentUtilitiesHandle <string> hnd, string regexPattern, ArgumentPlaceholder <RegexOptions> options = default)
 {
     regexPattern.Arg(nameof(regexPattern)).EnsureNotNullOrEmpty();
     if (hnd.Value is null || (options.HasExplicitValue ? Regex.IsMatch(input: hnd.Value, pattern: regexPattern, options: options.ExplicitValue) : Regex.IsMatch(input: hnd.Value, pattern: regexPattern)))
     {
         return(hnd);
     }
Example #2
0
        static IEnumerable <TItem> P_EnsureUnique <TItem, TValue>(ArgumentUtilitiesHandle <IEnumerable <TItem> > hnd, Func <TItem, int, TValue> keySelector, IEqualityComparer <TValue> comparer = default, bool skipNull = default, string keyName = default)
        {
            if (hnd.Value is null)
            {
                throw new ArgumentNullException(paramName: $"{nameof(hnd)}.{nameof(hnd.Value)}");
            }
            else if (keySelector is null)
            {
                throw new ArgumentNullException(paramName: nameof(keySelector));
            }
            //
            var elementIndexCounter = -1;
            var buffer = new HashSet <TValue>(comparer: comparer);

            foreach (var element in (skipNull ? hnd.Value.SkipNull() : hnd.Value))
            {
                elementIndexCounter++;
                var key = keySelector(element, elementIndexCounter);
                if (!buffer.Add(key))
                {
                    var exceptionMessage = $"{(hnd.IsProp ? $"Invalid value of property '{hnd.Name}'.{Environment.NewLine}" : string.Empty)}{FormatXResource(typeof(Array), "CanNotContainNonUnique/NonUniqueValueAt", key?.ToString(), elementIndexCounter.ToString("d"))}{(keyName is null ? string.Empty : $"{Environment.NewLine}\tKey name:{keyName.FmtStr().GNLI2()}")}";
                    throw hnd.ExceptionFactory?.Invoke(message: exceptionMessage) ?? new ArgumentException(paramName: $"{hnd.Name}[{elementIndexCounter:d}]", message: exceptionMessage);
                }
                yield return(element);
            }
Example #3
0
        // TODO: Put strings into the resources.
        //
        public static ArrayPartitionBoundary Get(int arrayLength, ArgumentUtilitiesHandle <int?> offset, ArgumentUtilitiesHandle <int?> count)
        {
            arrayLength
            .Arg(nameof(arrayLength))
            .EnsureNotLessThan(0);
            var locOffset =
                offset
                .EnsureNotLessThan(0)
                .EnsureNotGreaterThan(
                    operand: arrayLength - 1,
                    exceptionMessageFirstLineFactory:
                    locHnd => $"Указанное смещение не соответствует длине массива (или к.л. другой последовательности).{Environment.NewLine}\tДлина массива:{Environment.NewLine}{arrayLength.ToString("d").FmtStr().GNLI2()}{Environment.NewLine}\tУказанное смещение:{Environment.NewLine}{locHnd.Value.Value.ToString("d").FmtStr().GNLI2()}")
                .Value
                ?? 0;
            var maxCount = arrayLength - locOffset;
            var locCount =
                count
                .EnsureNotLessThan(0)
                .EnsureNotGreaterThan(
                    operand: maxCount,
                    exceptionMessageFirstLineFactory:
                    locHnd => $"Указанная длина сегмента массива не соответствует указанному смещению и всей длине массива (или к.л. другой последовательности).{Environment.NewLine}\tДлина массива:{Environment.NewLine}{arrayLength.ToString("d").FmtStr().GNLI2()}{Environment.NewLine}\tУказанное смещение:{Environment.NewLine}{locOffset.ToString("d").FmtStr().GNLI2()}")
                .Value
                ??
                maxCount;

            //
            return
                (new ArrayPartitionBoundary(
                     offset: locOffset,
                     count: locCount));
        }
Example #4
0
 public static ArgumentUtilitiesHandle <Type> EnsureClassOrInterface(this ArgumentUtilitiesHandle <Type> hnd)
 {
     if (hnd.Value == null || hnd.Value.IsClass() || hnd.Value.IsInterface())
     {
         return(hnd);
     }
     throw new ArgumentOutOfRangeException(hnd.Name, FormatXResource(typeof(Type), "NotClass", hnd.Value));
 }
Example #5
0
 // TODO: Put strings into the resources.
 //
 public static ArgumentUtilitiesHandle <DateTime> EnsureNotLessThan(this ArgumentUtilitiesHandle <DateTime> hnd, ArgumentUtilitiesHandle <DateTime> other)
 {
     if (hnd.Value < other.Value)
     {
         throw new ArgumentException(FormatXResource(typeof(ArgumentOutOfRangeException), "CanNotLessThan/OtherArg", other.Value.ToString("O"), other.Name));
     }
     return(hnd);
 }
Example #6
0
 public static ArgumentUtilitiesHandle <long> EnsureNotGreaterThan(this ArgumentUtilitiesHandle <long> hnd, long operand)
 {
     if (hnd.Value > operand)
     {
         throw new ArgumentOutOfRangeException(hnd.Name, FormatXResource(typeof(ArgumentOutOfRangeException), "CanNotGreaterThan", operand.ToString("d")));
     }
     return(hnd);
 }
Example #7
0
 public static ArgumentUtilitiesHandle <long> EnsureNotLessThanZero(this ArgumentUtilitiesHandle <long> hnd)
 {
     if (hnd.Value < 0L)
     {
         throw new ArgumentOutOfRangeException(hnd.Name, FormatXResource(typeof(ArgumentOutOfRangeException), "CanNotLessThanZero"));
     }
     return(hnd);
 }
Example #8
0
 public static ArgumentUtilitiesHandle <long?> EnsureNotZero(this ArgumentUtilitiesHandle <long?> hnd)
 {
     if (hnd.Value.Value == 0L)
     {
         throw new ArgumentOutOfRangeException(hnd.Name, FormatXResource(typeof(ArgumentOutOfRangeException), "CanNotEqualTo", 0L));
     }
     return(hnd);
 }
Example #9
0
 public static ArgumentUtilitiesHandle <long> EnsureNotLessThan(this ArgumentUtilitiesHandle <long> hnd, ArgumentUtilitiesHandle <long> otherArgument)
 {
     if (hnd.Value < otherArgument.Value)
     {
         throw new ArgumentOutOfRangeException(hnd.Name, FormatXResource(typeof(ArgumentOutOfRangeException), "CanNotLessThan/OtherArg2", hnd.Value.ToString("d"), otherArgument.Value.ToString("d"), otherArgument.Name));
     }
     return(hnd);
 }
Example #10
0
 public static ArgumentUtilitiesHandle <TypeInfo> EnsureClass(this ArgumentUtilitiesHandle <TypeInfo> argument)
 {
     if (argument.Value == null || argument.Value.IsClass)
     {
         return(argument);
     }
     throw new ArgumentOutOfRangeException(argument.Name, FormatXResource(typeof(Type), "NotClass", argument.Value));
 }
Example #11
0
 // TODO: Put strings into the resources.
 //
 public static ArgumentUtilitiesHandle <DateTime?> EnsureHasZeroTime(this ArgumentUtilitiesHandle <DateTime?> hnd)
 {
     if (hnd.Value.HasValue && hnd.Value.Value.TimeOfDay != TimeSpan.Zero)
     {
         throw new ArgumentException(string.Format("Указанное значение '{0}' содержит ненулевой компонент времени. Ожидалось значение с нулевым компонентом времени.", hnd.Value.Value.ToString("O")), hnd.Name);
     }
     return(hnd);
 }
Example #12
0
 public ArrayPartitionBoundary(ArgumentUtilitiesHandle <int> offset, ArgumentUtilitiesHandle <int> count)
 {
     offset.EnsureNotLessThan(0);
     count.EnsureNotLessThan(0);
     //
     Offset = offset.Value;
     Count  = count.Value;
 }
Example #13
0
 // TODO: Put strings into the resources.
 //
 public static ArgumentUtilitiesHandle <T> EnsureIsNull <T>(this ArgumentUtilitiesHandle <T> hnd)
     where T : class
 {
     if (!(hnd.Value is null))
     {
         var exceptionMessage = (hnd.IsProp ? $"Invalid value of property '{hnd.Name}'.{Environment.NewLine}" : string.Empty) + $"Expected value '{FormatStringUtilities.GetNullValueText(formatProvider: null)}'.";
         throw
             hnd.ExceptionFactory?.Invoke(message: exceptionMessage)
             ?? new ArgumentOutOfRangeException(message: exceptionMessage, paramName: hnd.Name);
     }
Example #14
0
 public static ArgumentUtilitiesHandle <int?> EnsureNotGreaterThan(this ArgumentUtilitiesHandle <int?> hnd, int operand, Func <ArgumentUtilitiesHandle <int?>, string> exceptionMessageFirstLineFactory = null)
 {
     if (hnd.Value > operand)
     {
         var exceptionMessageFirstLine = exceptionMessageFirstLineFactory?.Invoke(hnd);
         var exceptionMessage          = $"{(hnd.IsProp ? $"Указано недопустимое значение свойства '{hnd.Name}'.{Environment.NewLine}" : string.Empty)}{($"{(string.IsNullOrEmpty(exceptionMessageFirstLine) ? string.Empty : $"{exceptionMessageFirstLine}{Environment.NewLine}")}{FormatXResource(typeof(ArgumentOutOfRangeException), "CanNotGreaterThan", operand.ToString("d"))}")}";
         throw
             hnd.ExceptionFactory?.Invoke(message: exceptionMessage)
             ?? new ArgumentOutOfRangeException(paramName: hnd.Name, message: exceptionMessage);
     }
Example #15
0
 public static ArgumentUtilitiesHandle <string> EnsureNotNullOrEmpty(this ArgumentUtilitiesHandle <string> hnd)
 {
     if (string.IsNullOrEmpty(hnd.Value))
     {
         throw new ArgumentOutOfRangeException(hnd.Name, FormatXResource(typeof(string), "CanNotNullOrEmpty"));
     }
     else
     {
         return(hnd);
     }
 }
Example #16
0
 public static ArgumentUtilitiesHandle <Uri> EnsureAbsolute(this ArgumentUtilitiesHandle <Uri> hnd)
 {
     if (hnd.Value != null && !hnd.Value.IsAbsoluteUri)
     {
         var exceptionMessage = $"{(hnd.IsProp ? $"Указано недопустимое значение свойства '{hnd.Name}'.{Environment.NewLine}" : string.Empty)}{FormatXResource(typeof(Uri), "NotAbsoluteUri", hnd.Value)}";
         throw
             hnd.ExceptionFactory?.Invoke(message: exceptionMessage)
             ?? new ArgumentOutOfRangeException(paramName: hnd.Name, message: exceptionMessage);
     }
     return(hnd);
 }
Example #17
0
 public static ArgumentUtilitiesHandle <double> EnsureNotNegativeInfinity(this ArgumentUtilitiesHandle <double> hnd)
 {
     if (double.IsNegativeInfinity(hnd.Value))
     {
         throw new ArgumentOutOfRangeException(hnd.Name, FormatXResource(typeof(ArgumentOutOfRangeException), "CanNotNegativeInfinity"));
     }
     else
     {
         return(hnd);
     }
 }
Example #18
0
 // TODO: Put strings into the resources.
 //
 public static ArgumentUtilitiesHandle <string> EnsureNotEmptyOrWhiteSpace(this ArgumentUtilitiesHandle <string> hnd)
 {
     if (hnd.Value != null && string.IsNullOrWhiteSpace(hnd.Value))
     {
         var exceptionMessage = $"{(hnd.IsProp ? $"Invalid value of property '{hnd.Name}'.{Environment.NewLine}" : string.Empty)}{FormatXResource(typeof(string), "CanNotEmptyOrWhiteSpace")}";
         throw
             hnd.ExceptionFactory?.Invoke(message: exceptionMessage)
             ?? new ArgumentOutOfRangeException(paramName: hnd.Name, message: exceptionMessage);
     }
     return(hnd);
 }
Example #19
0
 public static ArgumentUtilitiesHandle <TTo> Cast <TFrom, TTo>(this ArgumentUtilitiesHandle <TFrom> hnd)
 {
     try {
         return(hnd.AsChanged(value: hnd.Value.Cast <TFrom, TTo>()));
     } catch (Exception exception) {
         throw
             new ArgumentException(
                 message: $"{FormatXResource(typeof(ArgumentException), "InvalidType/IncompatibleWithT", hnd.Value?.GetType() ?? typeof(TFrom), typeof(TTo))}{Environment.NewLine}Имя параметра: '{hnd.Name}'.",
                 innerException: exception);
     }
 }
Example #20
0
 public static ArgumentUtilitiesHandle <T> EnsureNotNull <T>(this ArgumentUtilitiesHandle <T> hnd)
 {
     if (hnd.Value == null)
     {
         throw Internal_CreateNullValueException(isPropertyValue: hnd.IsProp, name: hnd.Name, exceptionFactory: hnd.ExceptionFactory);
     }
     else
     {
         return(hnd);
     }
 }
Example #21
0
 public static ArgumentUtilitiesHandle <float> EnsureNotNaN(this ArgumentUtilitiesHandle <float> hnd)
 {
     if (float.IsNaN(hnd.Value))
     {
         throw new ArgumentOutOfRangeException(hnd.Name, FormatXResource(typeof(ArgumentOutOfRangeException), "CanNotNaN"));
     }
     else
     {
         return(hnd);
     }
 }
Example #22
0
 // TODO: Put strings into the resources.
 //
 public static ArgumentUtilitiesHandle <PropertyInfo> EnsureNonStaticRead(this ArgumentUtilitiesHandle <PropertyInfo> hnd, out MethodInfo getAccessor)
 {
     if (!(hnd.Value is null))
     {
         if (!PropertyInfoUtilities.IsNonStaticRead(hnd: hnd, getAccessor: out getAccessor))
         {
             throw
                 new ArgumentException(
                     message: $"Указанное свойство является недоступным для чтения.{Environment.NewLine}\tСвойство:{hnd.Value.FmtStr().GNLI2()}",
                     paramName: hnd.Name);
         }
     }
Example #23
0
 public static ArgumentUtilitiesHandle <Uri> EnsureScheme(this ArgumentUtilitiesHandle <Uri> hnd, string scheme, string altScheme)
 {
     scheme.Arg(nameof(scheme)).EnsureNotNullOrWhiteSpace();
     altScheme.Arg(nameof(altScheme)).EnsureNotNullOrWhiteSpace();
     //
     if (!(hnd.Value is null) && hnd.Value.IsAbsoluteUri && !(hnd.Value.Scheme.EqualsOrdinalCI(scheme) || hnd.Value.Scheme.EqualsOrdinalCI(altScheme)))
     {
         var exceptionMessage = $"{(hnd.IsProp ? $"Указано недопустимое значение свойства '{hnd.Name}'.{Environment.NewLine}" : string.Empty)}{FormatXResource(typeof(Uri), "NotValidScheme/Expected", hnd.Value, scheme.FmtStr().G())}";
         throw
             hnd.ExceptionFactory?.Invoke(message: exceptionMessage)
             ?? new ArgumentOutOfRangeException(paramName: hnd.Name, message: exceptionMessage);
     }
Example #24
0
 public static ArgumentUtilitiesHandle <string> EnsureHasMinLength(this ArgumentUtilitiesHandle <string> hnd, int minLength)
 {
     minLength.Arg(nameof(minLength)).EnsureNotLessThan(1);
     //
     if (hnd.Value != null && hnd.Value.Length < minLength)
     {
         var exceptionMessage = $"{(hnd.IsProp ? $"Invalid value of property '{hnd.Name}'.{Environment.NewLine}" : string.Empty)}{FormatXResource(typeof(string), "TooSmall/MinLength", minLength.ToString("d"))}{Environment.NewLine}\tДлина указанного значения (строки):{Environment.NewLine}{hnd.Value.Length.ToString("d").FmtStr().GNLI2()}";
         throw
             hnd.ExceptionFactory?.Invoke(message: exceptionMessage)
             ?? new ArgumentOutOfRangeException(message: exceptionMessage, paramName: hnd.Name);
     }
     return(hnd);
 }
Example #25
0
 // TODO: Put strings into the resources.
 //
 public static ArgumentUtilitiesHandle <Array> EnsureOneDimensional(this ArgumentUtilitiesHandle <Array> hnd)
 {
     if (!(hnd.Value is null) && hnd.Value.Rank != 1)
     {
         var exceptionMessage =
             $"{(hnd.IsProp ? $"Invalid value of property '{hnd.Name}'.{Environment.NewLine}" : string.Empty)}"
             + $"{FormatXResource(locator: typeof(Array), subpath: "NotOneDimensional", args: new object[ ] { hnd.Value.Rank.ToString("d") })}";
         throw
             hnd.ExceptionFactory?.Invoke(message: exceptionMessage)
             ?? new ArgumentOutOfRangeException(paramName: hnd.Name, message: exceptionMessage);
     }
     return(hnd);
 }
Example #26
0
 public static ArgumentUtilitiesHandle <double> EnsureNotGreaterThan(this ArgumentUtilitiesHandle <double> argument, double operand)
 {
     operand.EnsureNotNaN(nameof(operand));
     //
     if (double.IsNaN(argument.Value) || argument.Value <= operand)
     {
         return(argument);
     }
     else
     {
         throw new ArgumentOutOfRangeException(argument.Name, FormatXResource(typeof(ArgumentOutOfRangeException), "CanNotGreaterThan", operand.ToString("r")));
     }
 }
Example #27
0
 public static ArgumentUtilitiesHandle <float> EnsureNotLessThan(this ArgumentUtilitiesHandle <float> hnd, float operand)
 {
     operand.EnsureNotNaN(nameof(operand));
     //
     if (float.IsNaN(hnd.Value) || hnd.Value >= operand)
     {
         return(hnd);
     }
     else
     {
         throw new ArgumentOutOfRangeException(hnd.Name, FormatXResource(typeof(ArgumentOutOfRangeException), "CanNotLessThan", operand.ToString("r")));
     }
 }
Example #28
0
 // TODO: Put strings into the resources.
 //
 public static ArgumentUtilitiesHandle <T> EnsureEq <T>(this ArgumentUtilitiesHandle <T> hnd, T operand, bool skipNull)
 {
     if ((skipNull && hnd.Value == null) || EqualityComparer <T> .Default.Equals(x: hnd.Value, y: operand))
     {
         return(hnd);
     }
     else
     {
         var exceptionMessage = hnd.IsProp ? $"Invalid value of property '{hnd.Name}'.{Environment.NewLine}\tExpected value:{operand.FmtStr().GNLI2()}" : $"Expected value:{operand.FmtStr().GNLI2()}";
         throw
             hnd.ExceptionFactory?.Invoke(message: exceptionMessage)
             ?? new ArgumentOutOfRangeException(message: exceptionMessage, paramName: hnd.Name);
     }
 }
Example #29
0
 // TODO: Put strings into the resources.
 //
 public static ArgumentUtilitiesHandle <T> EnsureNotEq <T>(this ArgumentUtilitiesHandle <T> hnd, T operand)
 {
     if (EqualityComparer <T> .Default.Equals(x: hnd.Value, y: operand))
     {
         var exceptionMessage = (hnd.IsProp ? $"Invalid value of property '{hnd.Name}'.{Environment.NewLine}" : string.Empty) + $"Value cannot be '{operand.FmtStr().G()}'.";
         throw
             hnd.ExceptionFactory?.Invoke(message: exceptionMessage)
             ?? new ArgumentOutOfRangeException(message: exceptionMessage, paramName: hnd.Name);
     }
     else
     {
         return(hnd);
     }
 }
Example #30
0
 public static ArgumentUtilitiesHandle <int?> EnsureNotLessThan(this ArgumentUtilitiesHandle <int?> hnd, int operand)
 {
     if (hnd.Value < operand)
     {
         var exceptionMessage = $"{(hnd.IsProp ? $"Указано недопустимое значение свойства '{hnd.Name}'.{Environment.NewLine}" : string.Empty)}{FormatXResource(typeof(ArgumentOutOfRangeException), "CanNotLessThan", operand.ToString("d"))}";
         throw
             hnd.ExceptionFactory?.Invoke(message: exceptionMessage)
             ?? new ArgumentOutOfRangeException(paramName: hnd.Name, message: exceptionMessage);
     }
     else
     {
         return(hnd);
     }
 }