Skip to content

Commit

Permalink
Refactor FluentExtensions.Switch to use equality comparers
Browse files Browse the repository at this point in the history
  • Loading branch information
ric15ni authored and rdeago committed Aug 13, 2023
1 parent a8253ce commit 486cbb0
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 8 deletions.
125 changes: 117 additions & 8 deletions src/Louis/Fluency/FluentExtensions-Switch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -31,8 +32,33 @@ partial class FluentExtensions
/// <para>If <paramref name="value"/> is not equal to any of the values in <paramref name="cases"/>, this method returns immediately.</para>
/// </remarks>
public static T Switch<T, TValue>(this T @this, TValue value, params (TValue Comparand, FluentAction<T>? Action)[] cases)
where TValue : IEquatable<TValue>
=> Switch(@this, value, null, cases);
=> Switch(@this, EqualityComparer<TValue>.Default, value, null, cases);

/// <summary>
/// Selects an action to invoke on an object by comparing a given value against a list of comparands, then returns the object.
/// </summary>
/// <typeparam name="T">The type of the object.</typeparam>
/// <typeparam name="TValue">The type of the value being compared.</typeparam>
/// <param name="this">The object on which this method was called.</param>
/// <param name="comparer">The interface to use for comparisons.</param>
/// <param name="value">The value to compare.</param>
/// <param name="cases">
/// <para>An array of pairs, where each element consists of a value to compare against <paramref name="value"/>
/// and an optional action to perform on <paramref name="this"/> if they are equal.</para>
/// <para>If the action is <see langword="null"/>, no action is performed if the value is equal to <paramref name="value"/>;
/// instead, the method returns immediately.</para>
/// </param>
/// <returns>A reference to <paramref name="this"/> after one of the actions in <paramref name="cases"/>, if any, returns.</returns>
/// <exception cref="ArgumentNullException">
/// <para><paramref name="this"/> is <see langword="null"/>.</para>
/// <para>- or -</para>
/// <para><paramref name="cases"/> is <see langword="null"/>.</para>
/// </exception>
/// <remarks>
/// <para>If <paramref name="value"/> is not equal to any of the values in <paramref name="cases"/>, this method returns immediately.</para>
/// </remarks>
public static T Switch<T, TValue>(this T @this, IEqualityComparer<TValue> comparer, TValue value, params (TValue Comparand, FluentAction<T>? Action)[] cases)
=> Switch(@this, comparer, value, null, cases);

