1 /*
2 * Copyright (C) 2022 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.libraries.pcc.chronicle.api.optics
18
19 /**
20 * A manifest of all known optics which is capable of composing bespoke, nested optics for a given
21 * [OpticalAccessPath].
22 */
23 interface OpticsManifest {
24 /**
25 * Composes a [Traversal] for a given [accessPath].
26 *
27 * If one can't be composed (either the access path is invalid, or the manifest doesn't contain
28 * sufficient optics to compose a satisfactory result - an exception will be thrown).
29 *
30 * TODO(b/204939436): Consider making an API like this (or something nicer) - possibly as
31 * extension funs.
32 * ```
33 * optics[AppOps::class]["appOps"]["lastAccessTimeMs"]
34 * optics[AppOps::class]["appOps.lastAccessTimeMs"]
35 * ```
36 */
composeTraversalnull37 fun <S, T, A, B> composeTraversal(
38 accessPath: OpticalAccessPath,
39 sourceEntityType: Class<out S>,
40 targetEntityType: Class<out T>,
41 sourceFieldType: Class<out A>,
42 targetFieldType: Class<out B>,
43 ): Traversal<S, T, A, B>
44 }
45
46 /**
47 * Assemble a polymorphic [Traversal] for a given [accessPath].
48 *
49 * If one can't be assembled (either the access path is invalid, or the manifest doesn't contain
50 * sufficient optics to compose a satisfactory result - an exception will be thrown).
51 */
52 inline fun <reified S, reified T, reified A, reified B> OpticsManifest.composePoly(
53 accessPath: OpticalAccessPath
54 ): Traversal<S, T, A, B> {
55 return composeTraversal(accessPath, S::class.java, T::class.java, A::class.java, B::class.java)
56 }
57
58 /**
59 * Assemble a monomorphic [Traversal] for a given [accessPath].
60 *
61 * If one can't be assembled (either the access path is invalid, or the manifest doesn't contain
62 * sufficient optics to compose a satisfactory result - an exception will be thrown).
63 */
composeMononull64 inline fun <reified S, reified A> OpticsManifest.composeMono(
65 accessPath: OpticalAccessPath
66 ): Traversal<S, S, A, A> {
67 return composeTraversal(accessPath, S::class.java, S::class.java, A::class.java, A::class.java)
68 }
69