summaryrefslogtreecommitdiff
path: root/packages/SystemUI/src/com/android/systemui/log/table/TableChange.kt
blob: b73ddc50f831a5726dbe845191683ffb64c7c02f (plain)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.log.table

/**
 * A object used with [TableLogBuffer] to store changes in variables over time. Is recyclable.
 *
 * Each message represents a change to exactly 1 type, specified by [DataType].
 */
data class TableChange(
    var timestamp: Long = 0,
    var columnPrefix: String = "",
    var columnName: String = "",
    var type: DataType = DataType.EMPTY,
    var bool: Boolean = false,
    var int: Int? = null,
    var str: String? = null,
) {
    /** Resets to default values so that the object can be recycled. */
    fun reset(timestamp: Long, columnPrefix: String, columnName: String) {
        this.timestamp = timestamp
        this.columnPrefix = columnPrefix
        this.columnName = columnName
        this.type = DataType.EMPTY
        this.bool = false
        this.int = 0
        this.str = null
    }

    /** Sets this to store a string change. */
    fun set(value: String?) {
        type = DataType.STRING
        str = value
    }

    /** Sets this to store a boolean change. */
    fun set(value: Boolean) {
        type = DataType.BOOLEAN
        bool = value
    }

    /** Sets this to store an int change. */
    fun set(value: Int?) {
        type = DataType.INT
        int = value
    }

    /** Updates this to store the same value as [change]. */
    fun updateTo(change: TableChange) {
        reset(change.timestamp, change.columnPrefix, change.columnName)
        when (change.type) {
            DataType.STRING -> set(change.str)
            DataType.INT -> set(change.int)
            DataType.BOOLEAN -> set(change.bool)
            DataType.EMPTY -> {}
        }
    }

    /** Returns true if this object has a change. */
    fun hasData(): Boolean {
        return columnName.isNotBlank() && type != DataType.EMPTY
    }

    fun getName(): String {
        return if (columnPrefix.isNotBlank()) {
            "$columnPrefix.$columnName"
        } else {
            columnName
        }
    }

    fun getVal(): String {
        return when (type) {
            DataType.EMPTY -> null
            DataType.STRING -> str
            DataType.INT -> int
            DataType.BOOLEAN -> bool
        }.toString()
    }

    enum class DataType {
        STRING,
        BOOLEAN,
        INT,
        EMPTY,
    }
}