/// <summary>
/// Selects an action to invoke on an object by comparing a given value against a list of comparands, then returns the object.
Expand All @@ -57,8 +83,33 @@ public static T Switch<T, TValue>(this T @this, TValue value, params (TValue Com
/// <para>If <paramref name="value"/> is not equal to any of the values in <paramref name="cases"/>, this method returns immediately.</para>
/// </remarks>
public static T Switch<T, TValue>(this T @this, TValue value, params (TValue Comparand, Action<T>? Action)[] cases)
where TValue : IEquatable<TValue>
=> Switch(@this, value, null, cases);
=> Switch(@this, EqualityComparer<TValue>.Default, value, null, cases);

/// <summary>
/// Selects an action to invoke on an object by comparing a given value against a list of comparands, then returns the object.
/// </summary>
/// <typeparam name="T">The type of the object.</typeparam>
/// <typeparam name="TValue">The type of the value being compared.</typeparam>
/// <param name="this">The object on which this method was called.</param>
/// <param name="comparer">The interface to use for comparisons.</param>
/// <param name="value">The value to compare.</param>
/// <param name="cases">
/// <para>An array of pairs, where each element consists of a value to compare against <paramref name="value"/>
/// and an optional action to perform on <paramref name="this"/> if they are equal.</para>
/// <para>If the action is <see langword="null"/>, no action is performed if the value is equal to <paramref name="value"/>;
/// instead, the method returns immediately.</para>
/// </param>
/// <returns>A reference to <paramref name="this"/> after one of the actions in <paramref name="cases"/>, if any, returns.</returns>
/// <exception cref="ArgumentNullException">
/// <para><paramref name="this"/> is <see langword="null"/>.</para>
/// <para>- or -</para>
/// <para><paramref name="cases"/> is <see langword="null"/>.</para>
/// </exception>
/// <remarks>
/// <para>If <paramref name="value"/> is not equal to any of the values in <paramref name="cases"/>, this method returns immediately.</para>
/// </remarks>
public static T Switch<T, TValue>(this T @this, IEqualityComparer<TValue> comparer, TValue value, params (TValue Comparand, Action<T>? Action)[] cases)
=> Switch(@this, comparer, value, null, cases);

/// <summary>
/// Selects an action to invoke on an object by comparing a given value against a list of comparands, then returns the object.
Expand Down Expand Up @@ -86,14 +137,43 @@ public static T Switch<T, TValue>(this T @this, TValue value, params (TValue Com
/// <para><paramref name="cases"/> is <see langword="null"/>.</para>
/// </exception>
public static T Switch<T, TValue>(this T @this, TValue value, FluentAction<T>? @default, params (TValue Comparand, FluentAction<T>? Action)[] cases)
where TValue : IEquatable<TValue>
=> Switch(@this, EqualityComparer<TValue>.Default, value, @default, cases);

/// <summary>
/// Selects an action to invoke on an object by comparing a given value against a list of comparands, then returns the object.
/// </summary>
/// <typeparam name="T">The type of the object.</typeparam>
/// <typeparam name="TValue">The type of the value being compared.</typeparam>
/// <param name="this">The object on which this method was called.</param>
/// <param name="comparer">The interface to use for comparisons.</param>
/// <param name="value">The value to compare.</param>
/// <param name="default">
/// <para>An optional action to perform on <paramref name="this"/> if <paramref name="value"/> is not equal
/// to any of the values in <paramref name="cases"/>.</para>
/// <para>If this parameter is <see langword="null"/> and <paramref name="value"/> is not equal
/// to any of the values in <paramref name="cases"/>, this method returns immediately.</para>
/// </param>
/// <param name="cases">
/// <para>An array of pairs, where each element consists of a value to compare against <paramref name="value"/>
/// and an optional action to perform on <paramref name="this"/> if they are equal.</para>
/// <para>If the action is <see langword="null"/>, no action is performed if the value is equal to <paramref name="value"/>;
/// instead, the method returns immediately.</para>
/// </param>
/// <returns>A reference to <paramref name="this"/> after one of the actions in <paramref name="cases"/>, if any, returns.</returns>
/// <exception cref="ArgumentNullException">
/// <para><paramref name="this"/> is <see langword="null"/>.</para>
/// <para>- or -</para>
/// <para><paramref name="cases"/> is <see langword="null"/>.</para>
/// </exception>
public static T Switch<T, TValue>(this T @this, IEqualityComparer<TValue> comparer, TValue value, FluentAction<T>? @default, params (TValue Comparand, FluentAction<T>? 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);
}
Expand Down Expand Up @@ -128,14 +208,43 @@ public static T Switch<T, TValue>(this T @this, TValue value, FluentAction<T>? @
/// <para><paramref name="cases"/> is <see langword="null"/>.</para>
/// </exception>
public static T Switch<T, TValue>(this T @this, TValue value, FluentAction<T>? @default, params (TValue Comparand, Action<T>? Action)[] cases)
where TValue : IEquatable<TValue>
=> Switch(@this, EqualityComparer<TValue>.Default, value, @default, cases);

/// <summary>
/// Selects an action to invoke on an object by comparing a given value against a list of comparands, then returns the object.
/// </summary>
/// <typeparam name="T">The type of the object.</typeparam>
/// <typeparam name="TValue">The type of the value being compared.</typeparam>
/// <param name="this">The object on which this method was called.</param>
/// <param name="comparer">The interface to use for comparisons.</param>
/// <param name="value">The value to compare.</param>
/// <param name="default">
/// <para>An optional action to perform on <paramref name="this"/> if <paramref name="value"/> is not equal
/// to any of the values in <paramref name="cases"/>.</para>
/// <para>If this parameter is <see langword="null"/> and <paramref name="value"/> is not equal
/// to any of the values in <paramref name="cases"/>, this method returns immediately.</para>
/// </param>
/// <param name="cases">
/// <para>An array of pairs, where each element consists of a value to compare against <paramref name="value"/>
/// and an optional action to perform on <paramref name="this"/> if they are equal.</para>
/// <para>If the action is <see langword="null"/>, no action is performed if the value is equal to <paramref name="value"/>;
/// instead, the method returns immediately.</para>
/// </param>
/// <returns>A reference to <paramref name="this"/> after one of the actions in <paramref name="cases"/>, if any, returns.</returns>
/// <exception cref="ArgumentNullException">
/// <para><paramref name="this"/> is <see langword="null"/>.</para>
/// <para>- or -</para>
/// <para><paramref name="cases"/> is <see langword="null"/>.</para>
/// </exception>
public static T Switch<T, TValue>(this T @this, IEqualityComparer<TValue> comparer, TValue value, FluentAction<T>? @default, params (TValue Comparand, Action<T>? 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;
Expand Down
4 changes: 4 additions & 0 deletions src/Louis/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ static Louis.Fluency.FluentExtensions.InvokeIf<T, TArg1, TArg2, TArg3>(this T th
static Louis.Fluency.FluentExtensions.InvokeIf<T, TArg1, TArg2>(this T this, bool condition, TArg1 arg1, TArg2 arg2, System.Action<T, TArg1, TArg2>! action) -> T
static Louis.Fluency.FluentExtensions.InvokeIf<T, TArg>(this T this, bool condition, TArg arg, System.Action<T, TArg>! action) -> T
static Louis.Fluency.FluentExtensions.InvokeIf<T>(this T this, bool condition, System.Action<T>! action) -> T
static Louis.Fluency.FluentExtensions.Switch<T, TValue>(this T this, System.Collections.Generic.IEqualityComparer<TValue>! comparer, TValue value, Louis.Fluency.FluentAction<T>? default, params (TValue Comparand, Louis.Fluency.FluentAction<T>? Action)[]! cases) -> T
static Louis.Fluency.FluentExtensions.Switch<T, TValue>(this T this, System.Collections.Generic.IEqualityComparer<TValue>! comparer, TValue value, Louis.Fluency.FluentAction<T>? default, params (TValue Comparand, System.Action<T>? Action)[]! cases) -> T
static Louis.Fluency.FluentExtensions.Switch<T, TValue>(this T this, System.Collections.Generic.IEqualityComparer<TValue>! comparer, TValue value, params (TValue Comparand, Louis.Fluency.FluentAction<T>? Action)[]! cases) -> T
static Louis.Fluency.FluentExtensions.Switch<T, TValue>(this T this, System.Collections.Generic.IEqualityComparer<TValue>! comparer, TValue value, params (TValue Comparand, System.Action<T>? Action)[]! cases) -> T
static Louis.Fluency.FluentExtensions.Switch<T, TValue>(this T this, TValue value, Louis.Fluency.FluentAction<T>? default, params (TValue Comparand, Louis.Fluency.FluentAction<T>? Action)[]! cases) -> T
static Louis.Fluency.FluentExtensions.Switch<T, TValue>(this T this, TValue value, Louis.Fluency.FluentAction<T>? default, params (TValue Comparand, System.Action<T>? Action)[]! cases) -> T
static Louis.Fluency.FluentExtensions.Switch<T, TValue>(this T this, TValue value, params (TValue Comparand, Louis.Fluency.FluentAction<T>? Action)[]! cases) -> T
Expand Down

0 comments on commit 486cbb0

Please sign in to comment.