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

Refactoring HolidayProvider #622

Merged
merged 4 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class IHolidayProviderArchitectureTests
{
private static readonly IObjectProvider<Class> _holidayProviders = Classes()
.That()
.AreNotAbstract()
.And()
.AreAssignableTo(typeof(IHolidayProvider))
.As("Nager.Date HolidayProvider");

Expand Down
40 changes: 40 additions & 0 deletions src/Nager.Date/HolidayProviders/AbstractHolidayProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Nager.Date.Models;
using System.Collections.Generic;
using System.Linq;

namespace Nager.Date.HolidayProviders
{
/// <summary>
/// Abstract HolidayProvider
/// </summary>
internal abstract class AbstractHolidayProvider : IHolidayProvider
{
private readonly CountryCode _countryCode ;

/// <summary>
/// Abstract HolidayProvider
/// </summary>
/// <param name="countryCode"></param>
protected AbstractHolidayProvider(CountryCode countryCode)
{
this._countryCode = countryCode;
}

/// <inheritdoc/>
public IEnumerable<Holiday> GetHolidays(int year)
{
var holidaySpecifications = this.GetHolidaySpecifications(year);
var holidays = HolidaySpecificationProcessor.Process(holidaySpecifications, this._countryCode);
return holidays.OrderBy(o => o.Date);
}

/// <inheritdoc/>
public abstract IEnumerable<string> GetSources();

/// <summary>
/// Get Holiday specifications for a given year
/// </summary>
/// <param name="year"></param>
protected abstract IEnumerable<HolidaySpecification> GetHolidaySpecifications(int year);
}
}
14 changes: 5 additions & 9 deletions src/Nager.Date/HolidayProviders/AlandHolidayProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
using Nager.Date.ReligiousProviders;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Nager.Date.HolidayProviders
{
/// <summary>
/// Åland HolidayProvider
/// </summary>
internal sealed class AlandHolidayProvider : IHolidayProvider
internal sealed class AlandHolidayProvider : AbstractHolidayProvider
{
private readonly ICatholicProvider _catholicProvider;

Expand All @@ -19,16 +18,14 @@ internal sealed class AlandHolidayProvider : IHolidayProvider
/// </summary>
/// <param name="catholicProvider"></param>
public AlandHolidayProvider(
ICatholicProvider catholicProvider)
ICatholicProvider catholicProvider) : base(CountryCode.AX)
{
this._catholicProvider = catholicProvider;
}

/// <inheritdoc/>
public IEnumerable<Holiday> GetHolidays(int year)
protected override IEnumerable<HolidaySpecification> GetHolidaySpecifications(int year)
{
var countryCode = CountryCode.AX;

var thirdFridayInJune = DateHelper.FindDay(year, Month.June, DayOfWeek.Friday, Occurrence.Third);
var thirdSaturdayInJune = DateHelper.FindDay(year, Month.June, DayOfWeek.Saturday, Occurrence.Third);
var firstSaturdayInNovember = DateHelper.FindDay(year, Month.November, DayOfWeek.Saturday, Occurrence.First);
Expand Down Expand Up @@ -119,8 +116,7 @@ public IEnumerable<Holiday> GetHolidays(int year)
this._catholicProvider.Pentecost("Pingstdagen", year)
};

var holidays = HolidaySpecificationProcessor.Process(holidaySpecifications, countryCode);
return holidays.OrderBy(o => o.Date);
return holidaySpecifications;

//var items = new List<Holiday>();
//items.Add(new Holiday(year, 1, 1, "Nyårsdagen", "New Year's Day", countryCode));
Expand All @@ -144,7 +140,7 @@ public IEnumerable<Holiday> GetHolidays(int year)
}

/// <inheritdoc/>
public IEnumerable<string> GetSources()
public override IEnumerable<string> GetSources()
{
return
[
Expand Down
14 changes: 5 additions & 9 deletions src/Nager.Date/HolidayProviders/AlbaniaHolidayProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
using Nager.Date.ReligiousProviders;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Nager.Date.HolidayProviders
{
/// <summary>
/// Albania HolidayProvider
/// </summary>
internal sealed class AlbaniaHolidayProvider : IHolidayProvider
internal sealed class AlbaniaHolidayProvider : AbstractHolidayProvider
{
private readonly IOrthodoxProvider _orthodoxProvider;
private readonly ICatholicProvider _catholicProvider;
Expand All @@ -21,17 +20,15 @@ internal sealed class AlbaniaHolidayProvider : IHolidayProvider
/// <param name="orthodoxProvider"></param>
public AlbaniaHolidayProvider(
ICatholicProvider catholicProvider,
IOrthodoxProvider orthodoxProvider)
IOrthodoxProvider orthodoxProvider) : base(CountryCode.AL)
{
this._catholicProvider = catholicProvider;
this._orthodoxProvider = orthodoxProvider;
}

/// <inheritdoc/>
public IEnumerable<Holiday> GetHolidays(int year)
protected override IEnumerable<HolidaySpecification> GetHolidaySpecifications(int year)
{
var countryCode = CountryCode.AL;

var observedRuleSet = new ObservedRuleSet
{
Saturday = date => date.AddDays(2),
Expand Down Expand Up @@ -126,8 +123,7 @@ public IEnumerable<Holiday> GetHolidays(int year)
this._orthodoxProvider.EasterMonday("E hëna e Pashkëve Ortodokse", year)
};

var holidays = HolidaySpecificationProcessor.Process(holidaySpecifications, countryCode);
return holidays.OrderBy(o => o.Date);
return holidaySpecifications;

//TODO: Eid ul-Fitr is not implemented
//TODO: Eid ul-Adha is not implemented
Expand Down Expand Up @@ -163,7 +159,7 @@ public IEnumerable<Holiday> GetHolidays(int year)
//}

/// <inheritdoc/>
public IEnumerable<string> GetSources()
public override IEnumerable<string> GetSources()
{
return
[
Expand Down
13 changes: 5 additions & 8 deletions src/Nager.Date/HolidayProviders/AndorraHolidayProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
using Nager.Date.ReligiousProviders;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Nager.Date.HolidayProviders
{
/// <summary>
/// Andorra HolidayProvider
/// </summary>
internal sealed class AndorraHolidayProvider : IHolidayProvider
internal sealed class AndorraHolidayProvider : AbstractHolidayProvider
{
private readonly ICatholicProvider _catholicProvider;

Expand All @@ -18,15 +17,14 @@ internal sealed class AndorraHolidayProvider : IHolidayProvider
/// </summary>
/// <param name="catholicProvider"></param>
public AndorraHolidayProvider(
ICatholicProvider catholicProvider)
ICatholicProvider catholicProvider) : base(CountryCode.AD)
{
this._catholicProvider = catholicProvider;
}

/// <inheritdoc/>
public IEnumerable<Holiday> GetHolidays(int year)
protected override IEnumerable<HolidaySpecification> GetHolidaySpecifications(int year)
{
var countryCode = CountryCode.AD;
var easterSunday = this._catholicProvider.EasterSunday(year);

var holidaySpecifications = new List<HolidaySpecification>
Expand Down Expand Up @@ -113,8 +111,7 @@ public IEnumerable<Holiday> GetHolidays(int year)
this._catholicProvider.WhitMonday("Dilluns de Pentecosta", year)
};

var holidays = HolidaySpecificationProcessor.Process(holidaySpecifications, countryCode);
return holidays.OrderBy(o => o.Date);
return holidaySpecifications;


//var items = new List<Holiday>();
Expand All @@ -137,7 +134,7 @@ public IEnumerable<Holiday> GetHolidays(int year)
}

/// <inheritdoc/>
public IEnumerable<string> GetSources()
public override IEnumerable<string> GetSources()
{
return
[
Expand Down
13 changes: 5 additions & 8 deletions src/Nager.Date/HolidayProviders/ArgentinaHolidayProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
using Nager.Date.ReligiousProviders;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Nager.Date.HolidayProviders
{
/// <summary>
/// Argentina HolidayProvider
/// </summary>
internal sealed class ArgentinaHolidayProvider : IHolidayProvider
internal sealed class ArgentinaHolidayProvider : AbstractHolidayProvider
{
private readonly ICatholicProvider _catholicProvider;

Expand All @@ -19,15 +18,14 @@ internal sealed class ArgentinaHolidayProvider : IHolidayProvider
/// </summary>
/// <param name="catholicProvider"></param>
public ArgentinaHolidayProvider(
ICatholicProvider catholicProvider)
ICatholicProvider catholicProvider) : base(CountryCode.AR)
{
this._catholicProvider = catholicProvider;
}

/// <inheritdoc/>
public IEnumerable<Holiday> GetHolidays(int year)
protected override IEnumerable<HolidaySpecification> GetHolidaySpecifications(int year)
{
var countryCode = CountryCode.AR;
var easterSunday = this._catholicProvider.EasterSunday(year);

var thirdMondayInAugust = DateHelper.FindDay(year, Month.August, DayOfWeek.Monday, Occurrence.Third);
Expand Down Expand Up @@ -143,8 +141,7 @@ public IEnumerable<Holiday> GetHolidays(int year)
this._catholicProvider.GoodFriday("Viernes Santo", year)
};

var holidays = HolidaySpecificationProcessor.Process(holidaySpecifications, countryCode);
return holidays.OrderBy(o => o.Date);
return holidaySpecifications;

//var items = new List<Holiday>();
//items.Add(new Holiday(year, 1, 1, "Año Nuevo", "New Year's Day", countryCode));
Expand All @@ -168,7 +165,7 @@ public IEnumerable<Holiday> GetHolidays(int year)
}

/// <inheritdoc/>
public IEnumerable<string> GetSources()
public override IEnumerable<string> GetSources()
{
return
[
Expand Down
13 changes: 5 additions & 8 deletions src/Nager.Date/HolidayProviders/ArmeniaHolidayProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
using Nager.Date.ReligiousProviders;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Nager.Date.HolidayProviders
{
/// <summary>
/// Armenia HolidayProvider
/// </summary>
internal sealed class ArmeniaHolidayProvider : IHolidayProvider
internal sealed class ArmeniaHolidayProvider : AbstractHolidayProvider
{
private readonly ICatholicProvider _catholicProvider;

Expand All @@ -18,15 +17,14 @@ internal sealed class ArmeniaHolidayProvider : IHolidayProvider
/// </summary>
/// <param name="catholicProvider"></param>
public ArmeniaHolidayProvider(
ICatholicProvider catholicProvider)
ICatholicProvider catholicProvider) : base(CountryCode.AM)
{
this._catholicProvider = catholicProvider;
}

/// <inheritdoc/>
public IEnumerable<Holiday> GetHolidays(int year)
protected override IEnumerable<HolidaySpecification> GetHolidaySpecifications(int year)
{
var countryCode = CountryCode.AM;
var easterSunday = this._catholicProvider.EasterSunday(year);

var holidaySpecifications = new List<HolidaySpecification>
Expand Down Expand Up @@ -138,12 +136,11 @@ public IEnumerable<Holiday> GetHolidays(int year)
this._catholicProvider.EasterSunday("Ամանոր", year)
};

var holidays = HolidaySpecificationProcessor.Process(holidaySpecifications, countryCode);
return holidays.OrderBy(o => o.Date);
return holidaySpecifications;
}

/// <inheritdoc/>
public IEnumerable<string> GetSources()
public override IEnumerable<string> GetSources()
{
return
[
Expand Down
19 changes: 7 additions & 12 deletions src/Nager.Date/HolidayProviders/AustraliaHolidayProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
using Nager.Date.ReligiousProviders;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Nager.Date.HolidayProviders
{
/// <summary>
/// Australia HolidayProvider
/// </summary>
internal sealed class AustraliaHolidayProvider : IHolidayProvider, ISubdivisionCodesProvider
internal sealed class AustraliaHolidayProvider : AbstractHolidayProvider, ISubdivisionCodesProvider
{
private readonly ICatholicProvider _catholicProvider;

Expand All @@ -20,7 +19,7 @@ internal sealed class AustraliaHolidayProvider : IHolidayProvider, ISubdivisionC
/// </summary>
/// <param name="catholicProvider"></param>
public AustraliaHolidayProvider(
ICatholicProvider catholicProvider)
ICatholicProvider catholicProvider) : base(CountryCode.AU)
{
this._catholicProvider = catholicProvider;
}
Expand All @@ -42,9 +41,8 @@ public IDictionary<string, string> GetSubdivisionCodes()
}

/// <inheritdoc/>
public IEnumerable<Holiday> GetHolidays(int year)
protected override IEnumerable<HolidaySpecification> GetHolidaySpecifications(int year)
{
var countryCode = CountryCode.AU;
var easterSunday = this._catholicProvider.EasterSunday(year);

var secondMondayInMarch = DateHelper.FindDay(year, Month.March, DayOfWeek.Monday, Occurrence.Second);
Expand All @@ -63,14 +61,12 @@ public IEnumerable<Holiday> GetHolidays(int year)

var weekendObservedRuleSet = new ObservedRuleSet
{
Saturday = date => date.AddDays(2),
Sunday = date => date.AddDays(1),
Saturday = date => date.AddDays(2), Sunday = date => date.AddDays(1),
};

var weekendSequenceObservedRuleSet = new ObservedRuleSet
{
Saturday = date => date.AddDays(2),
Sunday = date => date.AddDays(2),
Saturday = date => date.AddDays(2), Sunday = date => date.AddDays(2),
};

var holidaySpecifications = new List<HolidaySpecification>
Expand Down Expand Up @@ -195,8 +191,7 @@ public IEnumerable<Holiday> GetHolidays(int year)
holidaySpecifications.AddRangeIfNotNull(this.MonarchBirthday(year));
holidaySpecifications.AddIfNotNull(this.MourningForQueenElizabeth(year));

var holidays = HolidaySpecificationProcessor.Process(holidaySpecifications, countryCode);
return holidays.OrderBy(o => o.Date);
return holidaySpecifications;

//var items = new List<Holiday>();
//items.Add(new Holiday(newYearsDay, "New Year's Day", "New Year's Day", countryCode));
Expand Down Expand Up @@ -356,7 +351,7 @@ private HolidaySpecification MourningForQueenElizabeth(int year)
}

/// <inheritdoc/>
public IEnumerable<string> GetSources()
public override IEnumerable<string> GetSources()
{
return
[
Expand Down
Loading