OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / libcore / luni / src / main / java / org / apache / harmony / luni / lang / reflect / ListOfTypes.java
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
17 package org.apache.harmony.luni.lang.reflect;
18
19 import java.lang.reflect.Type;
20 import java.util.ArrayList;
21 import java.util.List;
22
23 public final class ListOfTypes {
24     public static final ListOfTypes EMPTY = new ListOfTypes(0);
25
26     private final ArrayList<Type> types;
27     private Type[] resolvedTypes;
28
29     ListOfTypes(int capacity) {
30         types = new ArrayList<Type>(capacity);
31     }
32
33     ListOfTypes(Type[] types) {
34         this.types = new ArrayList<Type>(types.length);
35         for (Type type : types) {
36             this.types.add(type);
37         }
38     }
39
40     void add(Type type) {
41         if (type == null) {
42             throw new NullPointerException("type == null");
43         }
44         types.add(type);
45     }
46
47     int length() {
48         return types.size();
49     }
50
51     public Type[] getResolvedTypes() {
52         Type[] result = resolvedTypes;
53         return result != null ? result : (resolvedTypes = resolveTypes(types));
54     }
55
56     private Type[] resolveTypes(List<Type> unresolved) {
57         Type[] result = new Type[unresolved.size()];
58         for (int i = 0; i < unresolved.size(); i++) {
59             Type type = unresolved.get(i);
60             try {
61                 result[i] = ((ImplForType) type).getResolvedType();
62             } catch (ClassCastException e) {
63                 result[i] = type;
64             }
65         }
66         return result;
67     }
68
69     @Override public String toString() {
70         StringBuilder result = new StringBuilder();
71         for (int i = 0; i < types.size(); i++) {
72             if (i > 0) {
73                 result.append(", ");
74             }
75             result.append(types.get(i));
76         }
77         return result.toString();
78     }
79 }