M
M
Maxim Siomin2021-07-16 16:07:58
Kotlin
Maxim Siomin, 2021-07-16 16:07:58

How to limit the value of a variable in a data class?

Here is my data class:

// Entity of query
@Entity(tableName = TABLE_NAME)
data class HistoryItem(
    @PrimaryKey(autoGenerate = true)
    val id: Int,

    @ColumnInfo(name = SEARCHED_DOMAIN)
    val searchedDomain: String,

    @ColumnInfo(name = STATUS)
    val status: Int,
)

And statuses:
object Statuses { 
    const val FAILURE = 0
    const val NOT_FOUND = 1
    const val FOUND = 2
}

How can I make it so that int status from HistoryItem can only be set to the value from Statuses?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
illuzor, 2021-07-16
@MaxSiominDev

Use enum instead of int.
It seems that the latest version of room can automatically work with enum. If not, you can write an adapter.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question