diff --git a/src/Louis/Fluency/FluentExtensions-Switch.cs b/src/Louis/Fluency/FluentExtensions-Switch.cs index 8732473..6e70fb0 100644 --- a/src/Louis/Fluency/FluentExtensions-Switch.cs +++ b/src/Louis/Fluency/FluentExtensions-Switch.cs @@ -2,6 +2,7 @@ // See the LICENSE file in the project root for full license information. using System; +using System.Collections.Generic; using CommunityToolkit.Diagnostics; namespace Louis.Fluency; @@ -31,8 +32,33 @@ partial class FluentExtensions /// If is not equal to any of the values in , this method returns immediately. /// public static T Switch(this T @this, TValue value, params (TValue Comparand, FluentAction? Action)[] cases) - where TValue : IEquatable - => Switch(@this, value, null, cases); + => Switch(@this, EqualityComparer.Default, value, null, cases); + + /// + /// Selects an action to invoke on an object by comparing a given value against a list of comparands, then returns the object. + /// + /// The type of the object. + /// The type of the value being compared. + /// The object on which this method was called. + /// The interface to use for comparisons. + /// The value to compare. + /// + /// An array of pairs, where each element consists of a value to compare against + /// and an optional action to perform on if they are equal. + /// If the action is , no action is performed if the value is equal to ; + /// instead, the method returns immediately. + /// + /// A reference to after one of the actions in , if any, returns. + /// + /// is . + /// - or - + /// is . + /// + /// + /// If is not equal to any of the values in , this method returns immediately. + /// + public static T Switch(this T @this, IEqualityComparer comparer, TValue value, params (TValue Comparand, FluentAction? Action)[] cases) + => Switch(@this, comparer, value, null, cases); /// /// Selects an action to invoke on an object by comparing a given value against a list of comparands, then returns the object. @@ -57,8 +83,33 @@ public static T Switch(this T @this, TValue value, params (TValue Com /// If is not equal to any of the values in , this method returns immediately. /// public static T Switch(this T @this, TValue value, params (TValue Comparand, Action? Action)[] cases) - where TValue : IEquatable - => Switch(@this, value, null, cases); + => Switch(@this, EqualityComparer.Default, value, null, cases); + + /// + /// Selects an action to invoke on an object by comparing a given value against a list of comparands, then returns the object. + /// + /// The type of the object. + /// The type of the value being compared. + /// The object on which this method was called. + /// The interface to use for comparisons. + /// The value to compare. + /// + /// An array of pairs, where each element consists of a value to compare against + /// and an optional action to perform on if they are equal. + /// If the action is , no action is performed if the value is equal to ; + /// instead, the method returns immediately. + /// + /// A reference to after one of the actions in , if any, returns. + /// + /// is . + /// - or - + /// is . + /// + /// + /// If is not equal to any of the values in , this method returns immediately. + /// + public static T Switch(this T @this, IEqualityComparer comparer, TValue value, params (TValue Comparand, Action? Action)[] cases) + => Switch(@this, comparer, value, null, cases); /// /// Selects an action to invoke on an object by comparing a given value against a list of comparands, then returns the object. @@ -86,14 +137,43 @@ public static T Switch(this T @this, TValue value, params (TValue Com /// is . /// public static T Switch(this T @this, TValue value, FluentAction? @default, params (TValue Comparand, FluentAction? Action)[] cases) - where TValue : IEquatable + => Switch(@this, EqualityComparer.Default, value, @default, cases); + + /// + /// Selects an action to invoke on an object by comparing a given value against a list of comparands, then returns the object. + /// + /// The type of the object. + /// The type of the value being compared. + /// The object on which this method was called. + /// The interface to use for comparisons. + /// The value to compare. + /// + /// An optional action to perform on if is not equal + /// to any of the values in . + /// If this parameter is and is not equal + /// to any of the values in , this method returns immediately. + /// + /// + /// An array of pairs, where each element consists of a value to compare against + /// and an optional action to perform on if they are equal. + /// If the action is , no action is performed if the value is equal to ; + /// instead, the method returns immediately. + /// + /// A reference to after one of the actions in , if any, returns. + /// + /// is . + /// - or - + /// is . + /// + public static T Switch(this T @this, IEqualityComparer comparer, TValue value, FluentAction? @default, params (TValue Comparand, FluentAction? Action)[] cases) { Guard.IsNotNull(@this); + Guard.IsNotNull(comparer); Guard.IsNotNull(cases); foreach (var (comparand, action) in cases) { - if (value.Equals(comparand)) + if (comparer.Equals(value, comparand)) { return action == null ? @this : action(@this); } @@ -128,14 +208,43 @@ public static T Switch(this T @this, TValue value, FluentAction? @ /// is . /// public static T Switch(this T @this, TValue value, FluentAction? @default, params (TValue Comparand, Action? Action)[] cases) - where TValue : IEquatable + => Switch(@this, EqualityComparer.Default, value, @default, cases); + + /// + /// Selects an action to invoke on an object by comparing a given value against a list of comparands, then returns the object. + /// + /// The type of the object. + /// The type of the value being compared. + /// The object on which this method was called. + /// The interface to use for comparisons. + /// The value to compare. + /// + /// An optional action to perform on if is not equal + /// to any of the values in . + /// If this parameter is and is not equal + /// to any of the values in , this method returns immediately. + /// + /// + /// An array of pairs, where each element consists of a value to compare against + /// and an optional action to perform on if they are equal. + /// If the action is , no action is performed if the value is equal to ; + /// instead, the method returns immediately. + /// + /// A reference to after one of the actions in , if any, returns. + /// + /// is . + /// - or - + /// is . + /// + public static T Switch(this T @this, IEqualityComparer comparer, TValue value, FluentAction? @default, params (TValue Comparand, Action? Action)[] cases) { Guard.IsNotNull(@this); + Guard.IsNotNull(comparer); Guard.IsNotNull(cases); foreach (var (comparand, action) in cases) { - if (value.Equals(comparand)) + if (comparer.Equals(value, comparand)) { action?.Invoke(@this); return @this; diff --git a/src/Louis/PublicAPI.Unshipped.txt b/src/Louis/PublicAPI.Unshipped.txt index 342dea9..6d1e8e0 100644 --- a/src/Louis/PublicAPI.Unshipped.txt +++ b/src/Louis/PublicAPI.Unshipped.txt @@ -124,6 +124,10 @@ static Louis.Fluency.FluentExtensions.InvokeIf(this T th static Louis.Fluency.FluentExtensions.InvokeIf(this T this, bool condition, TArg1 arg1, TArg2 arg2, System.Action! action) -> T static Louis.Fluency.FluentExtensions.InvokeIf(this T this, bool condition, TArg arg, System.Action! action) -> T static Louis.Fluency.FluentExtensions.InvokeIf(this T this, bool condition, System.Action! action) -> T +static Louis.Fluency.FluentExtensions.Switch(this T this, System.Collections.Generic.IEqualityComparer! comparer, TValue value, Louis.Fluency.FluentAction? default, params (TValue Comparand, Louis.Fluency.FluentAction? Action)[]! cases) -> T +static Louis.Fluency.FluentExtensions.Switch(this T this, System.Collections.Generic.IEqualityComparer! comparer, TValue value, Louis.Fluency.FluentAction? default, params (TValue Comparand, System.Action? Action)[]! cases) -> T +static Louis.Fluency.FluentExtensions.Switch(this T this, System.Collections.Generic.IEqualityComparer! comparer, TValue value, params (TValue Comparand, Louis.Fluency.FluentAction? Action)[]! cases) -> T +static Louis.Fluency.FluentExtensions.Switch(this T this, System.Collections.Generic.IEqualityComparer! comparer, TValue value, params (TValue Comparand, System.Action? Action)[]! cases) -> T static Louis.Fluency.FluentExtensions.Switch(this T this, TValue value, Louis.Fluency.FluentAction? default, params (TValue Comparand, Louis.Fluency.FluentAction? Action)[]! cases) -> T static Louis.Fluency.FluentExtensions.Switch(this T this, TValue value, Louis.Fluency.FluentAction? default, params (TValue Comparand, System.Action? Action)[]! cases) -> T static Louis.Fluency.FluentExtensions.Switch(this T this, TValue value, params (TValue Comparand, Louis.Fluency.FluentAction? Action)[]! cases) -> T