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.text
18 
19 import com.android.tools.metalava.model.ClassItem
20 import com.android.tools.metalava.model.PackageItem
21 
22 class TextPackageItem(
23     codebase: TextCodebase,
24     private val name: String,
25     modifiers: TextModifiers,
26     position: SourcePositionInfo
27 ) : TextItem(codebase, position, modifiers = modifiers), PackageItem {
28     init {
29         modifiers.setOwner(this)
30     }
31 
32     private val classes = ArrayList<TextClassItem>(100)
33 
namenull34     fun name() = name
35 
36     fun addClass(classInfo: TextClassItem) {
37         classes.add(classInfo)
38     }
39 
pruneClassListnull40     internal fun pruneClassList() {
41         val iterator = classes.listIterator()
42         while (iterator.hasNext()) {
43             val cls = iterator.next()
44             if (cls.isInnerClass()) {
45                 iterator.remove()
46             }
47         }
48     }
49 
classListnull50     internal fun classList(): List<ClassItem> = classes
51 
52     override fun topLevelClasses(): Sequence<ClassItem> = classes.asSequence()
53 
54     override fun qualifiedName(): String = name
55 
56     override fun containingClass(strict: Boolean): ClassItem? = null
57 
58     override fun equals(other: Any?): Boolean {
59         if (this === other) return true
60         if (other !is PackageItem) return false
61 
62         return name == other.qualifiedName()
63     }
64 
hashCodenull65     override fun hashCode(): Int {
66         return name.hashCode()
67     }
68 
toStringnull69     override fun toString(): String = "package $name"
70 }
71