This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Talk private constructor(id: String, | |
title: String, | |
duration: Duration) { | |
val id = notBlank(id, "Cannot create a Talk is a blank id")!! | |
val title = notBlank(title, "Cannot create a Talk is a blank title")!! | |
val duration = notNegative(duration) | |
val format = TalkFormat.ofDuration(duration) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum class TalkFormat(val format: String, val durationRange: ClosedRange<Duration>) { | |
IGNITE("IGNITE", ofMinutes(1).rangeTo(ofMinutes(10).minusNanos(1))), | |
QUICKIE("QUICKIE", ofMinutes(10).rangeTo(ofMinutes(20).minusNanos(1))), | |
TOOL_IN_ACTION("TOOL_IN_ACTION", ofMinutes(20).rangeTo(ofMinutes(40).minusNanos(1))), | |
CONFERENCE("CONFERENCE", ofMinutes(40).rangeTo(ofMinutes(60).minusNanos(1))), | |
UNIVERSITY("UNIVERSITY", ofHours(1).rangeTo(ofHours(4))); | |
companion object Converter { | |
fun ofDuration(duration: Duration): TalkFormat { | |
isTrue(lessThanTheMinimumDuration(duration), "Duration is less than expected") | |
return values() | |
.singleOrNull { duration.isInRange(it.durationRange) } | |
?: UNIVERSITY | |
} | |
private fun lessThanTheMinimumDuration(duration: Duration): Boolean { | |
val firstRange = IGNITE.durationRange | |
return duration.coerceIn(firstRange) != firstRange.start | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment