1 /*
2  * Copyright (C) 2018 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.doclava1.TextCodebase
20 import com.android.tools.metalava.model.TypeParameterItem
21 import com.android.tools.metalava.model.TypeParameterList
22 
23 class TextTypeParameterList(val codebase: TextCodebase, private val typeListString: String) : TypeParameterList {
24     private var typeParameters: List<TypeParameterItem>? = null
25     private var typeParameterNames: List<String>? = null
26 
toStringnull27     override fun toString(): String = typeListString
28 
29     override fun typeParameterNames(): List<String> {
30         if (typeParameterNames == null) {
31 //     TODO: Delete this method now that I'm doing it differently: typeParameterNames(typeListString)
32             val typeParameters = typeParameters()
33             val names = ArrayList<String>(typeParameters.size)
34             for (parameter in typeParameters) {
35                 names.add(parameter.simpleName())
36             }
37             typeParameterNames = names
38         }
39         return typeParameterNames!!
40     }
41 
typeParametersnull42     override fun typeParameters(): List<TypeParameterItem> {
43         if (typeParameters == null) {
44             val strings = typeParameterStrings(typeListString)
45             val list = ArrayList<TypeParameterItem>(strings.size)
46             strings.mapTo(list) { TextTypeParameterItem.create(codebase, it) }
47             typeParameters = list
48         }
49         return typeParameters!!
50     }
51 
52     companion object {
createnull53         fun create(codebase: TextCodebase, typeListString: String): TypeParameterList {
54             return TextTypeParameterList(codebase, typeListString)
55         }
56 
typeParameterStringsnull57         fun typeParameterStrings(typeString: String?): List<String> {
58             val s = typeString ?: return emptyList()
59             val list = mutableListOf<String>()
60             var balance = 0
61             var expect = false
62             var start = 0
63             for (i in 0 until s.length) {
64                 val c = s[i]
65                 if (c == '<') {
66                     balance++
67                     expect = balance == 1
68                 } else if (c == '>') {
69                     balance--
70                     if (balance == 1) {
71                         add(list, s, start, i + 1)
72                         start = i + 1
73                     } else if (balance == 0) {
74                         add(list, s, start, i)
75                         return list
76                     }
77                 } else if (c == ',') {
78                     expect = if (balance == 1) {
79                         add(list, s, start, i)
80                         true
81                     } else {
82                         false
83                     }
84                 } else if (expect && balance == 1) {
85                     start = i
86                     expect = false
87                 }
88             }
89             return list
90         }
91 
addnull92         private fun add(list: MutableList<String>, s: String, from: Int, to: Int) {
93             for (i in from until to) {
94                 if (!Character.isWhitespace(s[i])) {
95                     list.add(s.substring(i, to))
96                     return
97                 }
98             }
99         }
100     }
101 }