• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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.adservices.shared.datastore_testing;
18 
19 import androidx.datastore.guava.GuavaDataStore;
20 
21 import com.android.adservices.shared.common.ApplicationContextSingleton;
22 import com.android.adservices.shared.datastore.ProtoSerializer;
23 import com.android.adservices.shared.datastore_testing.proto.ExampleDatastoreProto;
24 
25 import com.google.common.util.concurrent.ListenableFuture;
26 import com.google.protobuf.ExtensionRegistryLite;
27 
28 import java.util.function.Function;
29 
30 /**
31  * Wrapper class for to read and update the {@link ExampleDatastoreProto} stored in the datastore.
32  */
33 final class ExampleDataStore {
34     private final GuavaDataStore<ExampleDatastoreProto> mDataStore;
35 
ExampleDataStore(String fileName)36     ExampleDataStore(String fileName) {
37         mDataStore =
38                 new GuavaDataStore.Builder<ExampleDatastoreProto>(
39                                 ApplicationContextSingleton.get(),
40                                 fileName,
41                                 new ProtoSerializer<ExampleDatastoreProto>(
42                                         ExampleDatastoreProto.getDefaultInstance(),
43                                         ExtensionRegistryLite.getEmptyRegistry()))
44                         .build();
45     }
46 
getDataAsync()47     ListenableFuture<ExampleDatastoreProto> getDataAsync() {
48         return mDataStore.getDataAsync();
49     }
50 
updateDataAsync( Function<ExampleDatastoreProto, ExampleDatastoreProto> transform)51     ListenableFuture<ExampleDatastoreProto> updateDataAsync(
52             Function<ExampleDatastoreProto, ExampleDatastoreProto> transform) {
53         return mDataStore.updateDataAsync(input -> transform.apply(input));
54     }
55 }
56