1 package com.fasterxml.classmate.util;
2
3 import com.fasterxml.classmate.ResolvedType;
4
5 /**
6  * Key used for entries cached in a {@link ResolvedTypeCache}.
7  */

8 public class ResolvedTypeKey
9 {
10     private final Class<?> _erasedType;
11     private final ResolvedType[] _typeParameters;
12     private final int _hashCode;
13
14     public ResolvedTypeKey(Class<?> simpleType) {
15         this(simpleType, null);
16     }
17     
18     public ResolvedTypeKey(Class<?> erasedType, ResolvedType[] tp)
19     {
20         // let's not hold on type empty arrays
21         if (tp != null && tp.length == 0) {
22             tp = null;
23         }
24         _erasedType = erasedType;
25         _typeParameters = tp;
26         int h = erasedType.getName().hashCode();
27         if (tp != null) {
28             h += tp.length;
29         }
30         _hashCode = h;
31     }
32
33     @Override
34     public String toString() {
35         StringBuilder sb = new StringBuilder();
36         sb.append("[CacheKey: ");
37         sb.append(_erasedType.getName())
38             .append('(');
39         if (_typeParameters != null) {
40             for (int i = 0; i < _typeParameters.length; ++i) {
41                 if (i > 0) {
42                     sb.append(',');
43                 }
44                 sb.append(_typeParameters[i]);
45             }
46         }
47         sb.append(")]");
48         return sb.toString();
49     }
50     
51     @Override
52     public int hashCode() { return _hashCode; }
53
54     @Override
55     public boolean equals(Object o)
56     {
57         if (o == thisreturn true;
58         if (o == null || o.getClass() != getClass()) return false;
59         ResolvedTypeKey other = (ResolvedTypeKey) o;
60         if (other._erasedType != _erasedType) return false;
61         ResolvedType[] otherTP = other._typeParameters;
62         if (_typeParameters == null) {
63             return (otherTP == null);
64         }
65         if (otherTP == null || otherTP.length != _typeParameters.length) {
66             return false;
67         }
68         for (int i = 0, len = _typeParameters.length; i < len; ++i) {
69             if (!_typeParameters[i].equals(otherTP[i])) {
70                 return false;
71             }
72         }
73         return true;
74     }
75 }