1 package com.fasterxml.jackson.databind.type;
2
3 /**
4  * Key class, used as an efficient and accurate key
5  * for locating per-class values, such as
6  * {@link com.fasterxml.jackson.databind.JsonSerializer}s.
7  *<p>
8  * The reason for having a separate key class instead of
9  * directly using {@link Class} as key is mostly
10  * to allow for redefining <code>hashCode</code> method --
11  * for some strange reason, {@link Class} does not
12  * redefine {@link Object#hashCode} and thus uses identity
13  * hash, which is pretty slow. This makes key access using
14  * {@link Class} unnecessarily slow.
15  *<p>
16  * Note: since class is not strictly immutable, caller must
17  * know what it is doing, if changing field values.
18  */

19 public final class ClassKey
20     implements Comparable<ClassKey>,
21         java.io.Serializable // since 2.1
22 {
23     private static final long serialVersionUID = 1L;
24
25     private String _className;
26
27     private Class<?> _class;
28
29     /**
30      * Let's cache hash code straight away, since we are
31      * almost certain to need it.
32      */

33     private int _hashCode;
34
35     public ClassKey() 
36     {
37         _class = null;
38         _className = null;
39         _hashCode = 0;
40     }
41
42     public ClassKey(Class<?> clz)
43     {
44         _class = clz;
45         _className = clz.getName();
46         _hashCode = _className.hashCode();
47     }
48
49     public void reset(Class<?> clz)
50     {
51         _class = clz;
52         _className = clz.getName();
53         _hashCode = _className.hashCode();
54     }
55
56     /*
57     /**********************************************************
58     /* Comparable
59     /**********************************************************
60      */

61
62     @Override
63     public int compareTo(ClassKey other)
64     {
65         // Just need to sort by name, ok to collide (unless used in TreeMap/Set!)
66         return _className.compareTo(other._className);
67     }
68
69     /*
70     /**********************************************************
71     /* Standard methods
72     /**********************************************************
73      */

74
75     @Override
76         public boolean equals(Object o)
77     {
78         if (o == thisreturn true;
79         if (o == nullreturn false;
80         if (o.getClass() != getClass()) return false;
81         ClassKey other = (ClassKey) o;
82
83         /* Is it possible to have different Class object for same name + class loader combo?
84          * Let's assume answer is no: if this is wrong, will need to uncomment following functionality
85          */

86         /*
87         return (other._className.equals(_className))
88             && (other._class.getClassLoader() == _class.getClassLoader());
89         */

90         return other._class == _class;
91     }
92
93     @Override public int hashCode() { return _hashCode; }
94
95     @Override public String toString() { return _className; }
96     
97 }
98