diff --git a/src/Fluxera.ComponentModel.Annotations/ContainsAttribute.cs b/src/Fluxera.ComponentModel.Annotations/ContainsAttribute.cs index d7af026..452fcef 100644 --- a/src/Fluxera.ComponentModel.Annotations/ContainsAttribute.cs +++ b/src/Fluxera.ComponentModel.Annotations/ContainsAttribute.cs @@ -2,7 +2,6 @@ { using System; using System.ComponentModel.DataAnnotations; - using Fluxera.Guards; using JetBrains.Annotations; /// @@ -18,7 +17,7 @@ public sealed class ContainsAttribute : ValidationAttribute /// public ContainsAttribute(string part) { - Guard.Against.NullOrEmpty(part, nameof(part)); + Guard.ThrowIfNull(part); this.Part = part; } diff --git a/src/Fluxera.ComponentModel.Annotations/EndsWithAttribute.cs b/src/Fluxera.ComponentModel.Annotations/EndsWithAttribute.cs index 4f2b001..930b638 100644 --- a/src/Fluxera.ComponentModel.Annotations/EndsWithAttribute.cs +++ b/src/Fluxera.ComponentModel.Annotations/EndsWithAttribute.cs @@ -2,7 +2,6 @@ { using System; using System.ComponentModel.DataAnnotations; - using Fluxera.Guards; using JetBrains.Annotations; /// @@ -19,7 +18,7 @@ public sealed class EndsWithAttribute : ValidationAttribute /// public EndsWithAttribute(string end, StringComparison stringComparison = StringComparison.InvariantCultureIgnoreCase) { - Guard.Against.NullOrEmpty(end, nameof(end)); + Guard.ThrowIfNullOrEmpty(end); this.End = end; this.StringComparison = stringComparison; diff --git a/src/Fluxera.ComponentModel.Annotations/Fluxera.ComponentModel.Annotations.csproj b/src/Fluxera.ComponentModel.Annotations/Fluxera.ComponentModel.Annotations.csproj index 01802f9..d5f5237 100644 --- a/src/Fluxera.ComponentModel.Annotations/Fluxera.ComponentModel.Annotations.csproj +++ b/src/Fluxera.ComponentModel.Annotations/Fluxera.ComponentModel.Annotations.csproj @@ -22,7 +22,6 @@ - all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/Fluxera.ComponentModel.Annotations/Guard.cs b/src/Fluxera.ComponentModel.Annotations/Guard.cs new file mode 100644 index 0000000..17f48af --- /dev/null +++ b/src/Fluxera.ComponentModel.Annotations/Guard.cs @@ -0,0 +1,122 @@ +namespace Fluxera.ComponentModel.Annotations +{ + using System; + using System.Collections.Generic; + using System.Linq; + using System.Numerics; + using System.Runtime.CompilerServices; + using JetBrains.Annotations; + + internal static class Guard + { + public static T ThrowIfNull(T argument, [InvokerParameterName] [CallerArgumentExpression(nameof(argument))] string parameterName = null) + { + ArgumentNullException.ThrowIfNull(argument, parameterName); + + return argument; + } + + public static string ThrowIfNullOrEmpty(string argument, [InvokerParameterName][CallerArgumentExpression(nameof(argument))] string parameterName = null) + { + argument = ThrowIfNull(argument, parameterName); + + if(string.IsNullOrEmpty(argument)) + { + throw new ArgumentException("Value cannot be empty.", parameterName); + } + + return argument; + } + + public static string ThrowIfNullOrWhiteSpace(string argument, [InvokerParameterName][CallerArgumentExpression("argument")] string parameterName = null) + { + argument = ThrowIfNull(argument, parameterName); + + if(string.IsNullOrWhiteSpace(argument)) + { + throw new ArgumentException("Value cannot be whitespace-only.", parameterName); + } + + return argument; + } + + public static bool ThrowIfFalse(bool argument, [InvokerParameterName][CallerArgumentExpression(nameof(argument))] string parameterName = null, string message = null) + { + if(!argument) + { + throw new ArgumentException(message ?? "Value cannot be false.", parameterName); + } + + return true; + } + + public static IEnumerable ThrowIfNullOrEmpty(IEnumerable argument, [InvokerParameterName][CallerArgumentExpression(nameof(argument))] string parameterName = null) + { + argument = ThrowIfNull(argument, parameterName); + + // ReSharper disable PossibleMultipleEnumeration + if(!argument.Any()) + { + throw new ArgumentException("Enumerable cannot be empty.", parameterName); + } + + return argument; + // ReSharper enable PossibleMultipleEnumeration + } + +#if NET7_0_OR_GREATER + public static T ThrowIfNegative(T argument, [InvokerParameterName] [CallerArgumentExpression(nameof(argument))] string parameterName = null) + where T : INumber + { + if(T.IsNegative(argument)) + { + throw new ArgumentException("Value cannot be negative.", parameterName); + } + + return argument; + } +#endif + +#if NET6_0 + public static byte ThrowIfNegative(byte argument, [InvokerParameterName][CallerArgumentExpression(nameof(argument))] string parameterName = null) + { + if(argument < 0) + { + throw new ArgumentException("Value cannot be negative.", parameterName); + } + + return argument; + } + + public static short ThrowIfNegative(short argument, [InvokerParameterName][CallerArgumentExpression(nameof(argument))] string parameterName = null) + { + if(argument < 0) + { + throw new ArgumentException("Value cannot be negative.", parameterName); + } + + return argument; + } + + public static int ThrowIfNegative(int argument, [InvokerParameterName] [CallerArgumentExpression(nameof(argument))] string parameterName = null) + { + if(argument < 0) + { + throw new ArgumentException("Value cannot be negative.", parameterName); + } + + return argument; + } + + public static long ThrowIfNegative(long argument, [InvokerParameterName][CallerArgumentExpression(nameof(argument))] string parameterName = null) + { + if(argument < 0) + { + throw new ArgumentException("Value cannot be negative.", parameterName); + } + + return argument; + } +#endif + } +} diff --git a/src/Fluxera.ComponentModel.Annotations/ListLengthAttribute.cs b/src/Fluxera.ComponentModel.Annotations/ListLengthAttribute.cs index 26a973e..e167029 100644 --- a/src/Fluxera.ComponentModel.Annotations/ListLengthAttribute.cs +++ b/src/Fluxera.ComponentModel.Annotations/ListLengthAttribute.cs @@ -4,7 +4,6 @@ using System.Collections; using System.ComponentModel.DataAnnotations; using System.Linq; - using Fluxera.Guards; using JetBrains.Annotations; /// @@ -20,7 +19,7 @@ public sealed class ListLengthAttribute : ValidationAttribute /// public ListLengthAttribute(int maximumLength) { - Guard.Against.Negative(maximumLength, nameof(maximumLength)); + Guard.ThrowIfNegative(maximumLength); this.MaximumLength = maximumLength; } diff --git a/src/Fluxera.ComponentModel.Annotations/ListMaxLengthAttribute.cs b/src/Fluxera.ComponentModel.Annotations/ListMaxLengthAttribute.cs index afca03f..0c9e58a 100644 --- a/src/Fluxera.ComponentModel.Annotations/ListMaxLengthAttribute.cs +++ b/src/Fluxera.ComponentModel.Annotations/ListMaxLengthAttribute.cs @@ -4,7 +4,6 @@ using System.Collections; using System.ComponentModel.DataAnnotations; using System.Linq; - using Fluxera.Guards; using JetBrains.Annotations; /// @@ -20,7 +19,7 @@ public sealed class ListMaxLengthAttribute : ValidationAttribute /// public ListMaxLengthAttribute(int maximumLength) { - Guard.Against.Negative(maximumLength, nameof(maximumLength)); + Guard.ThrowIfNegative(maximumLength); this.MaximumLength = maximumLength; } diff --git a/src/Fluxera.ComponentModel.Annotations/ListMinLengthAttribute.cs b/src/Fluxera.ComponentModel.Annotations/ListMinLengthAttribute.cs index 903cb39..de59088 100644 --- a/src/Fluxera.ComponentModel.Annotations/ListMinLengthAttribute.cs +++ b/src/Fluxera.ComponentModel.Annotations/ListMinLengthAttribute.cs @@ -4,7 +4,6 @@ using System.Collections; using System.ComponentModel.DataAnnotations; using System.Linq; - using Fluxera.Guards; using JetBrains.Annotations; /// @@ -20,7 +19,7 @@ public sealed class ListMinLengthAttribute : ValidationAttribute /// public ListMinLengthAttribute(int minimumLength) { - Guard.Against.Negative(minimumLength, nameof(minimumLength)); + Guard.ThrowIfNegative(minimumLength); this.MinimumLength = minimumLength; } diff --git a/src/Fluxera.ComponentModel.Annotations/ReferenceAttribute.cs b/src/Fluxera.ComponentModel.Annotations/ReferenceAttribute.cs index 2cbff6c..26116cc 100644 --- a/src/Fluxera.ComponentModel.Annotations/ReferenceAttribute.cs +++ b/src/Fluxera.ComponentModel.Annotations/ReferenceAttribute.cs @@ -1,7 +1,6 @@ namespace Fluxera.ComponentModel.Annotations { using System; - using Fluxera.Guards; using JetBrains.Annotations; /// @@ -30,7 +29,7 @@ public class ReferenceAttribute : Attribute /// public ReferenceAttribute(Type referencedEntityType) { - Guard.Against.Null(referencedEntityType); + Guard.ThrowIfNull(referencedEntityType); this.ReferencedEntityName = referencedEntityType.Name; } @@ -41,7 +40,7 @@ public ReferenceAttribute(Type referencedEntityType) /// public ReferenceAttribute(string referencedEntityName) { - Guard.Against.NullOrWhiteSpace(referencedEntityName); + Guard.ThrowIfNullOrWhiteSpace(referencedEntityName); this.ReferencedEntityName = referencedEntityName; } diff --git a/src/Fluxera.ComponentModel.Annotations/RequiredIfAttribute.cs b/src/Fluxera.ComponentModel.Annotations/RequiredIfAttribute.cs index ba03cd9..acd91a8 100644 --- a/src/Fluxera.ComponentModel.Annotations/RequiredIfAttribute.cs +++ b/src/Fluxera.ComponentModel.Annotations/RequiredIfAttribute.cs @@ -2,7 +2,6 @@ { using System; using System.ComponentModel.DataAnnotations; - using Fluxera.Guards; using JetBrains.Annotations; /// @@ -25,8 +24,8 @@ public sealed class RequiredIfAttribute : RequiredAttribute /// The desired values are OR-ed when evaluating. public RequiredIfAttribute([InvokerParameterName] string propertyName, params object[] desiredValues) { - Guard.Against.NullOrWhiteSpace(propertyName, nameof(propertyName)); - Guard.Against.Null(desiredValues, nameof(desiredValues)); + Guard.ThrowIfNullOrWhiteSpace(propertyName); + Guard.ThrowIfNull(desiredValues); this.propertyName = propertyName; this.desiredValues = desiredValues; diff --git a/src/Fluxera.ComponentModel.Annotations/StartsWithAttribute.cs b/src/Fluxera.ComponentModel.Annotations/StartsWithAttribute.cs index 8106f4a..e8339cb 100644 --- a/src/Fluxera.ComponentModel.Annotations/StartsWithAttribute.cs +++ b/src/Fluxera.ComponentModel.Annotations/StartsWithAttribute.cs @@ -2,7 +2,6 @@ { using System; using System.ComponentModel.DataAnnotations; - using Fluxera.Guards; using JetBrains.Annotations; /// @@ -19,7 +18,7 @@ public sealed class StartsWithAttribute : ValidationAttribute /// public StartsWithAttribute(string start, StringComparison stringComparison = StringComparison.InvariantCultureIgnoreCase) { - Guard.Against.NullOrEmpty(start, nameof(start)); + Guard.ThrowIfNullOrEmpty(start); this.Start = start; this.StringComparison = stringComparison; diff --git a/src/Fluxera.Linq.Expressions/Fluxera.Linq.Expressions.csproj b/src/Fluxera.Linq.Expressions/Fluxera.Linq.Expressions.csproj index b0099b9..f5e2bff 100644 --- a/src/Fluxera.Linq.Expressions/Fluxera.Linq.Expressions.csproj +++ b/src/Fluxera.Linq.Expressions/Fluxera.Linq.Expressions.csproj @@ -22,7 +22,6 @@ - all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/Fluxera.Utilities/Collections/SortedList.cs b/src/Fluxera.Utilities/Collections/SortedList.cs index 34dca93..75b1798 100644 --- a/src/Fluxera.Utilities/Collections/SortedList.cs +++ b/src/Fluxera.Utilities/Collections/SortedList.cs @@ -4,7 +4,7 @@ namespace Fluxera.Collections.Generic using System; using System.Collections; using System.Collections.Generic; - using Guards; + using Fluxera.Utilities; using JetBrains.Annotations; /// @@ -41,7 +41,7 @@ public SortedList(Comparer comparer) /// public void Add(T item) { - Guard.Against.Null(item, nameof(item)); + Guard.ThrowIfNull(item); int position = this.innerList.BinarySearch(item, this.comparer); if (position < 0) diff --git a/src/Fluxera.Utilities/Collections/TypeList.cs b/src/Fluxera.Utilities/Collections/TypeList.cs index c734021..5d96fa9 100644 --- a/src/Fluxera.Utilities/Collections/TypeList.cs +++ b/src/Fluxera.Utilities/Collections/TypeList.cs @@ -6,7 +6,7 @@ namespace Fluxera.Collections.Generic using System.Collections; using System.Collections.Generic; using System.Reflection; - using Fluxera.Guards; + using Fluxera.Utilities; using JetBrains.Annotations; /// @@ -20,7 +20,7 @@ public class TypeList : ITypeList private readonly List innerList; /// - /// Creates a new instance of the type.. + /// Creates a new instance of the type. /// public TypeList() { @@ -138,7 +138,7 @@ public void Remove() where T : TBaseType private static void CheckType(Type item) { - item = Guard.Against.Null(item); + item = Guard.ThrowIfNull(item); if(!typeof(TBaseType).GetTypeInfo().IsAssignableFrom(item)) { diff --git a/src/Fluxera.Utilities/Extensions/Array/IsIndexWithin.cs b/src/Fluxera.Utilities/Extensions/Array/IsIndexWithin.cs index 71cc018..e07a0c7 100644 --- a/src/Fluxera.Utilities/Extensions/Array/IsIndexWithin.cs +++ b/src/Fluxera.Utilities/Extensions/Array/IsIndexWithin.cs @@ -3,7 +3,6 @@ namespace Fluxera.Utilities.Extensions { using System; - using Fluxera.Guards; using JetBrains.Annotations; /// @@ -20,7 +19,7 @@ public static partial class ArrayExtensions /// True, if the index lies within the array. public static bool IsIndexWithin(this Array source, int index) { - Guard.Against.Null(source, nameof(source)); + Guard.ThrowIfNull(source); return index >= 0 && index < source.Length; } diff --git a/src/Fluxera.Utilities/Extensions/Assembly/GetResourceAsStringAsync.cs b/src/Fluxera.Utilities/Extensions/Assembly/GetResourceAsStringAsync.cs index 71e2e2f..d8ee9bc 100644 --- a/src/Fluxera.Utilities/Extensions/Assembly/GetResourceAsStringAsync.cs +++ b/src/Fluxera.Utilities/Extensions/Assembly/GetResourceAsStringAsync.cs @@ -5,7 +5,6 @@ namespace Fluxera.Utilities.Extensions using System.IO; using System.Reflection; using System.Threading.Tasks; - using Fluxera.Guards; using JetBrains.Annotations; /// @@ -22,7 +21,7 @@ public static class AssemblyExtensions /// The content of the resource. public static async Task GetResourceAsStringAsync(this Assembly assembly, string resourceLocation) { - Guard.Against.Null(assembly, nameof(assembly)); + Guard.ThrowIfNull(assembly); string result = string.Empty; diff --git a/src/Fluxera.Utilities/Extensions/Char/Repeat.cs b/src/Fluxera.Utilities/Extensions/Char/Repeat.cs index 0d1b85d..df858c4 100644 --- a/src/Fluxera.Utilities/Extensions/Char/Repeat.cs +++ b/src/Fluxera.Utilities/Extensions/Char/Repeat.cs @@ -2,7 +2,6 @@ namespace Fluxera.Utilities.Extensions { - using Fluxera.Guards; using JetBrains.Annotations; /// @@ -21,7 +20,7 @@ public static class CharExtensions [Pure] public static string Repeat(this char input, int count) { - Guard.Against.Negative(count, nameof(count)); + Guard.ThrowIfNegative(count); string str = string.Empty; count.Times(() => str = string.Concat(str, input)); @@ -39,7 +38,7 @@ public static string Repeat(this char input, int count) [Pure] public static string Repeat(this char input, int count) { - Guard.Against.Negative(count, nameof(count)); + Guard.ThrowIfNegative(count); string str = string.Empty; count.Times(() => str = string.Concat(str, input)); diff --git a/src/Fluxera.Utilities/Extensions/Collection/AddFor.cs b/src/Fluxera.Utilities/Extensions/Collection/AddFor.cs index a35605e..50212f6 100644 --- a/src/Fluxera.Utilities/Extensions/Collection/AddFor.cs +++ b/src/Fluxera.Utilities/Extensions/Collection/AddFor.cs @@ -4,7 +4,6 @@ namespace Fluxera.Utilities.Extensions { using System; using System.Collections.Generic; - using Fluxera.Guards; /// /// Extension methods for the type. @@ -20,7 +19,7 @@ public static partial class CollectionExtensions /// The predicate to match by the elements to add. public static void AddFor(this ICollection target, T value, Predicate predicate) { - Guard.Against.Null(target, nameof(target)); + Guard.ThrowIfNull(target); if(predicate(value)) { diff --git a/src/Fluxera.Utilities/Extensions/Collection/AddIfNotContains.cs b/src/Fluxera.Utilities/Extensions/Collection/AddIfNotContains.cs index f470757..203b6c6 100644 --- a/src/Fluxera.Utilities/Extensions/Collection/AddIfNotContains.cs +++ b/src/Fluxera.Utilities/Extensions/Collection/AddIfNotContains.cs @@ -5,7 +5,6 @@ namespace Fluxera.Utilities.Extensions using System; using System.Collections.Generic; using System.Linq; - using Fluxera.Guards; using JetBrains.Annotations; /// @@ -23,7 +22,7 @@ public static partial class CollectionExtensions /// Returns True if added, returns False if not. public static bool AddIfNotContains(this ICollection collection, T item) { - Guard.Against.Null(collection, nameof(collection)); + Guard.ThrowIfNull(collection); if(collection.Contains(item)) { @@ -43,7 +42,7 @@ public static bool AddIfNotContains(this ICollection collection, T item) /// Returns the added items. public static IEnumerable AddIfNotContains(this ICollection collection, IEnumerable items) { - Guard.Against.Null(collection, nameof(collection)); + Guard.ThrowIfNull(collection); IList addedItems = new List(); @@ -73,9 +72,9 @@ public static IEnumerable AddIfNotContains(this ICollection collection, public static bool AddIfNotContains(this ICollection collection, Func predicate, Func itemFactory) { - Guard.Against.Null(collection, nameof(collection)); - Guard.Against.Null(predicate, nameof(predicate)); - Guard.Against.Null(itemFactory, nameof(itemFactory)); + Guard.ThrowIfNull(collection); + Guard.ThrowIfNull(predicate); + Guard.ThrowIfNull(itemFactory); if(collection.Any(predicate)) { @@ -97,9 +96,9 @@ public static bool AddIfNotContains(this ICollection collection, /// Returns True if added, returns False if not. public static bool AddIfNotContains(this ICollection collection, Predicate predicate, Func itemFactory) { - Guard.Against.Null(collection, nameof(collection)); - Guard.Against.Null(predicate, nameof(predicate)); - Guard.Against.Null(itemFactory, nameof(itemFactory)); + Guard.ThrowIfNull(collection); + Guard.ThrowIfNull(predicate); + Guard.ThrowIfNull(itemFactory); if(collection.Any(x => predicate(x))) { diff --git a/src/Fluxera.Utilities/Extensions/Collection/AddRange.cs b/src/Fluxera.Utilities/Extensions/Collection/AddRange.cs index bb825db..7650691 100644 --- a/src/Fluxera.Utilities/Extensions/Collection/AddRange.cs +++ b/src/Fluxera.Utilities/Extensions/Collection/AddRange.cs @@ -3,7 +3,6 @@ namespace Fluxera.Utilities.Extensions { using System.Collections.Generic; - using Fluxera.Guards; /// /// Extension methods for the type. @@ -18,8 +17,8 @@ public static partial class CollectionExtensions /// The elements to add. public static void AddRange(this ICollection target, IEnumerable values) { - target = Guard.Against.Null(target); - values = Guard.Against.Null(values); + target = Guard.ThrowIfNull(target); + values = Guard.ThrowIfNull(values); foreach(T value in values) { diff --git a/src/Fluxera.Utilities/Extensions/Collection/AddRangeFor.cs b/src/Fluxera.Utilities/Extensions/Collection/AddRangeFor.cs index dcaab80..dac6d80 100644 --- a/src/Fluxera.Utilities/Extensions/Collection/AddRangeFor.cs +++ b/src/Fluxera.Utilities/Extensions/Collection/AddRangeFor.cs @@ -4,7 +4,6 @@ namespace Fluxera.Utilities.Extensions { using System; using System.Collections.Generic; - using Fluxera.Guards; /// /// Extension methods for the type. @@ -20,8 +19,8 @@ public static partial class CollectionExtensions /// The predicate to match. public static void AddRangeFor(this ICollection target, IEnumerable values, Predicate predicate) { - target = Guard.Against.Null(target); - values = Guard.Against.Null(values); + target = Guard.ThrowIfNull(target); + values = Guard.ThrowIfNull(values); foreach(T value in values) { diff --git a/src/Fluxera.Utilities/Extensions/Collection/RemoveMatching.cs b/src/Fluxera.Utilities/Extensions/Collection/RemoveMatching.cs index ff1cd52..0900dcf 100644 --- a/src/Fluxera.Utilities/Extensions/Collection/RemoveMatching.cs +++ b/src/Fluxera.Utilities/Extensions/Collection/RemoveMatching.cs @@ -5,7 +5,6 @@ namespace Fluxera.Utilities.Extensions using System; using System.Collections.Generic; using System.Linq; - using Fluxera.Guards; /// /// Extension methods for the type. @@ -21,8 +20,8 @@ public static partial class CollectionExtensions /// public static void RemoveMatching(this ICollection collection, Func predicate) { - Guard.Against.Null(collection, nameof(collection)); - Guard.Against.Null(predicate, nameof(predicate)); + Guard.ThrowIfNull(collection); + Guard.ThrowIfNull(predicate); IList deleteList = collection.Where(predicate).ToList(); deleteList.ForEach(x => collection.Remove(x)); diff --git a/src/Fluxera.Utilities/Extensions/DateTime/GetFirstDayOfWeek.cs b/src/Fluxera.Utilities/Extensions/DateTime/GetFirstDayOfWeek.cs index 03f43f0..5605326 100644 --- a/src/Fluxera.Utilities/Extensions/DateTime/GetFirstDayOfWeek.cs +++ b/src/Fluxera.Utilities/Extensions/DateTime/GetFirstDayOfWeek.cs @@ -4,7 +4,6 @@ namespace Fluxera.Utilities.Extensions { using System; using System.Globalization; - using Fluxera.Guards; /// /// Extension methods for the type. @@ -29,7 +28,7 @@ public static DateTime GetFirstDayOfWeek(this DateTime date) /// The first day of the week. public static DateTime GetFirstDayOfWeek(this DateTime date, CultureInfo cultureInfo) { - Guard.Against.Null(cultureInfo, nameof(cultureInfo)); + Guard.ThrowIfNull(cultureInfo); DayOfWeek firstDayOfWeek = cultureInfo.DateTimeFormat.FirstDayOfWeek; while(date.DayOfWeek != firstDayOfWeek) diff --git a/src/Fluxera.Utilities/Extensions/DateTime/GetLastDayOfWeek.cs b/src/Fluxera.Utilities/Extensions/DateTime/GetLastDayOfWeek.cs index eaf8ee6..0e2d20a 100644 --- a/src/Fluxera.Utilities/Extensions/DateTime/GetLastDayOfWeek.cs +++ b/src/Fluxera.Utilities/Extensions/DateTime/GetLastDayOfWeek.cs @@ -4,7 +4,6 @@ namespace Fluxera.Utilities.Extensions { using System; using System.Globalization; - using Fluxera.Guards; /// /// Extension methods for the type. @@ -29,7 +28,7 @@ public static DateTime GetLastDayOfWeek(this DateTime date) /// The last day of the week public static DateTime GetLastDayOfWeek(this DateTime date, CultureInfo cultureInfo) { - Guard.Against.Null(cultureInfo, nameof(cultureInfo)); + Guard.ThrowIfNull(cultureInfo); return date.GetFirstDayOfWeek(cultureInfo).AddDays(6); } diff --git a/src/Fluxera.Utilities/Extensions/DateTime/GetWeeksWeekday.cs b/src/Fluxera.Utilities/Extensions/DateTime/GetWeeksWeekday.cs index 2d66729..871e98d 100644 --- a/src/Fluxera.Utilities/Extensions/DateTime/GetWeeksWeekday.cs +++ b/src/Fluxera.Utilities/Extensions/DateTime/GetWeeksWeekday.cs @@ -4,7 +4,6 @@ namespace Fluxera.Utilities.Extensions { using System; using System.Globalization; - using Fluxera.Guards; /// /// Extension methods for the type. @@ -31,7 +30,7 @@ public static DateTime GetWeeksWeekday(this DateTime date, DayOfWeek weekday) /// The calculated date. public static DateTime GetWeeksWeekday(this DateTime date, DayOfWeek weekday, CultureInfo cultureInfo) { - Guard.Against.Null(cultureInfo, nameof(cultureInfo)); + Guard.ThrowIfNull(cultureInfo); DateTime firstDayOfWeek = date.GetFirstDayOfWeek(cultureInfo); return firstDayOfWeek.GetNextWeekday(weekday); diff --git a/src/Fluxera.Utilities/Extensions/Dictionary/ContainsValue.cs b/src/Fluxera.Utilities/Extensions/Dictionary/ContainsValue.cs index ea64347..c0f8eed 100644 --- a/src/Fluxera.Utilities/Extensions/Dictionary/ContainsValue.cs +++ b/src/Fluxera.Utilities/Extensions/Dictionary/ContainsValue.cs @@ -5,7 +5,6 @@ namespace Fluxera.Utilities.Extensions using System; using System.Collections.Generic; using System.Linq; - using Fluxera.Guards; using JetBrains.Annotations; /// @@ -25,7 +24,7 @@ public static partial class DictionaryExtensions /// public static bool ContainsValue(this IDictionary dictionary, TValue value) { - Guard.Against.Null(dictionary, nameof(dictionary)); + Guard.ThrowIfNull(dictionary); return dictionary.Values.Any(x => x.Equals(value)); } diff --git a/src/Fluxera.Utilities/Extensions/Enumerable/AsReadOnly.cs b/src/Fluxera.Utilities/Extensions/Enumerable/AsReadOnly.cs index c6c27b7..ff27080 100644 --- a/src/Fluxera.Utilities/Extensions/Enumerable/AsReadOnly.cs +++ b/src/Fluxera.Utilities/Extensions/Enumerable/AsReadOnly.cs @@ -5,7 +5,6 @@ namespace Fluxera.Utilities.Extensions using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; - using Fluxera.Guards; using JetBrains.Annotations; /// @@ -22,7 +21,7 @@ public static partial class EnumerableExtensions /// A write-protected collection. public static IReadOnlyCollection AsReadOnly(this IEnumerable enumerable) { - enumerable = Guard.Against.Null(enumerable); + enumerable = Guard.ThrowIfNull(enumerable); return new ReadOnlyCollection(enumerable.ToList()); } diff --git a/src/Fluxera.Utilities/Extensions/Enumerable/ForEach.cs b/src/Fluxera.Utilities/Extensions/Enumerable/ForEach.cs index 533a091..18fbcd9 100644 --- a/src/Fluxera.Utilities/Extensions/Enumerable/ForEach.cs +++ b/src/Fluxera.Utilities/Extensions/Enumerable/ForEach.cs @@ -4,7 +4,6 @@ namespace Fluxera.Utilities.Extensions { using System; using System.Collections.Generic; - using Fluxera.Guards; /// /// Extension methods for the type. @@ -19,8 +18,8 @@ public static partial class EnumerableExtensions /// The action execute. public static void ForEach(this IEnumerable enumerable, Action action) { - enumerable = Guard.Against.Null(enumerable); - action = Guard.Against.Null(action); + enumerable = Guard.ThrowIfNull(enumerable); + action = Guard.ThrowIfNull(action); foreach(T item in enumerable) { diff --git a/src/Fluxera.Utilities/Extensions/Inflector.cs b/src/Fluxera.Utilities/Extensions/Inflector.cs index 3020b48..a7d94d9 100644 --- a/src/Fluxera.Utilities/Extensions/Inflector.cs +++ b/src/Fluxera.Utilities/Extensions/Inflector.cs @@ -13,9 +13,9 @@ [PublicAPI] internal static class Inflector { - private static readonly List plurals = new List(); - private static readonly List singulars = new List(); - private static readonly List uncountables = new List(); + private static readonly List Plurals = new List(); + private static readonly List Singulars = new List(); + private static readonly List Uncountables = new List(); static Inflector() { @@ -92,34 +92,34 @@ private static void AddIrregular(string singular, string plural) private static void AddUncountable(string word) { - uncountables.Add(word.ToLower()); + Uncountables.Add(word.ToLower()); } private static void AddPlural(string rule, string replacement) { - plurals.Add(new Rule(rule, replacement)); + Plurals.Add(new Rule(rule, replacement)); } private static void AddSingular(string rule, string replacement) { - singulars.Add(new Rule(rule, replacement)); + Singulars.Add(new Rule(rule, replacement)); } internal static string Pluralize(string word) { - return ApplyRules(plurals, word); + return ApplyRules(Plurals, word); } internal static string Singularize(string word) { - return ApplyRules(singulars, word); + return ApplyRules(Singulars, word); } private static string ApplyRules(IReadOnlyList rules, string word) { string result = word; - if(!uncountables.Contains(word.ToLower())) + if(!Uncountables.Contains(word.ToLower())) { for(int i = rules.Count - 1; i >= 0; i--) { diff --git a/src/Fluxera.Utilities/Extensions/List/AddRangeSorted.cs b/src/Fluxera.Utilities/Extensions/List/AddRangeSorted.cs index 69d4023..dfbe154 100644 --- a/src/Fluxera.Utilities/Extensions/List/AddRangeSorted.cs +++ b/src/Fluxera.Utilities/Extensions/List/AddRangeSorted.cs @@ -3,8 +3,6 @@ namespace Fluxera.Utilities.Extensions { using System.Collections.Generic; - using System.Linq; - using Fluxera.Guards; /// /// Extension methods for the type. @@ -20,9 +18,9 @@ public static partial class ListExtensions /// The optional item comparer. public static void AddRangeSorted(this List target, IEnumerable items, IComparer comparer = null) { - Guard.Against.Null(target, nameof(target)); + Guard.ThrowIfNull(target); - items ??= Enumerable.Empty(); + items ??= []; foreach(T item in items) { diff --git a/src/Fluxera.Utilities/Extensions/List/AddSorted.cs b/src/Fluxera.Utilities/Extensions/List/AddSorted.cs index 4cecdb1..9baddbb 100644 --- a/src/Fluxera.Utilities/Extensions/List/AddSorted.cs +++ b/src/Fluxera.Utilities/Extensions/List/AddSorted.cs @@ -3,7 +3,6 @@ namespace Fluxera.Utilities.Extensions { using System.Collections.Generic; - using Fluxera.Guards; using JetBrains.Annotations; /// @@ -21,8 +20,8 @@ public static partial class ListExtensions /// The optional item comparer. public static void AddSorted(this List target, T item, IComparer comparer = null) { - Guard.Against.Null(target, nameof(target)); - Guard.Against.Null(item, nameof(item)); + Guard.ThrowIfNull(target, nameof(target)); + Guard.ThrowIfNull(item, nameof(item)); comparer ??= Comparer.Default; diff --git a/src/Fluxera.Utilities/Extensions/MemoryStream/ToContentString.cs b/src/Fluxera.Utilities/Extensions/MemoryStream/ToContentString.cs index 154b208..0e78a19 100644 --- a/src/Fluxera.Utilities/Extensions/MemoryStream/ToContentString.cs +++ b/src/Fluxera.Utilities/Extensions/MemoryStream/ToContentString.cs @@ -5,7 +5,6 @@ namespace Fluxera.Utilities.Extensions using System.IO; using System.Text; using System.Threading.Tasks; - using Fluxera.Guards; using JetBrains.Annotations; /// @@ -32,8 +31,8 @@ public static Task ToContentString(this MemoryStream stream) /// The content of the stream as string. public static async Task ToContentString(this MemoryStream stream, Encoding encoding) { - Guard.Against.Null(stream, nameof(stream)); - Guard.Against.Null(encoding, nameof(encoding)); + Guard.ThrowIfNull(stream); + Guard.ThrowIfNull(encoding); using(StreamReader reader = new StreamReader(stream, encoding)) { diff --git a/src/Fluxera.Utilities/Extensions/Numeric/Ordinalize.cs b/src/Fluxera.Utilities/Extensions/Numeric/Ordinalize.cs index e5e3c16..5ac9c29 100644 --- a/src/Fluxera.Utilities/Extensions/Numeric/Ordinalize.cs +++ b/src/Fluxera.Utilities/Extensions/Numeric/Ordinalize.cs @@ -2,8 +2,6 @@ namespace Fluxera.Utilities.Extensions { - using Fluxera.Guards; - /// /// Extension methods for numeric types. /// @@ -16,7 +14,7 @@ public static partial class NumericExtensions /// The ordinal number as string. public static string Ordinalize(this byte number) { - Guard.Against.Negative(number, nameof(number)); + Guard.ThrowIfNegative(number); return Inflector.Ordinalize(number); } @@ -28,7 +26,7 @@ public static string Ordinalize(this byte number) /// The ordinal number as string. public static string Ordinalize(this short number) { - Guard.Against.Negative(number, nameof(number)); + Guard.ThrowIfNegative(number); return Inflector.Ordinalize(number); } @@ -40,7 +38,7 @@ public static string Ordinalize(this short number) /// The ordinal number as string. public static string Ordinalize(this int number) { - Guard.Against.Negative(number, nameof(number)); + Guard.ThrowIfNegative(number); return Inflector.Ordinalize(number); } @@ -52,7 +50,7 @@ public static string Ordinalize(this int number) /// The ordinal number as string. public static string Ordinalize(this long number) { - Guard.Against.Negative(number, nameof(number)); + Guard.ThrowIfNegative(number); return Inflector.Ordinalize(number); } diff --git a/src/Fluxera.Utilities/Extensions/Numeric/Times.cs b/src/Fluxera.Utilities/Extensions/Numeric/Times.cs index 3385fa4..3b18f49 100644 --- a/src/Fluxera.Utilities/Extensions/Numeric/Times.cs +++ b/src/Fluxera.Utilities/Extensions/Numeric/Times.cs @@ -4,7 +4,6 @@ namespace Fluxera.Utilities.Extensions { using System; using System.Numerics; - using Fluxera.Guards; using JetBrains.Annotations; /// @@ -23,9 +22,8 @@ public static partial class NumericExtensions /// public static void Times(this T value, Action action) where T : INumber { - // Against Negative - Guard.Against.False(T.IsPositive(value), message: "The number must be positive."); - Guard.Against.Null(action, nameof(action)); + Guard.ThrowIfNegative(value); + Guard.ThrowIfNull(action); for(T i = T.Zero; i < value; i++) { @@ -42,9 +40,8 @@ public static void Times(this T value, Action action) where T : INumber /// public static void Times(this T value, Action action) where T : INumber { - // Against Negative - Guard.Against.False(T.IsPositive(value), message: "The number must be positive."); - Guard.Against.Null(action, nameof(action)); + Guard.ThrowIfNegative(value); + Guard.ThrowIfNull(action); for(T i = T.Zero; i < value; i++) { @@ -63,8 +60,8 @@ public static void Times(this T value, Action action) where T : INumber /// public static void Times(this byte value, Action action) { - Guard.Against.Negative(value, nameof(value)); - Guard.Against.Null(action, nameof(action)); + Guard.ThrowIfNegative(value); + Guard.ThrowIfNull(action); Times(value, action); } @@ -78,8 +75,8 @@ public static void Times(this byte value, Action action) /// public static void Times(this byte value, Action action) { - Guard.Against.Negative(value, nameof(value)); - Guard.Against.Null(action, nameof(action)); + Guard.ThrowIfNegative(value); + Guard.ThrowIfNull(action); Times(value, action); } @@ -93,8 +90,8 @@ public static void Times(this byte value, Action action) /// public static void Times(this short value, Action action) { - Guard.Against.Negative(value, nameof(value)); - Guard.Against.Null(action, nameof(action)); + Guard.ThrowIfNegative(value); + Guard.ThrowIfNull(action); Times(value, action); } @@ -108,8 +105,8 @@ public static void Times(this short value, Action action) /// public static void Times(this short value, Action action) { - Guard.Against.Negative(value, nameof(value)); - Guard.Against.Null(action, nameof(action)); + Guard.ThrowIfNegative(value); + Guard.ThrowIfNull(action); Times(value, action); } @@ -123,8 +120,8 @@ public static void Times(this short value, Action action) /// public static void Times(this int value, Action action) { - Guard.Against.Negative(value, nameof(value)); - Guard.Against.Null(action, nameof(action)); + Guard.ThrowIfNegative(value); + Guard.ThrowIfNull(action); Times(value, action); } @@ -138,8 +135,8 @@ public static void Times(this int value, Action action) /// public static void Times(this int value, Action action) { - Guard.Against.Negative(value, nameof(value)); - Guard.Against.Null(action, nameof(action)); + Guard.ThrowIfNegative(value); + Guard.ThrowIfNull(action); Times(value, action); } @@ -154,8 +151,8 @@ public static void Times(this int value, Action action) /// public static void Times(this long value, Action action) { - Guard.Against.Negative(value, nameof(value)); - Guard.Against.Null(action, nameof(action)); + Guard.ThrowIfNegative(value); + Guard.ThrowIfNull(action); Times(value, action); } @@ -169,8 +166,8 @@ public static void Times(this long value, Action action) /// public static void Times(this long value, Action action) { - Guard.Against.Negative(value, nameof(value)); - Guard.Against.Null(action, nameof(action)); + Guard.ThrowIfNegative(value); + Guard.ThrowIfNull(action); Times(value, action); } diff --git a/src/Fluxera.Utilities/Extensions/Principal/GetClaimValue.cs b/src/Fluxera.Utilities/Extensions/Principal/GetClaimValue.cs index 0e44f7f..4aaeb2b 100644 --- a/src/Fluxera.Utilities/Extensions/Principal/GetClaimValue.cs +++ b/src/Fluxera.Utilities/Extensions/Principal/GetClaimValue.cs @@ -3,7 +3,6 @@ namespace Fluxera.Utilities.Extensions { using System.Security.Claims; - using Fluxera.Guards; using JetBrains.Annotations; /// @@ -20,8 +19,8 @@ public static partial class PrincipalExtensions /// The values of the claim or null. public static string GetClaimValue(this ClaimsPrincipal principal, string claim) { - Guard.Against.Null(principal, nameof(principal)); - Guard.Against.NullOrWhiteSpace(claim, nameof(claim)); + Guard.ThrowIfNull(principal); + Guard.ThrowIfNullOrWhiteSpace(claim); return principal.FindFirst(claim)?.Value; } diff --git a/src/Fluxera.Utilities/Extensions/Principal/GetClaimValues.cs b/src/Fluxera.Utilities/Extensions/Principal/GetClaimValues.cs index e0da3f2..8031213 100644 --- a/src/Fluxera.Utilities/Extensions/Principal/GetClaimValues.cs +++ b/src/Fluxera.Utilities/Extensions/Principal/GetClaimValues.cs @@ -5,7 +5,6 @@ namespace Fluxera.Utilities.Extensions using System.Collections.Generic; using System.Linq; using System.Security.Claims; - using Fluxera.Guards; /// /// Extension methods for the type. @@ -20,8 +19,8 @@ public static partial class PrincipalExtensions /// The values of the claim or null. public static IEnumerable GetClaimValues(this ClaimsPrincipal principal, string claim) { - Guard.Against.Null(principal, nameof(principal)); - Guard.Against.NullOrWhiteSpace(claim, nameof(claim)); + Guard.ThrowIfNull(principal); + Guard.ThrowIfNullOrWhiteSpace(claim); return principal.FindAll(x => x.Type == claim).Select(x => x.Value); } diff --git a/src/Fluxera.Utilities/Extensions/Principal/GetDisplayName.cs b/src/Fluxera.Utilities/Extensions/Principal/GetDisplayName.cs index 3082f61..3bbe62d 100644 --- a/src/Fluxera.Utilities/Extensions/Principal/GetDisplayName.cs +++ b/src/Fluxera.Utilities/Extensions/Principal/GetDisplayName.cs @@ -4,7 +4,6 @@ namespace Fluxera.Utilities.Extensions { using System.Diagnostics; using System.Security.Claims; - using Fluxera.Guards; /// /// Extension methods for the type. @@ -19,7 +18,7 @@ public static partial class PrincipalExtensions [DebuggerStepThrough] public static string GetDisplayName(this ClaimsPrincipal principal) { - principal = Guard.Against.Null(principal); + principal = Guard.ThrowIfNull(principal); string name = principal.Identity?.Name; if(!string.IsNullOrWhiteSpace(name)) diff --git a/src/Fluxera.Utilities/Extensions/Principal/GetSubjectId.cs b/src/Fluxera.Utilities/Extensions/Principal/GetSubjectId.cs index 72ce507..fe83ade 100644 --- a/src/Fluxera.Utilities/Extensions/Principal/GetSubjectId.cs +++ b/src/Fluxera.Utilities/Extensions/Principal/GetSubjectId.cs @@ -6,7 +6,6 @@ namespace Fluxera.Utilities.Extensions using System.Diagnostics; using System.Security.Claims; using System.Security.Principal; - using Fluxera.Guards; /// /// Extension methods for the type. @@ -21,7 +20,7 @@ public static partial class PrincipalExtensions [DebuggerStepThrough] public static string GetSubjectId(this IPrincipal principal) { - Guard.Against.Null(principal, nameof(principal)); + Guard.ThrowIfNull(principal); return principal.Identity.GetSubjectId(); } @@ -31,11 +30,11 @@ public static string GetSubjectId(this IPrincipal principal) /// /// The identity. /// The subject. - /// sub claim is missing + /// When the 'sub' claim is missing. [DebuggerStepThrough] public static string GetSubjectId(this IIdentity identity) { - Guard.Against.Null(identity, nameof(identity)); + Guard.ThrowIfNull(identity); ClaimsIdentity claimsIdentity = identity as ClaimsIdentity; Claim claim = claimsIdentity?.FindFirst("sub"); diff --git a/src/Fluxera.Utilities/Extensions/Principal/IsAuthenticated.cs b/src/Fluxera.Utilities/Extensions/Principal/IsAuthenticated.cs index 9f52b30..9ea22ec 100644 --- a/src/Fluxera.Utilities/Extensions/Principal/IsAuthenticated.cs +++ b/src/Fluxera.Utilities/Extensions/Principal/IsAuthenticated.cs @@ -4,7 +4,6 @@ namespace Fluxera.Utilities.Extensions { using System.Diagnostics; using System.Security.Principal; - using Fluxera.Guards; /// /// Extension methods for the type. @@ -19,7 +18,7 @@ public static partial class PrincipalExtensions [DebuggerStepThrough] public static bool IsAuthenticated(this IPrincipal principal) { - Guard.Against.Null(principal, nameof(principal)); + Guard.ThrowIfNull(principal); return principal.Identity != null && principal.Identity.IsAuthenticated; } diff --git a/src/Fluxera.Utilities/Extensions/Reflection/GetMemberType.cs b/src/Fluxera.Utilities/Extensions/Reflection/GetMemberType.cs index 073e2c5..1fe0295 100644 --- a/src/Fluxera.Utilities/Extensions/Reflection/GetMemberType.cs +++ b/src/Fluxera.Utilities/Extensions/Reflection/GetMemberType.cs @@ -4,7 +4,6 @@ namespace Fluxera.Utilities.Extensions { using System; using System.Reflection; - using Fluxera.Guards; /// /// Extension methods for the type. @@ -18,7 +17,7 @@ public static partial class ReflectionExtensions /// The underlying type of the member. public static Type GetMemberType(this MemberInfo memberInfo) { - Guard.Against.Null(memberInfo, nameof(memberInfo)); + Guard.ThrowIfNull(memberInfo); return memberInfo switch { diff --git a/src/Fluxera.Utilities/Extensions/Stream/CopyTo.cs b/src/Fluxera.Utilities/Extensions/Stream/CopyTo.cs index f7f601b..7f13882 100644 --- a/src/Fluxera.Utilities/Extensions/Stream/CopyTo.cs +++ b/src/Fluxera.Utilities/Extensions/Stream/CopyTo.cs @@ -4,7 +4,6 @@ namespace Fluxera.Utilities.Extensions { using System; using System.IO; - using Fluxera.Guards; /// /// Extension methods for the type. @@ -31,11 +30,11 @@ public static void CopyTo(this Stream stream, Stream targetStream) /// public static void CopyTo(this Stream stream, Stream targetStream, int bufferSize) { - Guard.Against.Null(stream, nameof(stream)); - Guard.Against.Null(targetStream, nameof(targetStream)); + Guard.ThrowIfNull(stream); + Guard.ThrowIfNull(targetStream); - Guard.Against.False(stream.CanRead, nameof(stream), "The stream does not support reading."); - Guard.Against.False(targetStream.CanWrite, nameof(targetStream), "The stream does not support writing."); + Guard.ThrowIfFalse(stream.CanRead, message: "The stream does not support reading."); + Guard.ThrowIfFalse(targetStream.CanWrite, message: "The stream does not support writing."); byte[] buffer = new byte[bufferSize]; int bytesRead; diff --git a/src/Fluxera.Utilities/Extensions/Stream/CopyToMemory.cs b/src/Fluxera.Utilities/Extensions/Stream/CopyToMemory.cs index 149a52c..2c870dd 100644 --- a/src/Fluxera.Utilities/Extensions/Stream/CopyToMemory.cs +++ b/src/Fluxera.Utilities/Extensions/Stream/CopyToMemory.cs @@ -4,7 +4,6 @@ namespace Fluxera.Utilities.Extensions { using System; using System.IO; - using Fluxera.Guards; /// /// Extension methods for the type. @@ -19,7 +18,7 @@ public static partial class StreamExtensions /// public static MemoryStream CopyToMemory(this Stream stream) { - Guard.Against.Null(stream, nameof(stream)); + Guard.ThrowIfNull(stream); MemoryStream memoryStream = new MemoryStream((int)stream.Length); CopyTo(stream, memoryStream); diff --git a/src/Fluxera.Utilities/Extensions/Stream/GetBytes.cs b/src/Fluxera.Utilities/Extensions/Stream/GetBytes.cs index 452fd44..83d23f8 100644 --- a/src/Fluxera.Utilities/Extensions/Stream/GetBytes.cs +++ b/src/Fluxera.Utilities/Extensions/Stream/GetBytes.cs @@ -4,7 +4,6 @@ namespace Fluxera.Utilities.Extensions { using System.IO; using System.Threading.Tasks; - using Fluxera.Guards; /// /// Extension methods for the type. @@ -15,11 +14,11 @@ public static partial class StreamExtensions /// Gets all bytes of a . /// /// - /// the bytes. + /// The data of the stream. public static byte[] GetBytes(this Stream stream) { - Guard.Against.Null(stream, nameof(stream)); - Guard.Against.False(stream.CanRead, nameof(stream), "The stream does not support reading."); + Guard.ThrowIfNull(stream); + Guard.ThrowIfFalse(stream.CanRead, message: "The stream does not support reading."); byte[] buffer = new byte[16 * 1024]; using(MemoryStream ms = new MemoryStream()) @@ -38,11 +37,11 @@ public static byte[] GetBytes(this Stream stream) /// Gets all bytes from the steam. /// /// - /// + /// The data of the stream. public static byte[] GetAllBytes(this Stream stream) { - Guard.Against.Null(stream, nameof(stream)); - Guard.Against.False(stream.CanRead, nameof(stream), "The stream does not support reading."); + Guard.ThrowIfNull(stream); + Guard.ThrowIfFalse(stream.CanRead, message: "The stream does not support reading."); using(MemoryStream memoryStream = new MemoryStream()) { @@ -55,11 +54,11 @@ public static byte[] GetAllBytes(this Stream stream) /// Gets all bytes from the steam. /// /// - /// + /// The data of the stream. public static async Task GetAllBytesAsync(this Stream stream) { - Guard.Against.Null(stream, nameof(stream)); - Guard.Against.False(stream.CanRead, nameof(stream), "The stream does not support reading."); + Guard.ThrowIfNull(stream); + Guard.ThrowIfFalse(stream.CanRead, message: "The stream does not support reading."); await using(MemoryStream memoryStream = new MemoryStream()) { diff --git a/src/Fluxera.Utilities/Extensions/Stream/GetReader.cs b/src/Fluxera.Utilities/Extensions/Stream/GetReader.cs index c35bd5c..3c5b1e6 100644 --- a/src/Fluxera.Utilities/Extensions/Stream/GetReader.cs +++ b/src/Fluxera.Utilities/Extensions/Stream/GetReader.cs @@ -5,7 +5,6 @@ namespace Fluxera.Utilities.Extensions using System; using System.IO; using System.Text; - using Fluxera.Guards; /// /// Extension methods for the type. @@ -32,10 +31,10 @@ public static StreamReader GetReader(this Stream stream) /// public static StreamReader GetReader(this Stream stream, Encoding encoding) { - Guard.Against.Null(stream, nameof(stream)); - Guard.Against.False(stream.CanRead, nameof(stream), "The stream does not support reading."); + Guard.ThrowIfNull(stream); + Guard.ThrowIfFalse(stream.CanRead, message: "The stream does not support reading."); - encoding = encoding ?? Encoding.UTF8; + encoding ??= Encoding.UTF8; return new StreamReader(stream, encoding); } } diff --git a/src/Fluxera.Utilities/Extensions/Stream/GetWriter.cs b/src/Fluxera.Utilities/Extensions/Stream/GetWriter.cs index 6c98492..afd4c17 100644 --- a/src/Fluxera.Utilities/Extensions/Stream/GetWriter.cs +++ b/src/Fluxera.Utilities/Extensions/Stream/GetWriter.cs @@ -5,7 +5,6 @@ namespace Fluxera.Utilities.Extensions using System; using System.IO; using System.Text; - using Fluxera.Guards; /// /// Extension methods for the type. @@ -32,10 +31,10 @@ public static StreamWriter GetWriter(this Stream stream) /// public static StreamWriter GetWriter(this Stream stream, Encoding encoding) { - Guard.Against.Null(stream, nameof(stream)); - Guard.Against.False(stream.CanWrite, nameof(stream), "The stream does not support writing."); + Guard.ThrowIfNull(stream); + Guard.ThrowIfFalse(stream.CanWrite, message: "The stream does not support writing."); - encoding = encoding ?? Encoding.UTF8; + encoding ??= Encoding.UTF8; return new StreamWriter(stream, encoding); } } diff --git a/src/Fluxera.Utilities/Extensions/Stream/Rewind.cs b/src/Fluxera.Utilities/Extensions/Stream/Rewind.cs index 6ea25c7..6c95455 100644 --- a/src/Fluxera.Utilities/Extensions/Stream/Rewind.cs +++ b/src/Fluxera.Utilities/Extensions/Stream/Rewind.cs @@ -4,7 +4,6 @@ namespace Fluxera.Utilities.Extensions { using System; using System.IO; - using Fluxera.Guards; using JetBrains.Annotations; /// @@ -20,8 +19,8 @@ public static partial class StreamExtensions /// public static void Rewind(this Stream stream) { - Guard.Against.Null(stream, nameof(stream)); - Guard.Against.False(stream.CanSeek, nameof(stream), "The stream does not support seeking."); + Guard.ThrowIfNull(stream); + Guard.ThrowIfFalse(stream.CanSeek, message: "The stream does not support seeking."); stream.Seek(0, SeekOrigin.Begin); } diff --git a/src/Fluxera.Utilities/Extensions/Stream/Write.cs b/src/Fluxera.Utilities/Extensions/Stream/Write.cs index 1895efa..da04193 100644 --- a/src/Fluxera.Utilities/Extensions/Stream/Write.cs +++ b/src/Fluxera.Utilities/Extensions/Stream/Write.cs @@ -3,7 +3,6 @@ namespace Fluxera.Utilities.Extensions { using System.IO; - using Fluxera.Guards; /// /// Extension methods for the type. @@ -17,7 +16,7 @@ public static partial class StreamExtensions /// The byte array / buffer. public static void Write(this Stream stream, byte[] bytes) { - Guard.Against.Null(stream, nameof(stream)); + Guard.ThrowIfNull(stream); stream.Write(bytes, 0, bytes.Length); } diff --git a/src/Fluxera.Utilities/Extensions/String/Camelize.cs b/src/Fluxera.Utilities/Extensions/String/Camelize.cs index 4ed7234..acd0046 100644 --- a/src/Fluxera.Utilities/Extensions/String/Camelize.cs +++ b/src/Fluxera.Utilities/Extensions/String/Camelize.cs @@ -2,8 +2,6 @@ namespace Fluxera.Utilities.Extensions { - using Fluxera.Guards; - /// /// Extension methods for the type. /// @@ -16,7 +14,7 @@ public static partial class StringExtensions /// The camel cased string. public static string Camelize(this string str) { - Guard.Against.NullOrWhiteSpace(str, nameof(str)); + Guard.ThrowIfNullOrWhiteSpace(str); return Inflector.Camelize(str); } diff --git a/src/Fluxera.Utilities/Extensions/String/Capitalize.cs b/src/Fluxera.Utilities/Extensions/String/Capitalize.cs index f1af398..7d3d103 100644 --- a/src/Fluxera.Utilities/Extensions/String/Capitalize.cs +++ b/src/Fluxera.Utilities/Extensions/String/Capitalize.cs @@ -2,8 +2,6 @@ namespace Fluxera.Utilities.Extensions { - using Fluxera.Guards; - /// /// Extension methods for the type. /// @@ -16,7 +14,7 @@ public static partial class StringExtensions /// The capitalized string. public static string Capitalize(this string str) { - Guard.Against.NullOrWhiteSpace(str, nameof(str)); + Guard.ThrowIfNullOrWhiteSpace(str); return Inflector.Capitalize(str); } diff --git a/src/Fluxera.Utilities/Extensions/String/ConcatWith.cs b/src/Fluxera.Utilities/Extensions/String/ConcatWith.cs index f8ff6fc..9b286ad 100644 --- a/src/Fluxera.Utilities/Extensions/String/ConcatWith.cs +++ b/src/Fluxera.Utilities/Extensions/String/ConcatWith.cs @@ -2,8 +2,6 @@ namespace Fluxera.Utilities.Extensions { - using Fluxera.Guards; - /// /// Extension methods for the type. /// @@ -17,8 +15,8 @@ public static partial class StringExtensions /// The concatenated string. public static string ConcatWith(this string str, params string[] values) { - Guard.Against.NullOrWhiteSpace(str, nameof(str)); - Guard.Against.NullOrEmpty(values, nameof(values)); + Guard.ThrowIfNullOrWhiteSpace(str); + Guard.ThrowIfNullOrEmpty(values); return string.Concat(str, string.Concat(values)); } @@ -32,8 +30,8 @@ public static string ConcatWith(this string str, params string[] values) /// The concatenated string public static string ConcatWith(this string str, char delimiter, params string[] values) { - Guard.Against.NullOrWhiteSpace(str, nameof(str)); - Guard.Against.NullOrEmpty(values, nameof(values)); + Guard.ThrowIfNullOrWhiteSpace(str); + Guard.ThrowIfNullOrEmpty(values); string[] valuesToConcat = new string[values.Length]; for(int i = 0; i < values.Length; i++) diff --git a/src/Fluxera.Utilities/Extensions/String/Dasherize.cs b/src/Fluxera.Utilities/Extensions/String/Dasherize.cs index 1e03f3c..2e26934 100644 --- a/src/Fluxera.Utilities/Extensions/String/Dasherize.cs +++ b/src/Fluxera.Utilities/Extensions/String/Dasherize.cs @@ -2,8 +2,6 @@ namespace Fluxera.Utilities.Extensions { - using Fluxera.Guards; - /// /// Extension methods for the type. /// @@ -16,7 +14,7 @@ public static partial class StringExtensions /// The dasherized string. public static string Dasherize(this string str) { - Guard.Against.NullOrWhiteSpace(str, nameof(str)); + Guard.ThrowIfNullOrWhiteSpace(str); return Inflector.Dasherize(str); } diff --git a/src/Fluxera.Utilities/Extensions/String/EnsureEndsWith.cs b/src/Fluxera.Utilities/Extensions/String/EnsureEndsWith.cs index 43c4933..0ae8eb4 100644 --- a/src/Fluxera.Utilities/Extensions/String/EnsureEndsWith.cs +++ b/src/Fluxera.Utilities/Extensions/String/EnsureEndsWith.cs @@ -2,8 +2,6 @@ namespace Fluxera.Utilities.Extensions { - using Fluxera.Guards; - /// /// Extension methods for the type. /// @@ -17,7 +15,7 @@ public static partial class StringExtensions /// The string including the suffix. public static string EnsureEndsWith(this string str, string suffix) { - Guard.Against.NullOrWhiteSpace(str, nameof(str)); + Guard.ThrowIfNullOrWhiteSpace(str); return str.EndsWith(suffix) ? str : string.Concat(str, suffix); } @@ -30,7 +28,7 @@ public static string EnsureEndsWith(this string str, string suffix) /// The string including the suffix. public static string EnsureEndsWith(this string str, char suffix) { - Guard.Against.NullOrWhiteSpace(str, nameof(str)); + Guard.ThrowIfNullOrWhiteSpace(str); return str.EndsWith(suffix.ToString()) ? str : string.Concat(str, suffix); } diff --git a/src/Fluxera.Utilities/Extensions/String/EnsureStartsWith.cs b/src/Fluxera.Utilities/Extensions/String/EnsureStartsWith.cs index 04bff6e..d501a90 100644 --- a/src/Fluxera.Utilities/Extensions/String/EnsureStartsWith.cs +++ b/src/Fluxera.Utilities/Extensions/String/EnsureStartsWith.cs @@ -2,8 +2,6 @@ namespace Fluxera.Utilities.Extensions { - using Fluxera.Guards; - /// /// Extension methods for the type. /// @@ -17,7 +15,7 @@ public static partial class StringExtensions /// The string including the prefix. public static string EnsureStartsWith(this string str, string prefix) { - Guard.Against.NullOrWhiteSpace(str, nameof(str)); + Guard.ThrowIfNullOrWhiteSpace(str); return str.StartsWith(prefix) ? str : string.Concat(prefix, str); } @@ -30,7 +28,7 @@ public static string EnsureStartsWith(this string str, string prefix) /// The string including the prefix. public static string EnsureStartsWith(this string str, char prefix) { - Guard.Against.NullOrWhiteSpace(str, nameof(str)); + Guard.ThrowIfNullOrWhiteSpace(str); return str.StartsWith(prefix.ToString()) ? str : string.Concat(prefix, str); } diff --git a/src/Fluxera.Utilities/Extensions/String/FormatInvariantWith.cs b/src/Fluxera.Utilities/Extensions/String/FormatInvariantWith.cs index 8033eda..7ad31d1 100644 --- a/src/Fluxera.Utilities/Extensions/String/FormatInvariantWith.cs +++ b/src/Fluxera.Utilities/Extensions/String/FormatInvariantWith.cs @@ -3,7 +3,6 @@ namespace Fluxera.Utilities.Extensions { using System.Globalization; - using Fluxera.Guards; /// /// Extension methods for the type. @@ -18,7 +17,7 @@ public static partial class StringExtensions /// The formatted string. public static string FormatInvariantWith(this string str, params object[] args) { - Guard.Against.NullOrWhiteSpace(str, nameof(str)); + Guard.ThrowIfNullOrWhiteSpace(str); return str.FormatWith(CultureInfo.InvariantCulture, args); } diff --git a/src/Fluxera.Utilities/Extensions/String/FormatWith.cs b/src/Fluxera.Utilities/Extensions/String/FormatWith.cs index 19f887e..739302a 100644 --- a/src/Fluxera.Utilities/Extensions/String/FormatWith.cs +++ b/src/Fluxera.Utilities/Extensions/String/FormatWith.cs @@ -4,7 +4,6 @@ namespace Fluxera.Utilities.Extensions { using System; using System.Globalization; - using Fluxera.Guards; /// /// Extension methods for the type. @@ -19,7 +18,7 @@ public static partial class StringExtensions /// The formatted string. public static string FormatWith(this string str, params object[] args) { - Guard.Against.NullOrWhiteSpace(str, nameof(str)); + Guard.ThrowIfNullOrWhiteSpace(str); return str.FormatWith(CultureInfo.CurrentCulture, args); } @@ -33,7 +32,7 @@ public static string FormatWith(this string str, params object[] args) /// The formatted string. public static string FormatWith(this string str, IFormatProvider formatProvider, params object[] args) { - Guard.Against.NullOrWhiteSpace(str, nameof(str)); + Guard.ThrowIfNullOrWhiteSpace(str); formatProvider ??= CultureInfo.CurrentCulture; return string.Format(formatProvider, str, args); diff --git a/src/Fluxera.Utilities/Extensions/String/GetBytes.cs b/src/Fluxera.Utilities/Extensions/String/GetBytes.cs index cb22718..3e51ecb 100644 --- a/src/Fluxera.Utilities/Extensions/String/GetBytes.cs +++ b/src/Fluxera.Utilities/Extensions/String/GetBytes.cs @@ -3,7 +3,6 @@ namespace Fluxera.Utilities.Extensions { using System.Text; - using Fluxera.Guards; /// /// Extension methods for the type. @@ -17,7 +16,7 @@ public static partial class StringExtensions /// The created byte array. public static byte[] GetBytes(this string str) { - Guard.Against.NullOrWhiteSpace(str, nameof(str)); + Guard.ThrowIfNullOrWhiteSpace(str); return str.GetBytes(Encoding.UTF8); } @@ -30,7 +29,7 @@ public static byte[] GetBytes(this string str) /// The created byte array. public static byte[] GetBytes(this string str, Encoding encoding) { - Guard.Against.NullOrWhiteSpace(str, nameof(str)); + Guard.ThrowIfNullOrWhiteSpace(str); encoding ??= Encoding.UTF8; return encoding.GetBytes(str); diff --git a/src/Fluxera.Utilities/Extensions/String/Humanize.cs b/src/Fluxera.Utilities/Extensions/String/Humanize.cs index d59d08f..17904fb 100644 --- a/src/Fluxera.Utilities/Extensions/String/Humanize.cs +++ b/src/Fluxera.Utilities/Extensions/String/Humanize.cs @@ -2,8 +2,6 @@ namespace Fluxera.Utilities.Extensions { - using Fluxera.Guards; - /// /// Extension methods for the type. /// @@ -16,7 +14,7 @@ public static partial class StringExtensions /// public static string Humanize(this string str) { - Guard.Against.NullOrWhiteSpace(str, nameof(str)); + Guard.ThrowIfNullOrWhiteSpace(str); return Inflector.Humanize(str); } diff --git a/src/Fluxera.Utilities/Extensions/String/IsNumeric.cs b/src/Fluxera.Utilities/Extensions/String/IsNumeric.cs index d030adf..929680e 100644 --- a/src/Fluxera.Utilities/Extensions/String/IsNumeric.cs +++ b/src/Fluxera.Utilities/Extensions/String/IsNumeric.cs @@ -2,8 +2,6 @@ namespace Fluxera.Utilities.Extensions { - using Fluxera.Guards; - /// /// Extension methods for the type. /// @@ -16,7 +14,7 @@ public static partial class StringExtensions /// True, if the string contents is a numeric value. public static bool IsNumeric(this string str) { - Guard.Against.NullOrWhiteSpace(str, nameof(str)); + Guard.ThrowIfNullOrWhiteSpace(str); return double.TryParse(str, out double _); } diff --git a/src/Fluxera.Utilities/Extensions/String/Left.cs b/src/Fluxera.Utilities/Extensions/String/Left.cs index 2595a46..524ff69 100644 --- a/src/Fluxera.Utilities/Extensions/String/Left.cs +++ b/src/Fluxera.Utilities/Extensions/String/Left.cs @@ -3,7 +3,6 @@ namespace Fluxera.Utilities.Extensions { using System; - using Fluxera.Guards; /// /// Extension methods for the type. @@ -15,7 +14,7 @@ public static partial class StringExtensions /// public static string Left(this string str, int length) { - Guard.Against.NullOrWhiteSpace(str, nameof(str)); + Guard.ThrowIfNullOrWhiteSpace(str); if(str.Length < length) { diff --git a/src/Fluxera.Utilities/Extensions/String/Ordinalize.cs b/src/Fluxera.Utilities/Extensions/String/Ordinalize.cs index 4d045bf..bfd8eb9 100644 --- a/src/Fluxera.Utilities/Extensions/String/Ordinalize.cs +++ b/src/Fluxera.Utilities/Extensions/String/Ordinalize.cs @@ -2,8 +2,6 @@ namespace Fluxera.Utilities.Extensions { - using Fluxera.Guards; - /// /// Extension methods for the type. /// @@ -16,8 +14,8 @@ public static partial class StringExtensions /// The ordinalized string. public static string Ordinalize(this string str) { - Guard.Against.NullOrWhiteSpace(str, nameof(str)); - Guard.Against.False(str.IsNumeric(), nameof(str)); + Guard.ThrowIfNullOrWhiteSpace(str); + Guard.ThrowIfFalse(str.IsNumeric(), message: "The string value must represent a numeric value."); return Inflector.Ordinalize(str); } diff --git a/src/Fluxera.Utilities/Extensions/String/Pascalize.cs b/src/Fluxera.Utilities/Extensions/String/Pascalize.cs index fbf0100..880694b 100644 --- a/src/Fluxera.Utilities/Extensions/String/Pascalize.cs +++ b/src/Fluxera.Utilities/Extensions/String/Pascalize.cs @@ -2,8 +2,6 @@ namespace Fluxera.Utilities.Extensions { - using Fluxera.Guards; - /// /// Extension methods for the type. /// @@ -16,7 +14,7 @@ public static partial class StringExtensions /// The pascal cased string. public static string Pascalize(this string str) { - Guard.Against.NullOrWhiteSpace(str, nameof(str)); + Guard.ThrowIfNullOrWhiteSpace(str); return Inflector.Pascalize(str); } diff --git a/src/Fluxera.Utilities/Extensions/String/Pluralize.cs b/src/Fluxera.Utilities/Extensions/String/Pluralize.cs index 447eba2..aabf1ac 100644 --- a/src/Fluxera.Utilities/Extensions/String/Pluralize.cs +++ b/src/Fluxera.Utilities/Extensions/String/Pluralize.cs @@ -2,8 +2,6 @@ namespace Fluxera.Utilities.Extensions { - using Fluxera.Guards; - /// /// Extension methods for the type. /// @@ -16,7 +14,7 @@ public static partial class StringExtensions /// A string that is the plural form of the input parameter. public static string Pluralize(this string str) { - Guard.Against.NullOrWhiteSpace(str, nameof(str)); + Guard.ThrowIfNullOrWhiteSpace(str); return Inflector.Pluralize(str); } diff --git a/src/Fluxera.Utilities/Extensions/String/RemoveSpaces.cs b/src/Fluxera.Utilities/Extensions/String/RemoveSpaces.cs index 1372734..29b4611 100644 --- a/src/Fluxera.Utilities/Extensions/String/RemoveSpaces.cs +++ b/src/Fluxera.Utilities/Extensions/String/RemoveSpaces.cs @@ -2,8 +2,6 @@ namespace Fluxera.Utilities.Extensions { - using Fluxera.Guards; - /// /// Extension methods for the type. /// @@ -15,7 +13,7 @@ public static partial class StringExtensions /// The string to remove the spaces from. public static string RemoveSpaces(this string str) { - Guard.Against.NullOrWhiteSpace(str, nameof(str)); + Guard.ThrowIfNullOrWhiteSpace(str); return str.Replace(" ", string.Empty); } diff --git a/src/Fluxera.Utilities/Extensions/String/RemoveSpecialCharacters.cs b/src/Fluxera.Utilities/Extensions/String/RemoveSpecialCharacters.cs index 969ffb5..a6bfc4a 100644 --- a/src/Fluxera.Utilities/Extensions/String/RemoveSpecialCharacters.cs +++ b/src/Fluxera.Utilities/Extensions/String/RemoveSpecialCharacters.cs @@ -2,9 +2,7 @@ namespace Fluxera.Utilities.Extensions { - using System; using System.Linq; - using Fluxera.Guards; /// /// Extension methods for the type. @@ -20,20 +18,13 @@ public static partial class StringExtensions /// The string without special characters. public static string RemoveSpecialCharacters(this string str, params char[] ignore) { - Guard.Against.NullOrEmpty(str, nameof(str)); + Guard.ThrowIfNullOrEmpty(str); string result = string.Empty; - ignore ??= Array.Empty(); + ignore ??= []; - foreach(char character in str) - { - if(char.IsLetterOrDigit(character) || ignore.Contains(character)) - { - result += character; - } - } - - return result; + return str.Where(character => char.IsLetterOrDigit(character) || ignore.Contains(character)) + .Aggregate(result, (current, character) => current + character); } } } diff --git a/src/Fluxera.Utilities/Extensions/String/Repeat.cs b/src/Fluxera.Utilities/Extensions/String/Repeat.cs index 072abba..71840f3 100644 --- a/src/Fluxera.Utilities/Extensions/String/Repeat.cs +++ b/src/Fluxera.Utilities/Extensions/String/Repeat.cs @@ -4,7 +4,6 @@ namespace Fluxera.Utilities.Extensions { using System; using System.Text; - using Fluxera.Guards; /// /// Extension methods for the type. @@ -20,7 +19,7 @@ public static partial class StringExtensions /// public static string Repeat(this string str, int repeatCount) { - Guard.Against.NullOrWhiteSpace(str, nameof(str)); + Guard.ThrowIfNullOrWhiteSpace(str); StringBuilder stringBuilder = new StringBuilder(); repeatCount.Times(() => stringBuilder.Append(str)); diff --git a/src/Fluxera.Utilities/Extensions/String/ReplaceDiacritics.cs b/src/Fluxera.Utilities/Extensions/String/ReplaceDiacritics.cs index 982788d..859aeaa 100644 --- a/src/Fluxera.Utilities/Extensions/String/ReplaceDiacritics.cs +++ b/src/Fluxera.Utilities/Extensions/String/ReplaceDiacritics.cs @@ -4,7 +4,6 @@ namespace Fluxera.Utilities.Extensions { using System.Globalization; using System.Text; - using Fluxera.Guards; /// /// Extension methods for the type. @@ -18,7 +17,7 @@ public static partial class StringExtensions /// The string without diacritical marks. public static string ReplaceDiacritics(this string str) { - Guard.Against.NullOrEmpty(str, nameof(str)); + Guard.ThrowIfNullOrEmpty(str); string normalizedString = str.Normalize(NormalizationForm.FormD); StringBuilder stringBuilder = new StringBuilder(); diff --git a/src/Fluxera.Utilities/Extensions/String/Right.cs b/src/Fluxera.Utilities/Extensions/String/Right.cs index 2fdd37c..738e705 100644 --- a/src/Fluxera.Utilities/Extensions/String/Right.cs +++ b/src/Fluxera.Utilities/Extensions/String/Right.cs @@ -3,7 +3,6 @@ namespace Fluxera.Utilities.Extensions { using System; - using Fluxera.Guards; /// /// Extension methods for the type. @@ -15,7 +14,7 @@ public static partial class StringExtensions /// public static string Right(this string str, int length) { - Guard.Against.NullOrWhiteSpace(str, nameof(str)); + Guard.ThrowIfNullOrWhiteSpace(str); if(str.Length < length) { diff --git a/src/Fluxera.Utilities/Extensions/String/Singularize.cs b/src/Fluxera.Utilities/Extensions/String/Singularize.cs index ddd9634..6dca4ee 100644 --- a/src/Fluxera.Utilities/Extensions/String/Singularize.cs +++ b/src/Fluxera.Utilities/Extensions/String/Singularize.cs @@ -2,8 +2,6 @@ namespace Fluxera.Utilities.Extensions { - using Fluxera.Guards; - /// /// Extension methods for the type. /// @@ -16,7 +14,7 @@ public static partial class StringExtensions /// A string that is the singular form of the input parameter. public static string Singularize(this string str) { - Guard.Against.NullOrWhiteSpace(str, nameof(str)); + Guard.ThrowIfNullOrWhiteSpace(str); return Inflector.Singularize(str); } diff --git a/src/Fluxera.Utilities/Extensions/String/Titleize.cs b/src/Fluxera.Utilities/Extensions/String/Titleize.cs index 5edf1c8..06ed9a7 100644 --- a/src/Fluxera.Utilities/Extensions/String/Titleize.cs +++ b/src/Fluxera.Utilities/Extensions/String/Titleize.cs @@ -2,8 +2,6 @@ namespace Fluxera.Utilities.Extensions { - using Fluxera.Guards; - /// /// Extension methods for the type. /// @@ -17,7 +15,7 @@ public static partial class StringExtensions /// A string that is in title form. public static string Titleize(this string str) { - Guard.Against.NullOrWhiteSpace(str, nameof(str)); + Guard.ThrowIfNullOrWhiteSpace(str); return Inflector.Titleize(str); } diff --git a/src/Fluxera.Utilities/Extensions/String/ToEnum.cs b/src/Fluxera.Utilities/Extensions/String/ToEnum.cs index 7148670..243d0f2 100644 --- a/src/Fluxera.Utilities/Extensions/String/ToEnum.cs +++ b/src/Fluxera.Utilities/Extensions/String/ToEnum.cs @@ -3,7 +3,6 @@ namespace Fluxera.Utilities.Extensions { using System; - using Fluxera.Guards; /// /// Extension methods for the type. @@ -33,7 +32,7 @@ public static TEnum ToEnum(this string value) where TEnum : struct, Enum /// public static TEnum ToEnum(this string value, bool ignoreCase) where TEnum : struct, Enum { - Guard.Against.Null(value, nameof(value)); + Guard.ThrowIfNull(value); return (TEnum)Enum.Parse(typeof(TEnum), value, ignoreCase); } diff --git a/src/Fluxera.Utilities/Extensions/String/Truncate.cs b/src/Fluxera.Utilities/Extensions/String/Truncate.cs index 3f79c85..c6eb888 100644 --- a/src/Fluxera.Utilities/Extensions/String/Truncate.cs +++ b/src/Fluxera.Utilities/Extensions/String/Truncate.cs @@ -2,8 +2,6 @@ namespace Fluxera.Utilities.Extensions { - using Fluxera.Guards; - /// /// Extension methods for the type. /// @@ -15,7 +13,7 @@ public static partial class StringExtensions /// The string that will be truncated. /// The total length of characters to maintain before the truncate happens. /// Add '...' suffix to the end of the result if this is true. Default is false. - /// truncated string + /// The truncated string public static string Truncate(this string str, int maxLength, bool addSuffix = true) { if(string.IsNullOrEmpty(str)) @@ -23,7 +21,7 @@ public static string Truncate(this string str, int maxLength, bool addSuffix = t return string.Empty; } - Guard.Against.Negative(maxLength, nameof(maxLength)); + Guard.ThrowIfNegative(maxLength); const string suffix = "..."; int strLength = maxLength - suffix.Length; diff --git a/src/Fluxera.Utilities/Extensions/String/Uncapitalize.cs b/src/Fluxera.Utilities/Extensions/String/Uncapitalize.cs index b9c12a7..b29639e 100644 --- a/src/Fluxera.Utilities/Extensions/String/Uncapitalize.cs +++ b/src/Fluxera.Utilities/Extensions/String/Uncapitalize.cs @@ -2,8 +2,6 @@ namespace Fluxera.Utilities.Extensions { - using Fluxera.Guards; - /// /// Extension methods for the type. /// @@ -16,7 +14,7 @@ public static partial class StringExtensions /// The uncapitalized string. public static string Uncapitalize(this string str) { - Guard.Against.NullOrWhiteSpace(str, nameof(str)); + Guard.ThrowIfNullOrWhiteSpace(str); return Inflector.Uncapitalize(str); } diff --git a/src/Fluxera.Utilities/Extensions/String/Underscore.cs b/src/Fluxera.Utilities/Extensions/String/Underscore.cs index eb8e840..52d1e6d 100644 --- a/src/Fluxera.Utilities/Extensions/String/Underscore.cs +++ b/src/Fluxera.Utilities/Extensions/String/Underscore.cs @@ -2,8 +2,6 @@ namespace Fluxera.Utilities.Extensions { - using Fluxera.Guards; - /// /// Extension methods for the type. /// @@ -16,7 +14,7 @@ public static partial class StringExtensions /// The string containing underscores. public static string Underscore(this string str) { - Guard.Against.NullOrWhiteSpace(str, nameof(str)); + Guard.ThrowIfNullOrWhiteSpace(str); return Inflector.Underscore(str); } diff --git a/src/Fluxera.Utilities/Extensions/Type/CreateInstance.cs b/src/Fluxera.Utilities/Extensions/Type/CreateInstance.cs index 16aa24d..fcb0a3a 100644 --- a/src/Fluxera.Utilities/Extensions/Type/CreateInstance.cs +++ b/src/Fluxera.Utilities/Extensions/Type/CreateInstance.cs @@ -3,7 +3,6 @@ namespace Fluxera.Utilities.Extensions { using System; - using Fluxera.Guards; using JetBrains.Annotations; /// @@ -20,7 +19,7 @@ public static partial class TypeExtensions /// The instantiated object. public static object CreateInstance(this Type type, params object[] constructorParameters) { - Guard.Against.Null(type, nameof(type)); + Guard.ThrowIfNull(type); return type.CreateInstance(constructorParameters); } diff --git a/src/Fluxera.Utilities/Extensions/Type/IsPrimitive.cs b/src/Fluxera.Utilities/Extensions/Type/IsPrimitive.cs index 2fe22b7..d221422 100644 --- a/src/Fluxera.Utilities/Extensions/Type/IsPrimitive.cs +++ b/src/Fluxera.Utilities/Extensions/Type/IsPrimitive.cs @@ -7,8 +7,8 @@ namespace Fluxera.Utilities.Extensions public static partial class TypeExtensions { - private static readonly HashSet extraPrimitiveTypes = new HashSet - { + private static readonly HashSet ExtraPrimitiveTypes = + [ typeof(string), typeof(decimal), typeof(DateOnly), @@ -17,14 +17,14 @@ public static partial class TypeExtensions typeof(DateTimeOffset), typeof(TimeSpan), typeof(Guid) - }; + ]; /// /// Determines whether the specified type is a primitive. It automatically - /// unwraps the type of a nullable. + /// unwraps the wrapped type of the nullable. /// /// The type. - /// if set to true include enums. + /// If set to true include enums. /// /// true if the specified type is a primitive; otherwise, false. /// @@ -42,7 +42,7 @@ public static bool IsPrimitive(this Type type, bool includeEnums = false) return true; } - return extraPrimitiveTypes.Contains(type); + return ExtraPrimitiveTypes.Contains(type); } } } diff --git a/src/Fluxera.Utilities/Fluxera.Utilities.csproj b/src/Fluxera.Utilities/Fluxera.Utilities.csproj index 3d9f2a2..ac36285 100644 --- a/src/Fluxera.Utilities/Fluxera.Utilities.csproj +++ b/src/Fluxera.Utilities/Fluxera.Utilities.csproj @@ -22,7 +22,6 @@ - all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/Fluxera.Utilities/Guard.cs b/src/Fluxera.Utilities/Guard.cs new file mode 100644 index 0000000..6344206 --- /dev/null +++ b/src/Fluxera.Utilities/Guard.cs @@ -0,0 +1,122 @@ +namespace Fluxera.Utilities +{ + using JetBrains.Annotations; + using System; + using System.Collections.Generic; + using System.Linq; + using System.Numerics; + using System.Runtime.CompilerServices; + + internal static class Guard + { + public static T ThrowIfNull(T argument, [InvokerParameterName] [CallerArgumentExpression(nameof(argument))] string parameterName = null) + { + ArgumentNullException.ThrowIfNull(argument, parameterName); + + return argument; + } + + public static string ThrowIfNullOrEmpty(string argument, [InvokerParameterName][CallerArgumentExpression(nameof(argument))] string parameterName = null) + { + argument = Guard.ThrowIfNull(argument, parameterName); + + if(string.IsNullOrEmpty(argument)) + { + throw new ArgumentException("Value cannot be empty.", parameterName); + } + + return argument; + } + + public static string ThrowIfNullOrWhiteSpace(string argument, [InvokerParameterName][CallerArgumentExpression("argument")] string parameterName = null) + { + argument = Guard.ThrowIfNull(argument, parameterName); + + if(string.IsNullOrWhiteSpace(argument)) + { + throw new ArgumentException("Value cannot be whitespace-only.", parameterName); + } + + return argument; + } + + public static bool ThrowIfFalse(bool argument, [InvokerParameterName][CallerArgumentExpression(nameof(argument))] string parameterName = null, string message = null) + { + if(!argument) + { + throw new ArgumentException(message ?? "Value cannot be false.", parameterName); + } + + return true; + } + + public static IEnumerable ThrowIfNullOrEmpty(IEnumerable argument, [InvokerParameterName][CallerArgumentExpression(nameof(argument))] string parameterName = null) + { + argument = Guard.ThrowIfNull(argument, parameterName); + + // ReSharper disable PossibleMultipleEnumeration + if(!argument.Any()) + { + throw new ArgumentException("Enumerable cannot be empty.", parameterName); + } + + return argument; + // ReSharper enable PossibleMultipleEnumeration + } + +#if NET7_0_OR_GREATER + public static T ThrowIfNegative(T argument, [InvokerParameterName] [CallerArgumentExpression(nameof(argument))] string parameterName = null) + where T : INumber + { + if(T.IsNegative(argument)) + { + throw new ArgumentException("Value cannot be negative.", parameterName); + } + + return argument; + } +#endif + +#if NET6_0 + public static byte ThrowIfNegative(byte argument, [InvokerParameterName][CallerArgumentExpression(nameof(argument))] string parameterName = null) + { + if(argument < 0) + { + throw new ArgumentException("Value cannot be negative.", parameterName); + } + + return argument; + } + + public static short ThrowIfNegative(short argument, [InvokerParameterName][CallerArgumentExpression(nameof(argument))] string parameterName = null) + { + if(argument < 0) + { + throw new ArgumentException("Value cannot be negative.", parameterName); + } + + return argument; + } + + public static int ThrowIfNegative(int argument, [InvokerParameterName] [CallerArgumentExpression(nameof(argument))] string parameterName = null) + { + if(argument < 0) + { + throw new ArgumentException("Value cannot be negative.", parameterName); + } + + return argument; + } + + public static long ThrowIfNegative(long argument, [InvokerParameterName][CallerArgumentExpression(nameof(argument))] string parameterName = null) + { + if(argument < 0) + { + throw new ArgumentException("Value cannot be negative.", parameterName); + } + + return argument; + } +#endif + } +} diff --git a/src/Fluxera.Utilities/Helpers/AsyncHelper.cs b/src/Fluxera.Utilities/Helpers/AsyncHelper.cs index 832b388..1a0907e 100644 --- a/src/Fluxera.Utilities/Helpers/AsyncHelper.cs +++ b/src/Fluxera.Utilities/Helpers/AsyncHelper.cs @@ -8,7 +8,6 @@ namespace Fluxera.Utilities using System.Security.Principal; using System.Threading; using System.Threading.Tasks; - using Fluxera.Guards; using JetBrains.Annotations; /// @@ -21,7 +20,7 @@ namespace Fluxera.Utilities [PublicAPI] public static class AsyncHelper { - private static readonly TaskFactory taskFactory = new TaskFactory(CancellationToken.None, + private static readonly TaskFactory TaskFactory = new TaskFactory(CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, TaskScheduler.Default); /// @@ -37,7 +36,7 @@ public static TResult RunSync(Func> func) CultureInfo cultureUi = CultureInfo.CurrentUICulture; CultureInfo culture = CultureInfo.CurrentCulture; - return taskFactory.StartNew(() => + return TaskFactory.StartNew(() => { Thread.CurrentPrincipal = principal; Thread.CurrentThread.CurrentCulture = culture; @@ -61,7 +60,7 @@ public static void RunSync(Func func) CultureInfo cultureUi = CultureInfo.CurrentUICulture; CultureInfo culture = CultureInfo.CurrentCulture; - taskFactory.StartNew(() => + TaskFactory.StartNew(() => { Thread.CurrentPrincipal = principal; Thread.CurrentThread.CurrentCulture = culture; @@ -80,7 +79,7 @@ public static void RunSync(Func func) /// A method to check public static bool IsAsync(this MethodInfo method) { - Guard.Against.Null(method, nameof(method)); + Guard.ThrowIfNull(method); Type type = method.ReturnType; return type == typeof(Task) || diff --git a/src/Fluxera.Utilities/Helpers/RandomHelper.cs b/src/Fluxera.Utilities/Helpers/RandomHelper.cs index a526cbe..0343efd 100644 --- a/src/Fluxera.Utilities/Helpers/RandomHelper.cs +++ b/src/Fluxera.Utilities/Helpers/RandomHelper.cs @@ -5,16 +5,15 @@ namespace Fluxera.Utilities using System; using System.Collections.Generic; using System.Linq; - using Fluxera.Guards; using JetBrains.Annotations; /// - /// A shortcut to use class. + /// A shortcut to use class. /// [PublicAPI] public static class RandomHelper { - private static readonly Random random = new Random(); + private static readonly Random Random = new Random(); /// /// Returns a random number within a specified range. @@ -31,9 +30,9 @@ public static class RandomHelper /// public static int GetRandom(int minValue, int maxValue) { - lock(random) + lock(Random) { - return random.Next(minValue, maxValue); + return Random.Next(minValue, maxValue); } } @@ -51,9 +50,9 @@ public static int GetRandom(int minValue, int maxValue) /// public static int GetRandom(int maxValue) { - lock(random) + lock(Random) { - return random.Next(maxValue); + return Random.Next(maxValue); } } @@ -63,9 +62,9 @@ public static int GetRandom(int maxValue) /// A 32-bit signed integer greater than or equal to zero and less than . public static int GetRandom() { - lock(random) + lock(Random) { - return random.Next(); + return Random.Next(); } } @@ -76,7 +75,7 @@ public static int GetRandom() /// List of object to select a random one public static T GetRandomOf(params T[] items) { - items = Guard.Against.Null(items); + items = Guard.ThrowIfNull(items); return items[GetRandom(0, items.Length)]; } @@ -88,7 +87,7 @@ public static T GetRandomOf(params T[] items) /// List of object to select a random one public static T GetRandomOfList(IList items) { - items = Guard.Against.Null(items); + items = Guard.ThrowIfNull(items); return items[GetRandom(0, items.Count)]; } @@ -100,7 +99,7 @@ public static T GetRandomOfList(IList items) /// items public static IList GenerateRandomizedList(IEnumerable items) { - items = Guard.Against.Null(items); + items = Guard.ThrowIfNull(items); IList currentList = new List(items); IList randomList = new List();