1 /*
2  * Copyright (C) 2009 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 signature.model.impl;
18 
19 import signature.model.IClassReference;
20 import signature.model.IParameterizedType;
21 import signature.model.ITypeReference;
22 import signature.model.util.ModelUtil;
23 
24 import java.io.Serializable;
25 import java.util.List;
26 
27 @SuppressWarnings("serial")
28 public class SigParameterizedType implements IParameterizedType, Serializable {
29 
30     private ITypeReference ownerType;
31     private IClassReference rawType;
32     private List<ITypeReference> typeArguments;
33 
SigParameterizedType(ITypeReference ownerType, IClassReference rawType, List<ITypeReference> typeArguments)34     public SigParameterizedType(ITypeReference ownerType,
35             IClassReference rawType, List<ITypeReference> typeArguments) {
36         this.ownerType = ownerType;
37         this.rawType = rawType;
38         this.typeArguments = typeArguments;
39     }
40 
getOwnerType()41     public ITypeReference getOwnerType() {
42         ITypeReference returnValue = ownerType;
43         if (returnValue == null) {
44             if (rawType.getClassDefinition().getDeclaringClass() != null) {
45                 returnValue = new SigClassReference(rawType
46                         .getClassDefinition().getDeclaringClass());
47             }
48         }
49         return returnValue;
50     }
51 
getRawType()52     public IClassReference getRawType() {
53         return rawType;
54     }
55 
getTypeArguments()56     public List<ITypeReference> getTypeArguments() {
57         return typeArguments;
58     }
59 
60     @Override
hashCode()61     public int hashCode() {
62         return hashCode(this);
63     }
64 
hashCode(IParameterizedType type)65     public static int hashCode(IParameterizedType type) {
66         final int prime = 31;
67         int result = 1;
68         result = prime * type.getRawType().hashCode();
69         result = prime * result + type.getTypeArguments().hashCode();
70         return result;
71     }
72 
73     @Override
equals(Object obj)74     public boolean equals(Object obj) {
75         return equals(this, obj);
76     }
77 
equals(IParameterizedType thiz, Object that)78     public static boolean equals(IParameterizedType thiz, Object that) {
79         if (!(that instanceof IParameterizedType)) {
80             return false;
81         }
82         IParameterizedType other = (IParameterizedType) that;
83         if (thiz.getOwnerType() == null) {
84             if (other.getOwnerType() != null) {
85                 return false;
86             }
87         } else if (Uninitialized.isInitialized(thiz.getOwnerType())) {
88             if (!Uninitialized.isInitialized(other.getOwnerType())) {
89                 return false;
90             }
91         } else if (!thiz.getOwnerType().equals(other.getOwnerType())) {
92             return false;
93         }
94         if (!thiz.getRawType().equals(other.getRawType())) {
95             return false;
96         }
97         if (!thiz.getTypeArguments().equals(other.getTypeArguments())) {
98             return false;
99         }
100         return true;
101     }
102 
103 
104     @Override
toString()105     public String toString() {
106         return SigParameterizedType.toString(this);
107     }
108 
toString(IParameterizedType type)109     public static String toString(IParameterizedType type) {
110         StringBuilder builder = new StringBuilder();
111         if (type.getOwnerType() != null) {
112             builder.append(type.getOwnerType().toString());
113             builder.append("::");
114         }
115         builder.append(type.getRawType());
116         builder.append("<");
117         builder.append(ModelUtil.separate(type.getTypeArguments(), ", "));
118         builder.append(">");
119         return builder.toString();
120     }
121 }
122