1 /* 2 * Copyright (C) 2022 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 com.android.systemui.log.table 18 19 /** 20 * A object used with [TableLogBuffer] to store changes in variables over time. Is recyclable. 21 * 22 * Each message represents a change to exactly 1 type, specified by [DataType]. 23 */ 24 data class TableChange( 25 var timestamp: Long = 0, 26 var columnPrefix: String = "", 27 var columnName: String = "", 28 var type: DataType = DataType.EMPTY, 29 var bool: Boolean = false, 30 var int: Int? = null, 31 var str: String? = null, 32 ) { 33 /** Resets to default values so that the object can be recycled. */ resetnull34 fun reset(timestamp: Long, columnPrefix: String, columnName: String) { 35 this.timestamp = timestamp 36 this.columnPrefix = columnPrefix 37 this.columnName = columnName 38 this.type = DataType.EMPTY 39 this.bool = false 40 this.int = 0 41 this.str = null 42 } 43 44 /** Sets this to store a string change. */ setnull45 fun set(value: String?) { 46 type = DataType.STRING 47 str = value 48 } 49 50 /** Sets this to store a boolean change. */ setnull51 fun set(value: Boolean) { 52 type = DataType.BOOLEAN 53 bool = value 54 } 55 56 /** Sets this to store an int change. */ setnull57 fun set(value: Int?) { 58 type = DataType.INT 59 int = value 60 } 61 62 /** Updates this to store the same value as [change]. */ updateTonull63 fun updateTo(change: TableChange) { 64 reset(change.timestamp, change.columnPrefix, change.columnName) 65 when (change.type) { 66 DataType.STRING -> set(change.str) 67 DataType.INT -> set(change.int) 68 DataType.BOOLEAN -> set(change.bool) 69 DataType.EMPTY -> {} 70 } 71 } 72 73 /** Returns true if this object has a change. */ hasDatanull74 fun hasData(): Boolean { 75 return columnName.isNotBlank() && type != DataType.EMPTY 76 } 77 getNamenull78 fun getName(): String { 79 return if (columnPrefix.isNotBlank()) { 80 "$columnPrefix.$columnName" 81 } else { 82 columnName 83 } 84 } 85 getValnull86 fun getVal(): String { 87 return when (type) { 88 DataType.EMPTY -> null 89 DataType.STRING -> str 90 DataType.INT -> int 91 DataType.BOOLEAN -> bool 92 }.toString() 93 } 94 95 enum class DataType { 96 STRING, 97 BOOLEAN, 98 INT, 99 EMPTY, 100 } 101 } 102