From 484fc3b6455f470159697d9b0b5906b6f4ab1d99 Mon Sep 17 00:00:00 2001 From: triplem <160079+triplem@users.noreply.github.com> Date: Tue, 24 Dec 2024 20:22:07 +0100 Subject: [PATCH] feat: caclulate valid holidays for specified timerange BREAKING CHANGE: The year is not a constructor argument for KHol anymore, it is now a function parameter --- src/main/kotlin/org/javafreedom/khol/KHol.kt | 17 ++++++++++++-- .../org/javafreedom/khol/KHolException.kt | 4 ++++ .../khol/KHolGermanHolidaysJavaTest.java | 4 ++-- .../khol/KHolGermanHolidaysTest.kt | 23 +++++++++---------- 4 files changed, 32 insertions(+), 16 deletions(-) create mode 100644 src/main/kotlin/org/javafreedom/khol/KHolException.kt diff --git a/src/main/kotlin/org/javafreedom/khol/KHol.kt b/src/main/kotlin/org/javafreedom/khol/KHol.kt index aa42694..7bd9d83 100644 --- a/src/main/kotlin/org/javafreedom/khol/KHol.kt +++ b/src/main/kotlin/org/javafreedom/khol/KHol.kt @@ -5,9 +5,11 @@ import kotlinx.datetime.LocalDate /** * Some doc */ -class KHol(val holidays: HolidayDeclarations, val year: Int, val validIn: String) { +class KHol(private val holidays: HolidayDeclarations, private val validIn: String) { + + fun validHolidays(year: Int) : List { + if (year < 1990) throw KHolException() - fun validHolidays() : List { val result = mutableListOf() holidays.declarations().filter { @@ -19,4 +21,15 @@ class KHol(val holidays: HolidayDeclarations, val year: Int, val validIn: String return result } + + fun validHolidays(start: LocalDate, end: LocalDate) : List { + if (start >= end) throw KHolException() + + val result = mutableListOf() + for (year in start.year..end.year) { + validHolidays(year).filter { it > start && it <= end }.forEach { result.add(it) } + } + + return result + } } diff --git a/src/main/kotlin/org/javafreedom/khol/KHolException.kt b/src/main/kotlin/org/javafreedom/khol/KHolException.kt new file mode 100644 index 0000000..7ff39f1 --- /dev/null +++ b/src/main/kotlin/org/javafreedom/khol/KHolException.kt @@ -0,0 +1,4 @@ +package org.javafreedom.khol + +class KHolException: RuntimeException() { +} \ No newline at end of file diff --git a/src/test/java/org/javafreedom/khol/KHolGermanHolidaysJavaTest.java b/src/test/java/org/javafreedom/khol/KHolGermanHolidaysJavaTest.java index d638555..c69a993 100644 --- a/src/test/java/org/javafreedom/khol/KHolGermanHolidaysJavaTest.java +++ b/src/test/java/org/javafreedom/khol/KHolGermanHolidaysJavaTest.java @@ -11,9 +11,9 @@ public class KHolGermanHolidaysJavaTest { @Test void testNW2024() { var holidayDeclarations = new GermanHolidayDeclarations(); - var sut = new KHol(holidayDeclarations, 2024, "NW"); + var sut = new KHol(holidayDeclarations, "NW"); - var validHoldays = sut.validHolidays(); + var validHoldays = sut.validHolidays(2024); assertEquals(11, validHoldays.size()); assertTrue(validHoldays.stream().anyMatch(it -> diff --git a/src/test/kotlin/org/javafreedom/khol/KHolGermanHolidaysTest.kt b/src/test/kotlin/org/javafreedom/khol/KHolGermanHolidaysTest.kt index ca3d905..f6faeac 100644 --- a/src/test/kotlin/org/javafreedom/khol/KHolGermanHolidaysTest.kt +++ b/src/test/kotlin/org/javafreedom/khol/KHolGermanHolidaysTest.kt @@ -2,6 +2,7 @@ package org.javafreedom.khol import kotlinx.datetime.LocalDate import org.javafreedom.khol.declarations.GermanHolidayDeclarations +import org.junit.jupiter.api.assertThrows import kotlin.test.Test import kotlin.test.assertContains import kotlin.test.assertEquals @@ -11,9 +12,9 @@ internal class KHolGermanHolidaysTest { @Test fun northrhineContainsReformationDay() { - val sut = KHol(GermanHolidayDeclarations(), 2024, "NW") + val sut = KHol(GermanHolidayDeclarations(), "NW") - val validHolidays = sut.validHolidays() + val validHolidays = sut.validHolidays(2024) assertEquals(11, validHolidays.size) assertContains(validHolidays, LocalDate.parse("2024-11-01")) @@ -21,9 +22,9 @@ internal class KHolGermanHolidaysTest { @Test fun hessenDoesNotContainReformationDay() { - val sut = KHol(GermanHolidayDeclarations(), 2024, "HE") + val sut = KHol(GermanHolidayDeclarations(), "HE") - val validHolidays = sut.validHolidays() + val validHolidays = sut.validHolidays(2024) assertEquals(10, validHolidays.size) assertFalse { validHolidays.contains(LocalDate.parse("2024-11-01")) } @@ -31,27 +32,25 @@ internal class KHolGermanHolidaysTest { @Test fun thereAreNoDefinedHolidaysBefore1990() { - val sut = KHol(GermanHolidayDeclarations(), 1989, "HE") + val sut = KHol(GermanHolidayDeclarations(), "HE") - val validHolidays = sut.validHolidays() - - assertEquals(0, validHolidays.size) + assertThrows { sut.validHolidays(1989) } } @Test fun bremenHolidaysBefore2018WithoutReformationDay() { - val sut = KHol(GermanHolidayDeclarations(), 2017, "HB") + val sut = KHol(GermanHolidayDeclarations(), "HB") - val validHolidays = sut.validHolidays() + val validHolidays = sut.validHolidays(2017) assertEquals(9, validHolidays.size) } @Test fun bremenHolidays2018WithReformationDay() { - val sut = KHol(GermanHolidayDeclarations(), 2018, "HB") + val sut = KHol(GermanHolidayDeclarations(), "HB") - val validHolidays = sut.validHolidays() + val validHolidays = sut.validHolidays(2018) assertEquals(10, validHolidays.size) }