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 d01edfd
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 23 deletions.
9 changes: 6 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ plugins {
group = "org.javafreedom"
project.version = project.findProperty("version") as String? ?: "0.0.1-SNAPSHOT"

val kotlinxDatetimeVersion = "0.6.1"
val konsistVersion = "0.17.3"

dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.6.1")
implementation("org.jetbrains.kotlinx:kotlinx-datetime:${kotlinxDatetimeVersion}")

testImplementation(kotlin("test"))

testImplementation("com.lemonappdev:konsist:0.17.3")
testImplementation("com.lemonappdev:konsist:${konsistVersion}")
}

tasks.test {
Expand All @@ -41,7 +44,7 @@ val version: String by lazy {
mavenPublishing {
println("Using Version: ${version}")

coordinates(group.toString(), rootProject.name, version)
coordinates(groupId = group.toString(), artifactId = rootProject.name, version = version)

publishToMavenCentral(SonatypeHost.CENTRAL_PORTAL)

Expand Down
26 changes: 24 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 < VALID_START_YEAR) throw KHolException("Year should be after 1989")

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

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

return result
}

/**
* All Holidays which are greater then, or equal to start and less then end are returned
*/
fun validHolidays(start: LocalDate, end: LocalDate) : List<LocalDate> {
if (start >= end) throw KHolException("Start Date should be before to End Date")

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

return result
}

companion object {
const val VALID_START_YEAR = 1990
}
}
3 changes: 3 additions & 0 deletions src/main/kotlin/org/javafreedom/khol/KHolException.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package org.javafreedom.khol

class KHolException(msg: String): RuntimeException(msg)
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
@file:Suppress("detekt:style:MagicNumber")
package org.javafreedom.khol.algorithm

import kotlinx.datetime.*
import kotlinx.datetime.DateTimeUnit
import kotlinx.datetime.LocalDate
import kotlinx.datetime.minus
import org.javafreedom.khol.BaseCalculationAlgorithm

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@file:Suppress("detekt:style:MagicNumber")
package org.javafreedom.khol.algorithm

import kotlinx.datetime.DateTimeUnit
Expand Down Expand Up @@ -37,7 +38,8 @@ class OrthodoxEaster : BaseCalculationAlgorithm {
private fun julianToGregorianDifference(year: Int): Int {
// The base year when the difference started (1582) - difference in days at that time was 10
val baseYear = 1582
// Difference in days (starts at 10 days for 1582, increases by 1 every 100 years except centuries divisible by 400)
// Difference in days (starts at 10 days for 1582, increases by 1 every 100 years except centuries
// divisible by 400)
var difference = 10

// Calculate how many centuries have passed since the base year
Expand Down
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
53 changes: 41 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,49 +12,77 @@ 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)
}

@Test
fun calculateHolidaysBetweenTwoDates() {
val sut = KHol(GermanHolidayDeclarations(), "HB")

val validHolidays = sut.validHolidays(
LocalDate(2024, 1, 1),
LocalDate(2025, 1, 1)
)

assertEquals(10, validHolidays.size)
}

@Test
fun calculateHolidaysBetweenTwoDatesEndIsExclusive() {
val sut = KHol(GermanHolidayDeclarations(), "HB")

val validHolidays = sut.validHolidays(LocalDate(2024, 1, 1),
LocalDate(2024, 12, 26))

assertEquals(9, validHolidays.size)
}

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

assertThrows<KHolException> { sut.validHolidays(LocalDate(2024, 1, 1),
LocalDate(2024, 1, 1)) }
}

}
4 changes: 2 additions & 2 deletions src/test/kotlin/org/javafreedom/khol/KonsistTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ internal class KonsistTest {
}

companion object {
val BASE_PACKAGE = "org.javafreedom.khol"
const val BASE_PACKAGE = "org.javafreedom.khol"
}
}
}

0 comments on commit d01edfd

Please sign in to comment.