• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 com.android.tools.metalava.model.item
18 
19 import com.android.tools.metalava.model.Codebase
20 import com.android.tools.metalava.model.ParameterItem
21 
22 /**
23  * A lamda that given a [ParameterItem] will create a [ParameterDefaultValue] for it.
24  *
25  * This is called from within the constructor of the [ParameterItem] and should not access any
26  * properties of [ParameterItem] as they may not have been initialized. This should just store a
27  * reference for later use.
28  */
29 typealias ParameterDefaultValueFactory = (ParameterItem) -> ParameterDefaultValue
30 
31 /** Indicates whether a parameter has a default value. */
32 interface ParameterDefaultValue {
33 
34     /** A [ParameterDefaultValue] to use for a parameter that has no default value. */
35     data object NONE : ParameterDefaultValue {
hasDefaultValuenull36         override fun hasDefaultValue() = false
37 
38         /** This is suitable for use by [parameter] as it has no model or codebase dependencies. */
39         override fun duplicate(parameter: ParameterItem) = this
40 
41         /** This is suitable for use in the snapshot as it has no model or codebase dependencies. */
42         override fun snapshot(parameter: ParameterItem) = this
43     }
44 
45     /**
46      * A [ParameterDefaultValue] to use for a parameter that has a default value but its actual
47      * value is not known.
48      */
49     data object UNKNOWN : ParameterDefaultValue {
50         override fun hasDefaultValue() = true
51 
52         /** This is suitable for use by [parameter] as it has no model or codebase dependencies. */
53         override fun duplicate(parameter: ParameterItem) = this
54 
55         /** This is suitable for use in the snapshot as it has no model or codebase dependencies. */
56         override fun snapshot(parameter: ParameterItem) = this
57     }
58 
59     /**
60      * Returns whether this parameter has a default value.
61      *
62      * This is only supported in Kotlin.
63      */
hasDefaultValuenull64     fun hasDefaultValue(): Boolean
65 
66     /**
67      * Return a duplicate of this instance to use by [parameter] which will be in the same type of
68      * [Codebase] as this.
69      */
70     fun duplicate(parameter: ParameterItem): ParameterDefaultValue
71 
72     /**
73      * Creates a snapshot of this.
74      *
75      * The default implementation assumes that this is either dependent on a model or the codebase
76      * and so creates a new [ParameterDefaultValue] based on the functions above.
77      */
78     fun snapshot(parameter: ParameterItem) =
79         when {
80             !hasDefaultValue() -> NONE
81             else -> UNKNOWN
82         }
83 }
84