Skip to content

Instantly share code, notes, and snippets.

@beyondxscratch

beyondxscratch/Talk.kt

Last active Aug 10, 2020
Embed
What would you like to do?
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)
}
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
You can’t perform that action at this time.