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.DefaultModifierList
21 import com.android.tools.metalava.model.PackageItem
22 import com.android.tools.metalava.reporter.FileLocation
23 
24 internal class TextPackageItem(
25     codebase: TextCodebase,
26     private val name: String,
27     modifiers: DefaultModifierList,
28     fileLocation: FileLocation
29 ) : TextItem(codebase, fileLocation, modifiers = modifiers), PackageItem {
30 
31     private val classes = ArrayList<ClassItem>(100)
32 
33     private val classesNames = HashSet<String>(100)
34 
namenull35     fun name() = name
36 
37     fun addClass(classInfo: ClassItem) {
38         val classFullName = classInfo.fullName()
39         if (classFullName in classesNames) {
40             return
41         }
42         classes.add(classInfo)
43         classesNames.add(classFullName)
44     }
45 
topLevelClassesnull46     override fun topLevelClasses(): Sequence<ClassItem> = classes.asSequence()
47 
48     override fun qualifiedName(): String = name
49 
50     override fun containingClass(): ClassItem? = null
51 
52     override fun equals(other: Any?): Boolean {
53         if (this === other) return true
54         if (other !is PackageItem) return false
55 
56         return name == other.qualifiedName()
57     }
58 
hashCodenull59     override fun hashCode(): Int {
60         return name.hashCode()
61     }
62 }
63