ListOfTypes.java revision e96f94f57430bf3060581c816cc3a148adbe91a4
1e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom/*
2e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom * Copyright (C) 2008 The Android Open Source Project
3e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom *
4e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom * Licensed under the Apache License, Version 2.0 (the "License");
5e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom * you may not use this file except in compliance with the License.
6e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom * You may obtain a copy of the License at
7e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom *
8e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom *      http://www.apache.org/licenses/LICENSE-2.0
9e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom *
10e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom * Unless required by applicable law or agreed to in writing, software
11e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom * distributed under the License is distributed on an "AS IS" BASIS,
12e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom * See the License for the specific language governing permissions and
14e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom * limitations under the License.
15e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom */
16e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom
17e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrompackage libcore.reflect;
18e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom
19e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstromimport java.lang.reflect.Type;
20e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstromimport java.util.ArrayList;
21e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstromimport java.util.List;
22e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom
23e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrompublic final class ListOfTypes {
24e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    public static final ListOfTypes EMPTY = new ListOfTypes(0);
25e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom
26e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    private final ArrayList<Type> types;
27e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    private Type[] resolvedTypes;
28e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom
29e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    ListOfTypes(int capacity) {
30e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom        types = new ArrayList<Type>(capacity);
31e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    }
32e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom
33e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    ListOfTypes(Type[] types) {
34e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom        this.types = new ArrayList<Type>(types.length);
35e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom        for (Type type : types) {
36e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom            this.types.add(type);
37e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom        }
38e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    }
39e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom
40e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    void add(Type type) {
41e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom        if (type == null) {
42e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom            throw new NullPointerException("type == null");
43e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom        }
44e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom        types.add(type);
45e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    }
46e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom
47e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    int length() {
48e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom        return types.size();
49e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    }
50e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom
51e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    public Type[] getResolvedTypes() {
52e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom        Type[] result = resolvedTypes;
53e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom        return result != null ? result : (resolvedTypes = resolveTypes(types));
54e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    }
55e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom
56e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    private Type[] resolveTypes(List<Type> unresolved) {
57e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom        Type[] result = new Type[unresolved.size()];
58e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom        for (int i = 0; i < unresolved.size(); i++) {
59e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom            Type type = unresolved.get(i);
60e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom            try {
61e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom                result[i] = ((ParameterizedTypeImpl) type).getResolvedType();
62e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom            } catch (ClassCastException e) {
63e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom                result[i] = type;
64e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom            }
65e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom        }
66e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom        return result;
67e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    }
68e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom
69e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    @Override public String toString() {
70e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom        StringBuilder result = new StringBuilder();
71e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom        for (int i = 0; i < types.size(); i++) {
72e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom            if (i > 0) {
73e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom                result.append(", ");
74e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom            }
75e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom            result.append(types.get(i));
76e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom        }
77e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom        return result.toString();
78e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom    }
79e96f94f57430bf3060581c816cc3a148adbe91a4Brian Carlstrom}
80