1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.tools
18 
<lambda>null19 class TimestampFactory(private val realTimestampFormatter: (Long) -> String = { it.toString() }) {
<lambda>null20     private val empty by lazy { Timestamp(0L, 0L, 0L, realTimestampFormatter) }
<lambda>null21     private val min by lazy { Timestamp(1, 1, 1, realTimestampFormatter) }
<lambda>null22     private val max by lazy {
23         Timestamp(Long.MAX_VALUE, Long.MAX_VALUE, Long.MAX_VALUE, realTimestampFormatter)
24     }
25 
minnull26     fun min(): Timestamp = min
27     fun max(): Timestamp = max
28     fun empty(): Timestamp = empty
29 
30     fun from(
31         elapsedNanos: Long? = null,
32         systemUptimeNanos: Long? = null,
33         unixNanos: Long? = null,
34     ): Timestamp {
35         return Timestamp(
36             elapsedNanos ?: 0L,
37             systemUptimeNanos ?: 0L,
38             unixNanos ?: 0L,
39             realTimestampFormatter
40         )
41     }
42 
fromnull43     fun from(
44         elapsedNanos: String? = null,
45         systemUptimeNanos: String? = null,
46         unixNanos: String? = null,
47     ): Timestamp {
48         return from(
49             (elapsedNanos ?: "0").toLong(),
50             (systemUptimeNanos ?: "0").toLong(),
51             (unixNanos ?: "0").toLong()
52         )
53     }
54 
fromnull55     fun from(elapsedNanos: Long, elapsedOffsetNanos: Long): Timestamp {
56         return Timestamp(
57             elapsedNanos = elapsedNanos,
58             unixNanos = elapsedNanos + elapsedOffsetNanos,
59             realTimestampFormatter = realTimestampFormatter
60         )
61     }
62 
fromnull63     fun from(elapsedNanos: String, elapsedOffsetNanos: String): Timestamp {
64         val elapsedNanosLong = elapsedNanos.toLong()
65         return from(
66             elapsedNanos = elapsedNanosLong,
67             unixNanos = elapsedNanosLong + elapsedOffsetNanos.toLong()
68         )
69     }
70 }
71