1 package com.fasterxml.jackson.databind.util;
2
3 import com.fasterxml.jackson.databind.JavaType;
4
5 /**
6  * Key that offers two "modes"; one with raw class, as used for
7  * cases were raw class type is available (for example, when using
8  * runtime type); and one with full generics-including.
9  */

10 public class TypeKey
11 {
12     protected int _hashCode;
13
14     protected Class<?> _class;
15
16     protected JavaType _type;
17
18     /**
19      * Indicator of whether serializer stored has a type serializer
20      * wrapper around it or not; if not, it is "untyped" serializer;
21      * if it has, it is "typed"
22      */

23     protected boolean _isTyped;
24
25     public TypeKey() { }
26
27     public TypeKey(TypeKey src) {
28         _hashCode = src._hashCode;
29         _class = src._class;
30         _type = src._type;
31         _isTyped = src._isTyped;
32     }
33
34     public TypeKey(Class<?> key, boolean typed) {
35         _class = key;
36         _type = null;
37         _isTyped = typed;
38         _hashCode = typed ? typedHash(key) : untypedHash(key);
39     }
40
41     public TypeKey(JavaType key, boolean typed) {
42         _type = key;
43         _class = null;
44         _isTyped = typed;
45         _hashCode = typed ? typedHash(key) : untypedHash(key);
46     }
47
48     public final static int untypedHash(Class<?> cls) {
49         return cls.getName().hashCode();
50     }
51
52     public final static int typedHash(Class<?> cls) {
53         return cls.getName().hashCode()+1;
54     }
55
56     public final static int untypedHash(JavaType type) {
57         return type.hashCode() - 1;
58     }
59     
60     public final static int typedHash(JavaType type) {
61         return type.hashCode() - 2;
62     }
63     
64     public final void resetTyped(Class<?> cls) {
65         _type = null;
66         _class = cls;
67         _isTyped = true;
68         _hashCode = typedHash(cls);
69     }
70
71     public final void resetUntyped(Class<?> cls) {
72         _type = null;
73         _class = cls;
74         _isTyped = false;
75         _hashCode = untypedHash(cls);
76     }
77     
78     public final void resetTyped(JavaType type) {
79         _type = type;
80         _class = null;
81         _isTyped = true;
82         _hashCode = typedHash(type);
83     }
84
85     public final void resetUntyped(JavaType type) {
86         _type = type;
87         _class = null;
88         _isTyped = false;
89         _hashCode = untypedHash(type);
90     }
91
92     public boolean isTyped() {
93         return _isTyped;
94     }
95
96     public Class<?> getRawType() {
97         return _class;
98     }
99
100     public JavaType getType() {
101         return _type;
102     }
103
104     @Override public final int hashCode() { return _hashCode; }
105
106     @Override public final String toString() {
107         if (_class != null) {
108             return "{class: "+_class.getName()+", typed? "+_isTyped+"}";
109         }
110         return "{type: "+_type+", typed? "+_isTyped+"}";
111     }
112     
113     // note: we assume key is never used for anything other than as map key, so:
114     @Override public final boolean equals(Object o)
115     {
116         if (o == nullreturn false;
117         if (o == thisreturn true;
118         if (o.getClass() != getClass()) {
119             return false;
120         }
121         TypeKey other = (TypeKey) o;
122         if (other._isTyped == _isTyped) {
123             if (_class != null) {
124                 return other._class == _class;
125             }
126             return _type.equals(other._type);
127         }
128         return false;
129     } 
130 }