Skip to content

Commit

Permalink
Removed dependency. (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgernand authored May 26, 2024
1 parent 22ff42f commit 59543ff
Show file tree
Hide file tree
Showing 78 changed files with 417 additions and 274 deletions.
3 changes: 1 addition & 2 deletions src/Fluxera.ComponentModel.Annotations/ContainsAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
{
using System;
using System.ComponentModel.DataAnnotations;
using Fluxera.Guards;
using JetBrains.Annotations;

/// <summary>
Expand All @@ -18,7 +17,7 @@ public sealed class ContainsAttribute : ValidationAttribute
/// <param name="part"></param>
public ContainsAttribute(string part)
{
Guard.Against.NullOrEmpty(part, nameof(part));
Guard.ThrowIfNull(part);

this.Part = part;
}
Expand Down
3 changes: 1 addition & 2 deletions src/Fluxera.ComponentModel.Annotations/EndsWithAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
{
using System;
using System.ComponentModel.DataAnnotations;
using Fluxera.Guards;
using JetBrains.Annotations;

/// <summary>
Expand All @@ -19,7 +18,7 @@ public sealed class EndsWithAttribute : ValidationAttribute
/// <param name="stringComparison"></param>
public EndsWithAttribute(string end, StringComparison stringComparison = StringComparison.InvariantCultureIgnoreCase)
{
Guard.Against.NullOrEmpty(end, nameof(end));
Guard.ThrowIfNullOrEmpty(end);

this.End = end;
this.StringComparison = stringComparison;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Fluxera.Guards" Version="8.0.*" />
<PackageReference Include="GitVersion.MsBuild" Version="5.12.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
122 changes: 122 additions & 0 deletions src/Fluxera.ComponentModel.Annotations/Guard.cs
Original file line number Diff line number Diff line change
@@ -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>(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<T> ThrowIfNullOrEmpty<T>(IEnumerable<T> 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>(T argument, [InvokerParameterName] [CallerArgumentExpression(nameof(argument))] string parameterName = null)
where T : INumber<T>
{
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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Collections;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using Fluxera.Guards;
using JetBrains.Annotations;

/// <summary>
Expand All @@ -20,7 +19,7 @@ public sealed class ListLengthAttribute : ValidationAttribute
/// <param name="maximumLength"></param>
public ListLengthAttribute(int maximumLength)
{
Guard.Against.Negative(maximumLength, nameof(maximumLength));
Guard.ThrowIfNegative(maximumLength);

this.MaximumLength = maximumLength;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Collections;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using Fluxera.Guards;
using JetBrains.Annotations;

/// <summary>
Expand All @@ -20,7 +19,7 @@ public sealed class ListMaxLengthAttribute : ValidationAttribute
/// <param name="maximumLength"></param>
public ListMaxLengthAttribute(int maximumLength)
{
Guard.Against.Negative(maximumLength, nameof(maximumLength));
Guard.ThrowIfNegative(maximumLength);

this.MaximumLength = maximumLength;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Collections;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using Fluxera.Guards;
using JetBrains.Annotations;

/// <summary>
Expand All @@ -20,7 +19,7 @@ public sealed class ListMinLengthAttribute : ValidationAttribute
/// <param name="minimumLength"></param>
public ListMinLengthAttribute(int minimumLength)
{
Guard.Against.Negative(minimumLength, nameof(minimumLength));
Guard.ThrowIfNegative(minimumLength);

this.MinimumLength = minimumLength;
}
Expand Down
5 changes: 2 additions & 3 deletions src/Fluxera.ComponentModel.Annotations/ReferenceAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace Fluxera.ComponentModel.Annotations
{
using System;
using Fluxera.Guards;
using JetBrains.Annotations;

/// <summary>
Expand Down Expand Up @@ -30,7 +29,7 @@ public class ReferenceAttribute : Attribute
/// <param name="referencedEntityType"></param>
public ReferenceAttribute(Type referencedEntityType)
{
Guard.Against.Null(referencedEntityType);
Guard.ThrowIfNull(referencedEntityType);

this.ReferencedEntityName = referencedEntityType.Name;
}
Expand All @@ -41,7 +40,7 @@ public ReferenceAttribute(Type referencedEntityType)
/// <param name="referencedEntityName"></param>
public ReferenceAttribute(string referencedEntityName)
{
Guard.Against.NullOrWhiteSpace(referencedEntityName);
Guard.ThrowIfNullOrWhiteSpace(referencedEntityName);

this.ReferencedEntityName = referencedEntityName;
}
Expand Down
5 changes: 2 additions & 3 deletions src/Fluxera.ComponentModel.Annotations/RequiredIfAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
{
using System;
using System.ComponentModel.DataAnnotations;
using Fluxera.Guards;
using JetBrains.Annotations;

/// <summary>
Expand All @@ -25,8 +24,8 @@ public sealed class RequiredIfAttribute : RequiredAttribute
/// <param name="desiredValues">The desired values are OR-ed when evaluating.</param>
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
{
using System;
using System.ComponentModel.DataAnnotations;
using Fluxera.Guards;
using JetBrains.Annotations;

/// <summary>
Expand All @@ -19,7 +18,7 @@ public sealed class StartsWithAttribute : ValidationAttribute
/// <param name="stringComparison"></param>
public StartsWithAttribute(string start, StringComparison stringComparison = StringComparison.InvariantCultureIgnoreCase)
{
Guard.Against.NullOrEmpty(start, nameof(start));
Guard.ThrowIfNullOrEmpty(start);

this.Start = start;
this.StringComparison = stringComparison;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Fluxera.Guards" Version="8.0.*" />
<PackageReference Include="GitVersion.MsBuild" Version="5.12.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
4 changes: 2 additions & 2 deletions src/Fluxera.Utilities/Collections/SortedList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
Expand Down Expand Up @@ -41,7 +41,7 @@ public SortedList(Comparer<T> comparer)
/// <inheritdoc />
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)
Expand Down
6 changes: 3 additions & 3 deletions src/Fluxera.Utilities/Collections/TypeList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
Expand All @@ -20,7 +20,7 @@ public class TypeList<TBaseType> : ITypeList<TBaseType>
private readonly List<Type> innerList;

/// <summary>
/// Creates a new instance of the <see cref="TypeList{T}" /> type..
/// Creates a new instance of the <see cref="TypeList{T}" /> type.
/// </summary>
public TypeList()
{
Expand Down Expand Up @@ -138,7 +138,7 @@ public void Remove<T>() where T : TBaseType

private static void CheckType(Type item)
{
item = Guard.Against.Null(item);
item = Guard.ThrowIfNull(item);

if(!typeof(TBaseType).GetTypeInfo().IsAssignableFrom(item))
{
Expand Down
3 changes: 1 addition & 2 deletions src/Fluxera.Utilities/Extensions/Array/IsIndexWithin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Fluxera.Utilities.Extensions
{
using System;
using Fluxera.Guards;
using JetBrains.Annotations;

/// <summary>
Expand All @@ -20,7 +19,7 @@ public static partial class ArrayExtensions
/// <returns>True, if the index lies within the array.</returns>
public static bool IsIndexWithin(this Array source, int index)
{
Guard.Against.Null(source, nameof(source));
Guard.ThrowIfNull(source);

return index >= 0 && index < source.Length;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ namespace Fluxera.Utilities.Extensions
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using Fluxera.Guards;
using JetBrains.Annotations;

/// <summary>
Expand All @@ -22,7 +21,7 @@ public static class AssemblyExtensions
/// <returns>The content of the resource.</returns>
public static async Task<string> GetResourceAsStringAsync(this Assembly assembly, string resourceLocation)
{
Guard.Against.Null(assembly, nameof(assembly));
Guard.ThrowIfNull(assembly);

string result = string.Empty;

Expand Down
5 changes: 2 additions & 3 deletions src/Fluxera.Utilities/Extensions/Char/Repeat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Fluxera.Utilities.Extensions
{
using Fluxera.Guards;
using JetBrains.Annotations;

/// <summary>
Expand All @@ -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));
Expand All @@ -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));
Expand Down
Loading

0 comments on commit 59543ff

Please sign in to comment.