• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
34     private val classesNames = HashSet<String>(100)
35 
namenull36     fun name() = name
37 
38     fun addClass(classInfo: TextClassItem) {
39         val classFullName = classInfo.fullName()
40         if (classFullName in classesNames) {
41             return
42         }
43         classes.add(classInfo)
44         classesNames.add(classFullName)
45     }
46 
pruneClassListnull47     internal fun pruneClassList() {
48         val iterator = classes.listIterator()
49         while (iterator.hasNext()) {
50             val cls = iterator.next()
51             if (cls.isInnerClass()) {
52                 iterator.remove()
53             }
54         }
55     }
56 
classListnull57     internal fun classList(): List<ClassItem> = classes
58 
59     override fun topLevelClasses(): Sequence<ClassItem> = classes.asSequence()
60 
61     override fun qualifiedName(): String = name
62 
63     override fun containingClass(strict: Boolean): ClassItem? = null
64 
65     override fun equals(other: Any?): Boolean {
66         if (this === other) return true
67         if (other !is PackageItem) return false
68 
69         return name == other.qualifiedName()
70     }
71 
hashCodenull72     override fun hashCode(): Int {
73         return name.hashCode()
74     }
75 
toStringnull76     override fun toString(): String = "package $name"
77 }
78