xref: /aosp_15_r20/external/android_onboarding/java/com/android/onboarding/versions/ComplianceDate.kt (revision c625018464ae97c56936c82b1b617e11aa899faa)
1 package com.android.onboarding.versions
2 
3 import com.android.onboarding.versions.annotations.ChangeId
4 import java.time.LocalDate
5 
6 /**
7  * A class representing a specific compliance date which can be compared to other compliance dates.
8  *
9  * <p>If not specified, or is earlier than {@link EARLIEST_COMPLIANCE_DATE}, this will default to
10  * the {@link EARLIEST_COMPLIANCE_DATE}.
11  */
12 @JvmInline
13 value class ComplianceDate(private val complianceDate: LocalDate) {
14   constructor(
15     version: String? = null
16   ) : this(
<lambda>null17     version?.let(LocalDate::parse)?.takeUnless { it.isBefore(EARLIEST_COMPLIANCE_DATE_LOCALDATE) }
18       ?: EARLIEST_COMPLIANCE_DATE_LOCALDATE
19   )
20 
21   /** True if the compliance date is at least {@code version} */
isAtLeastnull22   fun isAtLeast(version: String): Boolean = isAtLeast(ComplianceDate(version))
23 
24   /** True if the compliance date is at least {@code version} */
25   fun isAtLeast(version: ComplianceDate): Boolean = !version.complianceDate.isAfter(complianceDate)
26 
27   /** True if the package's version is at most {@code version} */
28   fun isAtMost(version: String): Boolean = isAtMost(ComplianceDate(version))
29 
30   /** True if the package's version is at most {@code version} */
31   fun isAtMost(version: ComplianceDate): Boolean = !complianceDate.isAfter(version.complianceDate)
32 
33   /** True if the package's version is the same as {@code version} */
34   fun isEqualTo(version: String): Boolean = isEqualTo(ComplianceDate(version))
35 
36   /** True if the package's version is the same as {@code version} */
37   fun isEqualTo(version: ComplianceDate): Boolean = version.complianceDate == complianceDate
38 
39   /** True if the package's version is less than {@code version} */
40   fun isLessThan(version: String): Boolean = isLessThan(ComplianceDate(version))
41 
42   /** True if the package's version is less than {@code version} */
43   fun isLessThan(version: ComplianceDate): Boolean = version.complianceDate.isAfter(complianceDate)
44 
45   /**
46    * True if the [complianceDate] is greater than or equal to the available date of the [changeId].
47    */
48   fun isAvailable(changeId: ChangeId) =
49     if (changeId.available == ChangeId.NOT_AVAILABLE) {
50       false
51     } else {
52       isAtLeast(changeId.available)
53     }
54 
55   companion object {
56     /**
57      * The default for any APK for whom no compliance date is specified.
58      *
59      * <p>This represents the oldest API version which must be supported by all onboarding
60      * components. Any code dealing with versions older than this can be safely removed.
61      */
62     val EARLIEST_COMPLIANCE_DATE_STRING = "2023-07-05"
63 
64     /**
65      * The default for any APK for whom no compliance date is specified.
66      *
67      * <p>This represents the oldest API version which must be supported by all onboarding
68      * components. Any code dealing with versions older than this can be safely removed.
69      */
70     val EARLIEST_COMPLIANCE_DATE_LOCALDATE = LocalDate.parse(EARLIEST_COMPLIANCE_DATE_STRING)
71 
72     /**
73      * The default for any APK for whom no compliance date is specified.
74      *
75      * <p>This represents the oldest API version which must be supported by all onboarding
76      * components. Any code dealing with versions older than this can be safely removed.
77      */
78     val EARLIEST = ComplianceDate(EARLIEST_COMPLIANCE_DATE_LOCALDATE)
79   }
80 }
81