1 /*
2  * Copyright (C) 2023 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.cobalt.data;
18 
19 import androidx.annotation.NonNull;
20 import androidx.room.ColumnInfo;
21 import androidx.room.Entity;
22 import androidx.room.PrimaryKey;
23 
24 import com.google.auto.value.AutoValue;
25 import com.google.auto.value.AutoValue.CopyAnnotations;
26 
27 import java.time.Instant;
28 
29 /** Stores values used for features as string (key, value) pairs. */
30 @AutoValue
31 @CopyAnnotations
32 @Entity(tableName = "GlobalValues")
33 abstract class GlobalValueEntity {
34     enum Key {
35         INITIAL_ENABLED_TIME,
36         INITIAL_DISABLED_TIME,
37     }
38 
39     /** The feature's key. */
40     @CopyAnnotations
41     @ColumnInfo(name = "key")
42     @PrimaryKey
43     @NonNull
key()44     abstract Key key();
45 
46     /** The feature's value. */
47     @CopyAnnotations
48     @ColumnInfo(name = "value")
49     @NonNull
value()50     abstract String value();
51 
52     /**
53      * Creates a {@link GlobalValueEntity}.
54      *
55      * <p>Used by Room to instantiate objects.
56      */
57     @NonNull
create(Key key, String value)58     static GlobalValueEntity create(Key key, String value) {
59         return new AutoValue_GlobalValueEntity(key, value);
60     }
61 
timeFromDbString(String time)62     static Instant timeFromDbString(String time) {
63         return Instant.parse(time);
64     }
65 
timeToDbString(Instant time)66     static String timeToDbString(Instant time) {
67         return time.toString();
68     }
69 }
70