Skip to content

Instantly share code, notes, and snippets.

@beyondxscratch
Last active August 10, 2020 00:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beyondxscratch/d914091134d31e56e5fe9ff68b31d965 to your computer and use it in GitHub Desktop.
Save beyondxscratch/d914091134d31e56e5fe9ff68b31d965 to your computer and use it in GitHub Desktop.
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