mirror of
https://github.com/2martens/tsw-timetable.git
synced 2026-10-17 07:57:55 +02:00
Moved generic value classes to separate module
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
plugins {
|
||||
id("twomartens.kotlin")
|
||||
id("twomartens.java")
|
||||
kotlin("kapt")
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package de.twomartens.timetable.types
|
||||
|
||||
@JvmInline
|
||||
value class Hour private constructor(val value: Int) {
|
||||
init {
|
||||
require(value in 0..23) {
|
||||
"Hour must be between 0 and 23"
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun of(hour: Int): Hour {
|
||||
return Hour(hour)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package de.twomartens.timetable.types
|
||||
|
||||
import java.time.LocalDate
|
||||
import java.time.LocalDateTime
|
||||
import java.time.temporal.ChronoUnit
|
||||
|
||||
@JvmInline
|
||||
value class HourAtDay private constructor(private val value: LocalDateTime) {
|
||||
init {
|
||||
require(value.minute == 0 && value.second == 0 && value.nano == 0) {
|
||||
"DateTime must only include an hour and the date."
|
||||
}
|
||||
}
|
||||
|
||||
val date: LocalDate
|
||||
get() {
|
||||
return value.toLocalDate()
|
||||
}
|
||||
|
||||
val hour: Hour
|
||||
get() {
|
||||
return Hour.of(value.hour)
|
||||
}
|
||||
|
||||
val dateTime: LocalDateTime
|
||||
get() {
|
||||
return value
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun of(dateTime: LocalDateTime): HourAtDay {
|
||||
return HourAtDay(dateTime.truncatedTo(ChronoUnit.HOURS))
|
||||
}
|
||||
|
||||
fun of(hour: Hour, date: LocalDate): HourAtDay {
|
||||
return HourAtDay(date.atTime(hour.value, 0))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package de.twomartens.timetable.types
|
||||
|
||||
@JvmInline
|
||||
value class NonEmptyString(val name: String) {
|
||||
init {
|
||||
require(name.isNotBlank()) {
|
||||
"Name must not be blank"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package de.twomartens.timetable.types
|
||||
|
||||
@JvmInline
|
||||
value class PositiveInteger(val value: Int) {
|
||||
init {
|
||||
require(value > 0) {
|
||||
"Value must be positive integer"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user