Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement equality for 'ContrabSchedule' #56

Open
julealgon opened this issue Jan 9, 2020 · 1 comment
Open

Implement equality for 'ContrabSchedule' #56

julealgon opened this issue Jan 9, 2020 · 1 comment

Comments

@julealgon
Copy link

There is currently no way to check a ContrabSchedule object for equality against another schedule. It's also not possible to properly implement a custom EqualityComparer from the outside since the fields that store the data inside the schedule are all private.

This could be fixed by implementing IEquatable<ContrabSchedule> on the model.

@julealgon julealgon changed the title Implement 'IEquatable<ContrabSchedule>`' on 'ContrabSchedule' Implement 'IEquatable<ContrabSchedule>' on 'ContrabSchedule' Jan 9, 2020
@atifaziz
Copy link
Owner

I'll think about this but meanwhile…

There is currently no way to check a ContrabSchedule object for equality against another schedule.

The ToString() implementation of ContrabSchedule returns an expanded/lowered version of the one originally parsed expression:

/// <summary>
/// Returns a string in crontab expression (expanded) that represents
/// this schedule.
/// </summary>
public override string ToString()
{
var writer = new StringWriter(CultureInfo.InvariantCulture);
if (_seconds != null)
{
_seconds.Format(writer, true);
writer.Write(' ');
}
_minutes.Format(writer, true); writer.Write(' ');
_hours.Format(writer, true); writer.Write(' ');
_days.Format(writer, true); writer.Write(' ');
_months.Format(writer, true); writer.Write(' ');
_daysOfWeek.Format(writer, true);
return writer.ToString();
}

It can be used today as a good basis for equality comparison via an IEqualityComparer<CrontabSchedule> implementation like so:

sealed class EqualityComparer<CrontabSchedule> : IEqualityComparer<CrontabSchedule>
{
    public bool Equals(CrontabSchedule x, CrontabSchedule y) =>
        x?.ToString() == y?.ToString();

    public int GetHashCode(CrontabSchedule obj) =>
        obj.ToString().GetHashCode();
}

Usage would then be:

var comparer = new EqualityComparer<CrontabSchedule>();
var schedule1 = CrontabSchedule.Parse("0-59 * * * Mon-Fri/2");
var schedule2 = CrontabSchedule.Parse("* * * * Fri,Mon,Wed");
var schedule3 = CrontabSchedule.Parse("* * * * *");
Console.WriteLine(comparer.Equals(schedule1, schedule2)); // True
Console.WriteLine(comparer.Equals(schedule1, schedule3)); // False
Console.WriteLine(comparer.Equals(schedule2, schedule3)); // False

Note how 0-59 * * * Mon-Fri/2 (schedule1) and * * * * Fri,Mon,Wed (schedule2) compare equal even though they are expressed differently.

@atifaziz atifaziz changed the title Implement 'IEquatable<ContrabSchedule>' on 'ContrabSchedule' Implement equality for 'ContrabSchedule' Jan 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants