1 /*
2  * Copyright (C) 2017 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.turbine
18 
19 import com.android.tools.metalava.model.ClassItem
20 import com.android.tools.metalava.model.DefaultModifierList
21 import com.android.tools.metalava.model.PackageItem
22 import com.android.tools.metalava.model.VisibilityLevel
23 import com.android.tools.metalava.reporter.FileLocation
24 
25 internal class TurbinePackageItem(
26     codebase: TurbineBasedCodebase,
27     fileLocation: FileLocation,
28     private val qualifiedName: String,
29     modifiers: DefaultModifierList,
30     documentation: String,
31     isInitiallyHidden: Boolean,
32 ) : TurbineItem(codebase, fileLocation, modifiers, documentation, isInitiallyHidden), PackageItem {
33 
34     private var topClasses = mutableListOf<TurbineClassItem>()
35 
36     private var containingPackage: PackageItem? = null
37 
38     companion object {
createnull39         fun create(
40             codebase: TurbineBasedCodebase,
41             fileLocation: FileLocation,
42             qualifiedName: String,
43             modifiers: DefaultModifierList,
44             documentation: String,
45         ): TurbinePackageItem {
46             val isHidden = codebase.isPackageHidden(qualifiedName)
47             if (modifiers.isPackagePrivate()) {
48                 modifiers.setVisibilityLevel(VisibilityLevel.PUBLIC)
49             }
50             return TurbinePackageItem(
51                 codebase,
52                 fileLocation,
53                 qualifiedName,
54                 modifiers,
55                 documentation,
56                 isHidden,
57             )
58         }
59     }
60     // N.A. a package cannot be contained in a class
containingClassnull61     override fun containingClass(): ClassItem? = null
62 
63     fun updateOriginallyHiddenStatus(documentation: String) {
64         this.originallyHidden =
65             this.originallyHidden ||
66                 documentation.contains("@hide") ||
67                 documentation.contains("@pending") ||
68                 hasHideAnnotation()
69     }
70 
equalsnull71     override fun equals(other: Any?): Boolean {
72         if (this === other) {
73             return true
74         }
75         return other is PackageItem && qualifiedName == other.qualifiedName()
76     }
77 
hashCodenull78     override fun hashCode(): Int = qualifiedName.hashCode()
79 
80     override fun qualifiedName(): String = qualifiedName
81 
82     override fun topLevelClasses(): Sequence<ClassItem> = topClasses.asSequence()
83 
84     internal fun addTopClass(classItem: TurbineClassItem) {
85         topClasses.add(classItem)
86     }
87 
containingPackagenull88     override fun containingPackage(): PackageItem? {
89         // if this package is root package, then return null
90         return if (qualifiedName.isEmpty()) null
91         else {
92             if (containingPackage == null) {
93                 // If package is of the form A.B then the containing package is A
94                 // If package is top level, then containing package is the root package
95                 val name = qualifiedName()
96                 val lastDot = name.lastIndexOf('.')
97                 containingPackage =
98                     if (lastDot != -1) codebase.findPackage(name.substring(0, lastDot))
99                     else codebase.findPackage("")
100             }
101             return containingPackage
102         }
103     }
104 }
105