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 libcore.reflect;
18
19import java.lang.reflect.MalformedParameterizedTypeException;
20import java.lang.reflect.Type;
21import java.lang.reflect.WildcardType;
22import java.util.Arrays;
23
24public final class WildcardTypeImpl implements WildcardType {
25
26    private final ListOfTypes extendsBound, superBound;
27
28    public WildcardTypeImpl(ListOfTypes extendsBound, ListOfTypes superBound) {
29        this.extendsBound = extendsBound;
30        this.superBound = superBound;
31    }
32
33    public Type[] getLowerBounds() throws TypeNotPresentException,
34            MalformedParameterizedTypeException {
35        return superBound.getResolvedTypes().clone();
36    }
37
38    public Type[] getUpperBounds() throws TypeNotPresentException,
39            MalformedParameterizedTypeException {
40        return extendsBound.getResolvedTypes().clone();
41    }
42
43    @Override
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
54    public int hashCode() {
55        return 31 * Arrays.hashCode(getLowerBounds()) +
56                Arrays.hashCode(getUpperBounds());
57    }
58
59    @Override
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