Skip to content

Instantly share code, notes, and snippets.

@alorma
Created March 2, 2018 19:39
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 alorma/8d49b40792405732e383d26d10287813 to your computer and use it in GitHub Desktop.
Save alorma/8d49b40792405732e383d26d10287813 to your computer and use it in GitHub Desktop.
class DelayedTimeProvider(private val timeProvider: TimeProvider,
private val delayed: Delayed) : TimeProvider {
override fun getTime(): Long = timeProvider.getTime() + delayed.get()
}
data class Delayed(private val time: Long, private val unit: TimeUnit) {
fun get() = unit.toMillis(time)
}
interface TimeProvider {
fun getTime(): Long
}
class TimeToLive(private val ttlTime: Long, private val unit: TimeUnit, private val timeProvider: TimeProvider) {
fun isValid(time: Long): Boolean = (timeProvider.now() - time) <= unit.toMillis(ttlTime)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment