1 /*
2  * Copyright 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 // @exportToFramework:skipFile()
17 package androidx.appsearch.app;
18 
19 import androidx.annotation.RestrictTo;
20 import androidx.appsearch.annotation.Document;
21 
22 import org.jspecify.annotations.NonNull;
23 import org.jspecify.annotations.Nullable;
24 
25 import java.util.Collections;
26 import java.util.List;
27 import java.util.Map;
28 
29 /**
30  * A context object that holds mapping information for document classes and their parent types.
31  *
32  * <p>This class encapsulates the {@code documentClassMap} and {@code parentTypeMap} used during
33  * the deserialization of {@link GenericDocument} instances into specific document classes.
34  *
35  * @see GenericDocument#toDocumentClass(Class, DocumentClassMappingContext)
36  */
37 public class DocumentClassMappingContext {
38     /** An empty {@link DocumentClassMappingContext} instance. */
39     @ExperimentalAppSearchApi
40     @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
41     public static final DocumentClassMappingContext EMPTY =
42             new DocumentClassMappingContext(/* documentClassMap= */null, /* parentTypeMap= */null);
43 
44     private final @NonNull Map<String, List<String>> mDocumentClassMap;
45     private final @NonNull Map<String, List<String>> mParentTypeMap;
46 
47     /**
48      * Constructs a new {@link DocumentClassMappingContext}.
49      *
50      * @param documentClassMap A map from AppSearch's type name specified by {@link Document#name()}
51      *                         to the list of the fully qualified names of the corresponding
52      *                         document classes. In most cases, passing the value returned by
53      *                         {@link AppSearchDocumentClassMap#getGlobalMap()} will be sufficient.
54      * @param parentTypeMap    A map from AppSearch's type name specified by {@link Document#name()}
55      *                         to the list of its parent type names. In most cases, passing the
56      *                         value returned by {@link SearchResult#getParentTypeMap()} will be
57      *                         sufficient.
58      */
59     @ExperimentalAppSearchApi
DocumentClassMappingContext( @ullable Map<String, List<String>> documentClassMap, @Nullable Map<String, List<String>> parentTypeMap)60     public DocumentClassMappingContext(
61             @Nullable Map<String, List<String>> documentClassMap,
62             @Nullable Map<String, List<String>> parentTypeMap) {
63         mDocumentClassMap = documentClassMap != null ? Collections.unmodifiableMap(documentClassMap)
64                 : Collections.emptyMap();
65         mParentTypeMap = parentTypeMap != null ? Collections.unmodifiableMap(parentTypeMap)
66                 : Collections.emptyMap();
67     }
68 
69     /**
70      * Returns the document class map.
71      */
72     @ExperimentalAppSearchApi
getDocumentClassMap()73     public @NonNull Map<String, List<String>> getDocumentClassMap() {
74         return mDocumentClassMap;
75     }
76 
77     /**
78      * Returns the parent type map.
79      */
80     @ExperimentalAppSearchApi
getParentTypeMap()81     public @NonNull Map<String, List<String>> getParentTypeMap() {
82         return mParentTypeMap;
83     }
84 }
85