Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package com.fasterxml.jackson.databind.type;

Key class, used as an efficient and accurate key for locating per-class values, such as com.fasterxml.jackson.databind.JsonSerializers.

The reason for having a separate key class instead of directly using java.lang.Class as key is mostly to allow for redefining hashCode method -- for some strange reason, java.lang.Class does not redefine java.lang.Object.hashCode() and thus uses identity hash, which is pretty slow. This makes key access using java.lang.Class unnecessarily slow.

Note: since class is not strictly immutable, caller must know what it is doing, if changing field values.

public final class ClassKey
    implements Comparable<ClassKey>,
        java.io.Serializable // since 2.1
    private static final long serialVersionUID = 1L;
    private String _className;
    private Class<?> _class;

    
Let's cache hash code straight away, since we are almost certain to need it.
    private int _hashCode;
    public ClassKey() 
    {
         = null;
         = null;
         = 0;
    }
    public ClassKey(Class<?> clz)
    {
         = clz;
         = clz.getName();
         = .hashCode();
    }
    public void reset(Class<?> clz)
    {
         = clz;
         = clz.getName();
         = .hashCode();
    }
    /*
    /**********************************************************
    /* Comparable
    /**********************************************************
     */
//  @Override
    public int compareTo(ClassKey other)
    {
        // Just need to sort by name, ok to collide (unless used in TreeMap/Set!)
        return .compareTo(other._className);
    }
    /*
    /**********************************************************
    /* Standard methods
    /**********************************************************
     */
    @Override
        public boolean equals(Object o)
    {
        if (o == thisreturn true;
        if (o == nullreturn false;
        if (o.getClass() != getClass()) return false;
        ClassKey other = (ClassKeyo;
        /* Is it possible to have different Class object for same name + class loader combo?
         * Let's assume answer is no: if this is wrong, will need to uncomment following functionality
         */
        /*
        return (other._className.equals(_className))
            && (other._class.getClassLoader() == _class.getClassLoader());
        */
        return other._class == ;
    }
    @Override public int hashCode() { return ; }
    @Override public String toString() { return ; }
    
New to GrepCode? Check out our FAQ X