forked from PolymerLabs/arcs
/
Ttl.kt
44 lines (37 loc) · 1.32 KB
/
Ttl.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
* Copyright 2020 Google LLC.
*
* This code may only be used under the BSD style license found at
* http://polymer.github.io/LICENSE.txt
*
* Code distributed by Google as part of this project is also subject to an additional IP rights
* grant found at
* http://polymer.github.io/PATENTS.txt
*/
package arcs.core.data
import arcs.core.util.Time
/** A class containing retention policy information. */
sealed class Ttl(count: Int, val isInfinite: Boolean = false) {
val minutes: Int = count * when (this) {
is Minutes -> 1
is Hours -> 60
is Days -> 60 * 24
is Infinite -> 1
}
init {
require(count > 0 || isInfinite) {
"must be either positive count on infinite, " +
"but got count=$count and isInfinite=$isInfinite"
}
}
override fun equals(other: Any?): Boolean =
other is Ttl && minutes == other.minutes
override fun hashCode(): Int = minutes.hashCode()
fun calculateExpiration(time: Time): Long =
if (isInfinite) RawEntity.UNINITIALIZED_TIMESTAMP
else requireNotNull(time).currentTimeMillis + (minutes * 60 * 1000)
data class Minutes(val count: Int) : Ttl(count)
data class Hours(val count: Int) : Ttl(count)
data class Days(val count: Int) : Ttl(count)
object Infinite : Ttl(-1, true)
}