All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fitbur.fasterxml.jackson.jaxrs.json.util.ClassKey Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
package com.fitbur.fasterxml.jackson.jaxrs.json.util;

/**
 * Efficient key class, used instead of using Class.
 * The reason for having a separate key class instead of
 * directly using {@link Class} as key is mostly
 * to allow for redefining hashCode method --
 * for some strange reason, {@link Class} does not
 * redefine {@link Object#hashCode} and thus uses identity
 * hash, which is pretty slow. This makes key access using
 * {@link Class} unnecessarily slow.
 *

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

* NOTE: cut'n pasted from 'databind' package for 2.0, to reduce * tight coupling */ public final class ClassKey implements Comparable { 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() { _class = null; _className = null; _hashCode = 0; } public ClassKey(Class clz) { _class = clz; _className = clz.getName(); _hashCode = _className.hashCode(); } public void reset(Class clz) { _class = clz; _className = clz.getName(); _hashCode = _className.hashCode(); } /* /********************************************************** /* Comparable /********************************************************** */ // Just need to sort by name, ok to collide (unless used in TreeMap/Set!) //@Override public int com.fitburpareTo(ClassKey other) { return _className.com.fitburpareTo(other._className); } /* /********************************************************** /* Standard methods /********************************************************** */ @Override public boolean equals(Object o) { if (o == this) return true; if (o == null) return false; if (o.getClass() != getClass()) return false; ClassKey other = (ClassKey) o; /* Is it possible to have different Class object for same name + class loader com.fitburbo? * 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 == _class; } @Override public int hashCode() { return _hashCode; } @Override public String toString() { return _className; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy