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 libcore.reflect; 18 19 import java.lang.reflect.Type; 20 import java.util.ArrayList; 21 import java.util.List; 22 import libcore.util.EmptyArray; 23 24 public final class ListOfTypes { 25 public static final ListOfTypes EMPTY = new ListOfTypes(0); 26 27 private final ArrayList<Type> types; 28 private Type[] resolvedTypes; 29 ListOfTypes(int capacity)30 ListOfTypes(int capacity) { 31 types = new ArrayList<Type>(capacity); 32 } 33 ListOfTypes(Type[] types)34 ListOfTypes(Type[] types) { 35 this.types = new ArrayList<Type>(types.length); 36 for (Type type : types) { 37 this.types.add(type); 38 } 39 } 40 add(Type type)41 void add(Type type) { 42 if (type == null) { 43 throw new NullPointerException("type == null"); 44 } 45 types.add(type); 46 } 47 length()48 int length() { 49 return types.size(); 50 } 51 getResolvedTypes()52 public Type[] getResolvedTypes() { 53 Type[] result = resolvedTypes; 54 if (result == null) { 55 result = resolveTypes(types); 56 resolvedTypes = result; 57 } 58 return result; 59 } 60 resolveTypes(List<Type> unresolved)61 private Type[] resolveTypes(List<Type> unresolved) { 62 int size = unresolved.size(); 63 if (size == 0) { 64 return EmptyArray.TYPE; 65 } 66 Type[] result = new Type[size]; 67 for (int i = 0; i < size; i++) { 68 Type type = unresolved.get(i); 69 if (type instanceof ParameterizedTypeImpl) { 70 result[i] = ((ParameterizedTypeImpl) type).getResolvedType(); 71 } else { 72 result[i] = type; 73 } 74 } 75 return result; 76 } 77 toString()78 @Override public String toString() { 79 StringBuilder result = new StringBuilder(); 80 for (int i = 0; i < types.size(); i++) { 81 if (i > 0) { 82 result.append(", "); 83 } 84 result.append(types.get(i)); 85 } 86 return result.toString(); 87 } 88 } 89