-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Ensure extensions for range-related exceptions.
Added implementation of IEquatable<Range<T>> to Range struct. Exception check from the constructor of Range struct moved to Ensure extensions so its logic can be reused now. Added more comments to Range struct. Closes #5 and closes #6. Version 0.0.3.
- Loading branch information
Showing
4 changed files
with
110 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Runtime.CompilerServices; | ||
using Platform.Exceptions; | ||
|
||
#pragma warning disable IDE0060 // Remove unused parameter | ||
|
||
namespace Platform.Ranges | ||
{ | ||
public static class EnsureExtensions | ||
{ | ||
#region Always | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static void MaximumArgumentIsGreaterOrEqualToMinimum<T>(this EnsureAlwaysExtensionRoot root, T minimum, T maximum) => MaximumArgumentIsGreaterOrEqualToMinimum(root, minimum, maximum, nameof(maximum)); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static void MaximumArgumentIsGreaterOrEqualToMinimum<T>(this EnsureAlwaysExtensionRoot root, T minimum, T maximum, string argumentName) | ||
{ | ||
if (Comparer<T>.Default.Compare(maximum, minimum) < 0) | ||
{ | ||
throw new ArgumentException("Maximum should be greater or equal to minimum.", argumentName); | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static void ArgumentInRange<T>(this EnsureAlwaysExtensionRoot root, T argumentValue, T minimum, T maximum) => ArgumentInRange(root, argumentValue, new Range<T>(minimum, maximum), null); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static void ArgumentInRange<T>(this EnsureAlwaysExtensionRoot root, T argumentValue, T minimum, T maximum, string argumentName) => ArgumentInRange(root, argumentValue, new Range<T>(minimum, maximum), argumentName); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static void ArgumentInRange<T>(this EnsureAlwaysExtensionRoot root, T argumentValue, Range<T> range) => ArgumentInRange(root, argumentValue, range, null); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static void ArgumentInRange<T>(this EnsureAlwaysExtensionRoot root, T argumentValue, Range<T> range, string argumentName) | ||
{ | ||
if (!range.ContainsValue(argumentValue)) | ||
{ | ||
throw new ArgumentOutOfRangeException(argumentName, argumentValue, $"Argument value [{argumentValue}] is out of range {range}."); | ||
} | ||
} | ||
|
||
#endregion | ||
|
||
#region OnDebug | ||
|
||
[Conditional("DEBUG")] | ||
public static void MaximumArgumentIsGreaterOrEqualToMinimum<T>(this EnsureOnDebugExtensionRoot root, T minimum, T maximum) => Ensure.Always.MaximumArgumentIsGreaterOrEqualToMinimum(minimum, maximum, null); | ||
|
||
[Conditional("DEBUG")] | ||
public static void MaximumArgumentIsGreaterOrEqualToMinimum<T>(this EnsureOnDebugExtensionRoot root, T minimum, T maximum, string argumentName) => Ensure.Always.MaximumArgumentIsGreaterOrEqualToMinimum(minimum, maximum, argumentName); | ||
|
||
[Conditional("DEBUG")] | ||
public static void ArgumentInRange<T>(this EnsureOnDebugExtensionRoot root, T argumentValue, T minimum, T maximum) => Ensure.Always.ArgumentInRange(argumentValue, new Range<T>(minimum, maximum), null); | ||
|
||
[Conditional("DEBUG")] | ||
public static void ArgumentInRange<T>(this EnsureOnDebugExtensionRoot root, T argumentValue, T minimum, T maximum, string argumentName) => Ensure.Always.ArgumentInRange(argumentValue, new Range<T>(minimum, maximum), argumentName); | ||
|
||
[Conditional("DEBUG")] | ||
public static void ArgumentInRange<T>(this EnsureOnDebugExtensionRoot root, T argument, Range<T> range) => Ensure.Always.ArgumentInRange(argument, range, null); | ||
|
||
[Conditional("DEBUG")] | ||
public static void ArgumentInRange<T>(this EnsureOnDebugExtensionRoot root, T argument, Range<T> range, string argumentName) => Ensure.Always.ArgumentInRange(argument, range, argumentName); | ||
|
||
#endregion | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters