public abstract class KeyFactory
extends java.lang.Object
equals
and hashCode
methods follow the
the rules laid out in Effective Java by Joshua Bloch.
To generate a KeyFactory
, you need to supply an interface which
describes the structure of the key. The interface should have a
single method named newInstance
, which returns an
Object
. The arguments array can be
anything--Objects, primitive values, or single or
multi-dimension arrays of either. For example:
private interface IntStringKey { public Object newInstance(int i, String s); }
Once you have made a KeyFactory
, you generate a new key by calling
the newInstance
method defined by your interface.
IntStringKey factory = (IntStringKey)KeyFactory.create(IntStringKey.class); Object key1 = factory.newInstance(4, "Hello"); Object key2 = factory.newInstance(4, "World");
Note:
hashCode
equality between two keys key1
and key2
is only guaranteed if
key1.equals(key2)
and the keys were produced by the same factory.
Modifier and Type | Class and Description |
---|---|
static class |
KeyFactory.Generator |
Modifier and Type | Field and Description |
---|---|
private static Signature |
APPEND_STRING |
static Customizer |
CLASS_BY_NAME |
private static Signature |
EQUALS |
private static Signature |
GET_CLASS |
private static Signature |
GET_NAME |
private static Signature |
GET_SORT |
static HashCodeCustomizer |
HASH_ASM_TYPE
Type.hashCode() is very expensive as it traverses full descriptor to calculate hash code. |
private static Signature |
HASH_CODE |
private static Type |
KEY_FACTORY |
static Customizer |
OBJECT_BY_CLASS
Deprecated.
this customizer might result in unexpected class leak since key object still holds a strong reference to the Object and class.
It is recommended to have pre-processing method that would strip Objects and represent Classes as Strings
|
private static int[] |
PRIMES |
static FieldTypeCustomizer |
STORE_CLASS_AS_STRING |
private static Signature |
TO_STRING |
Modifier | Constructor and Description |
---|---|
protected |
KeyFactory() |
Modifier and Type | Method and Description |
---|---|
static KeyFactory |
create(java.lang.Class keyInterface) |
static KeyFactory |
create(java.lang.Class keyInterface,
Customizer customizer) |
static KeyFactory |
create(java.lang.Class keyInterface,
KeyFactoryCustomizer first,
java.util.List<KeyFactoryCustomizer> next) |
static KeyFactory |
create(java.lang.ClassLoader loader,
java.lang.Class keyInterface,
Customizer customizer) |
static KeyFactory |
create(java.lang.ClassLoader loader,
java.lang.Class keyInterface,
KeyFactoryCustomizer customizer,
java.util.List<KeyFactoryCustomizer> next) |
private static final Signature GET_NAME
private static final Signature GET_CLASS
private static final Signature HASH_CODE
private static final Signature EQUALS
private static final Signature TO_STRING
private static final Signature APPEND_STRING
private static final Type KEY_FACTORY
private static final Signature GET_SORT
private static final int[] PRIMES
public static final Customizer CLASS_BY_NAME
public static final FieldTypeCustomizer STORE_CLASS_AS_STRING
public static final HashCodeCustomizer HASH_ASM_TYPE
Type.hashCode()
is very expensive as it traverses full descriptor to calculate hash code.
This customizer uses Type.getSort()
as a hash code.@Deprecated public static final Customizer OBJECT_BY_CLASS
public static KeyFactory create(java.lang.Class keyInterface)
public static KeyFactory create(java.lang.Class keyInterface, Customizer customizer)
public static KeyFactory create(java.lang.Class keyInterface, KeyFactoryCustomizer first, java.util.List<KeyFactoryCustomizer> next)
public static KeyFactory create(java.lang.ClassLoader loader, java.lang.Class keyInterface, Customizer customizer)
public static KeyFactory create(java.lang.ClassLoader loader, java.lang.Class keyInterface, KeyFactoryCustomizer customizer, java.util.List<KeyFactoryCustomizer> next)