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.model.TypeAliasItem 27 import com.android.tools.metalava.reporter.FileLocation 28 29 open class DefaultPackageItem( 30 codebase: Codebase, 31 fileLocation: FileLocation, 32 itemLanguage: ItemLanguage, 33 modifiers: BaseModifierList, 34 documentationFactory: ItemDocumentationFactory, 35 variantSelectorsFactory: ApiVariantSelectorsFactory, 36 private val qualifiedName: String, 37 val containingPackage: PackageItem?, 38 override val overviewDocumentation: ResourceFile?, 39 ) : 40 DefaultSelectableItem( 41 codebase = codebase, 42 fileLocation = fileLocation, 43 itemLanguage = itemLanguage, 44 modifiers = modifiers, 45 documentationFactory = documentationFactory, 46 variantSelectorsFactory = variantSelectorsFactory, 47 ), 48 PackageItem { 49 50 init { 51 // Newly created package's always have `emit = false` as they should only be emitted if they 52 // have at least one class that has `emit = true`. That will be updated, if necessary, when 53 // adding a class or type alias to the package. 54 emit = false 55 } 56 57 private val topClasses = mutableListOf<ClassItem>() 58 qualifiedNamenull59 final override fun qualifiedName(): String = qualifiedName 60 61 final override fun topLevelClasses(): List<ClassItem> = 62 // Return a copy to avoid a ConcurrentModificationException. 63 topClasses.toList() 64 65 // N.A. a package cannot be contained in a class 66 override fun containingClass(): ClassItem? = null 67 68 final override fun containingPackage(): PackageItem? { 69 return containingPackage 70 } 71 addTopClassnull72 fun addTopClass(classItem: ClassItem) { 73 topClasses.add(classItem) 74 } 75 76 private val typeAliases = mutableListOf<TypeAliasItem>() 77 addTypeAliasnull78 internal fun addTypeAlias(typeAlias: DefaultTypeAliasItem) { 79 typeAliases += typeAlias 80 } 81 typeAliasesnull82 override fun typeAliases(): List<TypeAliasItem> { 83 return typeAliases.toList() 84 } 85 } 86