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.MalformedParameterizedTypeException;
20 import java.lang.reflect.Type;
21 import java.lang.reflect.WildcardType;
22 import java.util.Arrays;
23 
24 public final class WildcardTypeImpl implements WildcardType {
25 
26     private final ListOfTypes extendsBound, superBound;
27 
WildcardTypeImpl(ListOfTypes extendsBound, ListOfTypes superBound)28     public WildcardTypeImpl(ListOfTypes extendsBound, ListOfTypes superBound) {
29         this.extendsBound = extendsBound;
30         this.superBound = superBound;
31     }
32 
getLowerBounds()33     public Type[] getLowerBounds() throws TypeNotPresentException,
34             MalformedParameterizedTypeException {
35         return superBound.getResolvedTypes().clone();
36     }
37 
getUpperBounds()38     public Type[] getUpperBounds() throws TypeNotPresentException,
39             MalformedParameterizedTypeException {
40         return extendsBound.getResolvedTypes().clone();
41     }
42 
43     @Override
equals(Object o)44     public boolean equals(Object o) {
45         if(!(o instanceof WildcardType)) {
46             return false;
47         }
48         WildcardType that = (WildcardType) o;
49         return Arrays.equals(getLowerBounds(), that.getLowerBounds()) &&
50                 Arrays.equals(getUpperBounds(), that.getUpperBounds());
51     }
52 
53     @Override
hashCode()54     public int hashCode() {
55         return 31 * Arrays.hashCode(getLowerBounds()) +
56                 Arrays.hashCode(getUpperBounds());
57     }
58 
59     @Override
toString()60     public String toString() {
61         StringBuilder sb = new StringBuilder("?");
62         if ((extendsBound.length() == 1 && extendsBound.getResolvedTypes()[0] != Object.class)
63                 || extendsBound.length() > 1) {
64             sb.append(" extends ").append(extendsBound);
65         } else if (superBound.length() > 0) {
66             sb.append(" super ").append(superBound);
67         }
68         return sb.toString();
69     }
70 }
71