Skip to content

Commit

Permalink
feat: caclulate valid holidays for specified timerange
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The year is not a constructor argument for KHol anymore, it is now a function parameter
  • Loading branch information
triplem committed Dec 24, 2024
1 parent 00a4375 commit 484fc3b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 16 deletions.
17 changes: 15 additions & 2 deletions src/main/kotlin/org/javafreedom/khol/KHol.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<LocalDate> {
if (year < 1990) throw KHolException()

fun validHolidays() : List<LocalDate> {
val result = mutableListOf<LocalDate>()

holidays.declarations().filter {
Expand All @@ -19,4 +21,15 @@ class KHol(val holidays: HolidayDeclarations, val year: Int, val validIn: String

return result
}

fun validHolidays(start: LocalDate, end: LocalDate) : List<LocalDate> {
if (start >= end) throw KHolException()

val result = mutableListOf<LocalDate>()
for (year in start.year..end.year) {
validHolidays(year).filter { it > start && it <= end }.forEach { result.add(it) }
}

return result
}
}
4 changes: 4 additions & 0 deletions src/main/kotlin/org/javafreedom/khol/KHolException.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.javafreedom.khol

class KHolException: RuntimeException() {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand Down
23 changes: 11 additions & 12 deletions src/test/kotlin/org/javafreedom/khol/KHolGermanHolidaysTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -11,47 +12,45 @@ 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"))
}

@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")) }
}

@Test
fun thereAreNoDefinedHolidaysBefore1990() {
val sut = KHol(GermanHolidayDeclarations(), 1989, "HE")
val sut = KHol(GermanHolidayDeclarations(), "HE")

val validHolidays = sut.validHolidays()

assertEquals(0, validHolidays.size)
assertThrows<KHolException> { 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)
}
Expand Down

0 comments on commit 484fc3b

Please sign in to comment.