1 package com.fasterxml.classmate.util;
2
3 import java.io.Serializable;
4
5 /**
6  * Helper class used as key when we need efficient Class-to-value lookups.
7  */

8 @SuppressWarnings("serial")
9 public class ClassKey
10     implements Comparable<ClassKey>, Serializable
11 {
12     private final String _className;
13
14     private final Class<?> _class;
15
16     /**
17      * Let's cache hash code straight away, since we are almost certain to need it.
18      */

19     private final int _hashCode;
20
21     public ClassKey(Class<?> clz)
22     {
23         _class = clz;
24         _className = clz.getName();
25         _hashCode = _className.hashCode();
26     }
27
28     /*
29     /**********************************************************************
30     /* Comparable
31     /**********************************************************************
32      */

33
34     @Override
35     public int compareTo(ClassKey other)
36     {
37         // Just need to sort by name, ok to collide (unless used in TreeMap/Set!)
38         return _className.compareTo(other._className);
39     }
40
41     /*
42     /**********************************************************************
43     /* Standard methods
44     /**********************************************************************
45      */

46
47     @Override
48     public boolean equals(Object o)
49     {
50         if (o == thisreturn true;
51         if (o == nullreturn false;
52         if (o.getClass() != getClass()) return false;
53         ClassKey other = (ClassKey) o;
54
55         /* Is it possible to have different Class object for same name + class loader combo?
56          * Let's assume answer is no: if this is wrong, will need to uncomment following functionality
57          */

58         /*
59         return (other._className.equals(_className))
60             && (other._class.getClassLoader() == _class.getClassLoader());
61         */

62         return other._class == _class;
63     }
64
65     @Override public int hashCode() { return _hashCode; }
66
67     @Override public String toString() { return _className; }
68     
69 }
70