1 /*
2  * Copyright 2025 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.internal
18 
19 import androidx.annotation.RestrictTo
20 import androidx.appfunctions.AppFunctionData
21 
22 /**
23  * An interface for factory classes that convert between a class annotated with
24  * [androidx.appfunctions.AppFunctionSerializable] and [androidx.appfunctions.AppFunctionData].
25  *
26  * Each class annotated with [androidx.appfunctions.AppFunctionSerializable] will have a generated
27  * class that implements this interface.
28  */
29 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
30 public interface AppFunctionSerializableFactory<T : Any> {
31     /**
32      * Deserializes the given [androidx.appfunctions.AppFunctionData] into an instance of the
33      * AppFunctionSerializable annotated class.
34      *
35      * Type mismatch: An [IllegalArgumentException] if a property is stored as a different type in
36      * [appFunctionData].
37      */
fromAppFunctionDatanull38     public fun fromAppFunctionData(appFunctionData: AppFunctionData): T
39 
40     /** Serializes the given class into an [AppFunctionData]. */
41     public fun toAppFunctionData(appFunctionSerializable: T): AppFunctionData
42 
43     /**
44      * Contains the information about the type parameter.
45      *
46      * The class is used by [AppFunctionSerializableFactory] for generic serializable to resolve the
47      * type information in runtime.
48      */
49     public sealed class TypeParameter<T> {
50         /** The [TypeParameter] for Kotlin primitive types. */
51         public data class PrimitiveTypeParameter<T>(
52             /** The type class. */
53             val clazz: Class<T>,
54         ) : TypeParameter<T>()
55 
56         /** The [TypeParameter] for [List] type. */
57         public data class ListTypeParameter<I, T : List<*>?>(
58             /** The item type class. */
59             val itemClazz: Class<I>,
60         ) : TypeParameter<T>()
61     }
62 }
63