1 /*
<lambda>null2  * 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 
17 package androidx.appfunctions.metadata
18 
19 import androidx.annotation.RestrictTo
20 import androidx.appsearch.annotation.Document
21 import java.util.Objects
22 
23 /** Represent the reusable components in a function specification. */
24 public class AppFunctionComponentsMetadata
25 @JvmOverloads
26 constructor(
27     /**
28      * The map of data types that can be reused across the schema.
29      *
30      * The *key* of this map is a string that serves as an *identifier* for the data type. This
31      * identifier can then be used to reference and reuse this `AppFunctionDataTypeMetadata`
32      * definition in other parts of the schema. For example, a Person type can be defined here with
33      * a key "Person" and referenced in multiple places by `#components/dataTypes/Person`.
34      *
35      * @see [AppFunctionReferenceTypeMetadata.referenceDataType]
36      */
37     public val dataTypes: Map<String, AppFunctionDataTypeMetadata> = emptyMap(),
38 ) {
39     override fun equals(other: Any?): Boolean {
40         if (this === other) return true
41         if (javaClass != other?.javaClass) return false
42 
43         other as AppFunctionComponentsMetadata
44 
45         return dataTypes == other.dataTypes
46     }
47 
48     override fun hashCode(): Int {
49         return Objects.hashCode(dataTypes)
50     }
51 
52     override fun toString(): String {
53         return "AppFunctionComponentsMetadata(dataTypes=$dataTypes)"
54     }
55 
56     /**
57      * Converts the [AppFunctionComponentsMetadata] to a [AppFunctionComponentsMetadataDocument].
58      *
59      * @return the [AppFunctionComponentsMetadataDocument] representation of the metadata.
60      */
61     @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
62     public fun toAppFunctionComponentsMetadataDocument(): AppFunctionComponentsMetadataDocument {
63         return AppFunctionComponentsMetadataDocument(
64             dataTypes =
65                 dataTypes.map { (name, dataType) ->
66                     AppFunctionNamedDataTypeMetadataDocument(
67                         name = name,
68                         dataTypeMetadata = dataType.toAppFunctionDataTypeMetadataDocument()
69                     )
70                 }
71         )
72     }
73 }
74 
75 /** Represents the persistent storage format of [AppFunctionComponentsMetadata]. */
76 @Document
77 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
78 public data class AppFunctionComponentsMetadataDocument(
79     @Document.Namespace public val namespace: String = APP_FUNCTION_NAMESPACE,
80     @Document.Id public val id: String = APP_FUNCTION_ID_EMPTY,
81     /** The list of data types that ban be reusable across the schema. */
82     @Document.DocumentProperty public val dataTypes: List<AppFunctionNamedDataTypeMetadataDocument>,
83 ) {
toAppFunctionComponentsMetadatanull84     public fun toAppFunctionComponentsMetadata(): AppFunctionComponentsMetadata =
85         AppFunctionComponentsMetadata(
86             dataTypes =
87                 dataTypes.associate {
88                     it.name to it.dataTypeMetadata.toAppFunctionDataTypeMetadata()
89                 }
90         )
91 }
92