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.item
18 
19 import com.android.tools.metalava.model.ApiVariantSelectorsFactory
20 import com.android.tools.metalava.model.BaseModifierList
21 import com.android.tools.metalava.model.ClassItem
22 import com.android.tools.metalava.model.Codebase
23 import com.android.tools.metalava.model.ItemDocumentationFactory
24 import com.android.tools.metalava.model.ItemLanguage
25 import com.android.tools.metalava.model.PackageItem
26 import com.android.tools.metalava.reporter.FileLocation
27 
28 open class DefaultPackageItem(
29     codebase: Codebase,
30     fileLocation: FileLocation,
31     itemLanguage: ItemLanguage,
32     modifiers: BaseModifierList,
33     documentationFactory: ItemDocumentationFactory,
34     variantSelectorsFactory: ApiVariantSelectorsFactory,
35     private val qualifiedName: String,
36     val containingPackage: PackageItem?,
37     override val overviewDocumentation: ResourceFile?,
38 ) :
39     DefaultSelectableItem(
40         codebase = codebase,
41         fileLocation = fileLocation,
42         itemLanguage = itemLanguage,
43         modifiers = modifiers,
44         documentationFactory = documentationFactory,
45         variantSelectorsFactory = variantSelectorsFactory,
46     ),
47     PackageItem {
48 
49     init {
50         // Newly created package's always have `emit = false` as they should only be emitted if they
51         // have at least one class that has `emit = true`. That will be updated, if necessary, when
52         // adding a class to the package.
53         emit = false
54     }
55 
56     private val topClasses = mutableListOf<ClassItem>()
57 
qualifiedNamenull58     final override fun qualifiedName(): String = qualifiedName
59 
60     final override fun topLevelClasses(): List<ClassItem> =
61         // Return a copy to avoid a ConcurrentModificationException.
62         topClasses.toList()
63 
64     // N.A. a package cannot be contained in a class
65     override fun containingClass(): ClassItem? = null
66 
67     final override fun containingPackage(): PackageItem? {
68         return containingPackage
69     }
70 
addTopClassnull71     fun addTopClass(classItem: ClassItem) {
72         topClasses.add(classItem)
73     }
74 }
75