1 /*
2  * Copyright (C) 2018 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
18 
19 import com.android.tools.metalava.model.Item
20 import java.util.function.Predicate
21 
22 /** Types of APIs emitted (or parsed etc.) */
23 enum class ApiType(val flagName: String, val displayName: String = flagName) {
24     /** The public API */
25     PUBLIC_API("api", "public") {
26 
getNonElidingFilternull27         override fun getNonElidingFilter(apiPredicateConfig: ApiPredicate.Config): Predicate<Item> {
28             // This filter is for API signature files, where we don't need the "for stub purposes"
29             // APIs.
30             return ApiPredicate(
31                 includeApisForStubPurposes = false,
32                 config = apiPredicateConfig,
33             )
34         }
35 
getReferenceFilternull36         override fun getReferenceFilter(apiPredicateConfig: ApiPredicate.Config): Predicate<Item> {
37             return ApiPredicate(config = apiPredicateConfig.copy(ignoreShown = true))
38         }
39     },
40 
41     /** The API that has been removed */
42     REMOVED("removed", "removed") {
43 
getNonElidingFilternull44         override fun getNonElidingFilter(apiPredicateConfig: ApiPredicate.Config): Predicate<Item> {
45             // This filter is for API signature files, where we don't need the "for stub purposes"
46             // APIs.
47             return ApiPredicate(
48                 includeApisForStubPurposes = false,
49                 matchRemoved = true,
50                 config = apiPredicateConfig,
51             )
52         }
53 
getReferenceFilternull54         override fun getReferenceFilter(apiPredicateConfig: ApiPredicate.Config): Predicate<Item> {
55             return ApiPredicate(
56                 ignoreRemoved = true,
57                 config = apiPredicateConfig.copy(ignoreShown = true),
58             )
59         }
60     },
61 
62     /** Everything */
63     ALL("all", "all") {
64 
getNonElidingFilternull65         override fun getNonElidingFilter(apiPredicateConfig: ApiPredicate.Config): Predicate<Item> {
66             return Predicate { it.emit }
67         }
68 
getEmitFilternull69         override fun getEmitFilter(apiPredicateConfig: ApiPredicate.Config): Predicate<Item> {
70             return Predicate { it.emit }
71         }
72 
getReferenceFilternull73         override fun getReferenceFilter(apiPredicateConfig: ApiPredicate.Config): Predicate<Item> {
74             return Predicate { true }
75         }
76     };
77 
getNonElidingFilternull78     abstract fun getNonElidingFilter(apiPredicateConfig: ApiPredicate.Config): Predicate<Item>
79 
80     open fun getEmitFilter(apiPredicateConfig: ApiPredicate.Config): Predicate<Item> {
81         val nonElidingFilter = FilterPredicate(getNonElidingFilter(apiPredicateConfig))
82         val referenceFilter = getReferenceFilter(apiPredicateConfig)
83         return nonElidingFilter.and(elidingPredicate(referenceFilter, apiPredicateConfig))
84     }
85 
getReferenceFilternull86     abstract fun getReferenceFilter(apiPredicateConfig: ApiPredicate.Config): Predicate<Item>
87 
88     /**
89      * Create an [ElidingPredicate] that wraps [wrappedPredicate] and uses information from the
90      * [apiPredicateConfig].
91      */
92     protected fun elidingPredicate(
93         wrappedPredicate: Predicate<Item>,
94         apiPredicateConfig: ApiPredicate.Config
95     ) =
96         ElidingPredicate(
97             wrappedPredicate,
98             addAdditionalOverrides = apiPredicateConfig.addAdditionalOverrides,
99         )
100 
101     override fun toString(): String = displayName
102 }
103