1 /*
<lambda>null2  * 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.AppFunctionContext
21 import androidx.appfunctions.AppFunctionFunctionNotFoundException
22 
23 /**
24  * An [AppFunctionInvoker] that will delegate [unsafeInvoke] to the implementation that supports the
25  * given function call request.
26  *
27  * AppFunction compiler will automatically generate the implementation of this class.
28  */
29 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
30 public abstract class AggregatedAppFunctionInvoker : AppFunctionInvoker {
31 
32     /** The list of [AppFunctionInvoker] instances that contribute to this aggregate. */
33     public abstract val invokers: List<AppFunctionInvoker>
34 
35     final override val supportedFunctionIds: Set<String> by lazy {
36         // Empty collection can't be reduced
37         if (invokers.isEmpty()) return@lazy emptySet<String>()
38         invokers.map(AppFunctionInvoker::supportedFunctionIds).reduce { acc, ids -> acc + ids }
39     }
40 
41     final override suspend fun unsafeInvoke(
42         appFunctionContext: AppFunctionContext,
43         functionIdentifier: String,
44         parameters: Map<String, Any?>
45     ): Any? {
46         for (invoker in invokers) {
47             if (invoker.supportedFunctionIds.contains(functionIdentifier)) {
48                 return invoker.unsafeInvoke(appFunctionContext, functionIdentifier, parameters)
49             }
50         }
51         throw AppFunctionFunctionNotFoundException("Unable to find $functionIdentifier")
52     }
53 }
54