1/*
2 * Copyright (C) 2008 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
17package org.apache.harmony.luni.lang.reflect;
18
19import java.lang.reflect.Type;
20import java.util.ArrayList;
21
22public class ListOfTypes {
23    static final ListOfTypes empty = new ListOfTypes(0);
24
25    ArrayList<Type> list;
26    private Type[] resolvedTypes;
27
28    void add(Type elem) {
29        if (elem == null) {
30            throw new RuntimeException("Adding null type is not allowed!");
31        }
32        list.add(elem);
33    }
34
35    ListOfTypes(int capacity) {
36        list = new ArrayList<Type>(capacity);
37    }
38
39    ListOfTypes(Type[] types) {
40        list = new ArrayList<Type>();
41        for(Type t : types) {
42            list.add(t);
43        }
44    }
45
46    int length() {
47        return list.size();
48    }
49
50    @Override
51    public String toString() {
52        StringBuilder sb = new StringBuilder();
53        int i = 0;
54        for (Type t : list) {
55            if (i != 0) { sb.append(", "); }
56            sb.append(t.toString());
57        }
58        return sb.toString();
59    }
60
61    // Returns not null, but maybe an array of length 0.
62    public Type[] getResolvedTypes() {
63        if (resolvedTypes == null) {
64            resolvedTypes = new Type[list.size()];
65            int i = 0;
66            for (Type t : list) {
67                try {
68                    resolvedTypes[i] = ((ImplForType)t).getResolvedType();
69                } catch (ClassCastException e) {
70                    resolvedTypes[i] = t;
71                }
72                i++;
73            }
74            list = null;
75        }
76        return resolvedTypes;
77    }
78}
79