1 /*
2  * Copyright 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 androidx.datastore.core
18 
19 /** Represents the current state of the DataStore. */
20 internal sealed class State<T>(val version: Int)
21 
22 internal object UnInitialized : State<Any>(-1)
23 
24 /** A read from disk has succeeded, value represents the current on disk state. */
25 internal class Data<T>(val value: T, val hashCode: Int, version: Int) : State<T>(version) {
checkHashCodenull26     fun checkHashCode() {
27         check(value.hashCode() == hashCode) {
28             "Data in DataStore was mutated but DataStore is only compatible with Immutable types."
29         }
30     }
31 }
32 
33 /** A read from disk has failed. ReadException is the exception that was thrown. */
34 internal class ReadException<T>(val readException: Throwable, version: Int) : State<T>(version)
35 
36 /** The scope has been cancelled. This DataStore cannot process any new reads or writes. */
37 internal class Final<T>(val finalException: Throwable) : State<T>(Int.MAX_VALUE)
38