com.googlecode.ehcache.annotations.key
Class AbstractDeepCacheKeyGenerator<G,T extends java.io.Serializable>

java.lang.Object
  extended by com.googlecode.ehcache.annotations.key.AbstractCacheKeyGenerator<T>
      extended by com.googlecode.ehcache.annotations.key.AbstractDeepCacheKeyGenerator<G,T>
All Implemented Interfaces:
CacheKeyGenerator<T>, ReflectionHelperAware
Direct Known Subclasses:
AbstractHashingCacheKeyGenerator, ListCacheKeyGenerator, StringCacheKeyGenerator

public abstract class AbstractDeepCacheKeyGenerator<G,T extends java.io.Serializable>
extends AbstractCacheKeyGenerator<T>
implements ReflectionHelperAware

Base class for key generators that do deep inspection of the key data for generation. Arrays, Iterable and Map are iterated over and their values recursively inspected. Also supports reflective recursion which can be useful for objects that may not support the hashCode, equals or other methods required by the key generation implementation. Reflective recursion add significant overhead to the deep inspection process.

Version:
$Revision: 656 $
Author:
Eric Dalquist

Constructor Summary
AbstractDeepCacheKeyGenerator()
           
AbstractDeepCacheKeyGenerator(boolean includeMethod, boolean includeParameterTypes)
           
 
Method Summary
protected  void append(G generator, boolean[] a)
          Append a boolean array.
protected  void append(G generator, byte[] a)
          Append a byte array.
protected  void append(G generator, char[] a)
          Append a char array.
protected  void append(G generator, double[] a)
          Append a double array.
protected  void append(G generator, float[] a)
          Append a float array.
protected  void append(G generator, int[] a)
          Append a int array.
protected  void append(G generator, long[] a)
          Append a long array.
protected abstract  void append(G generator, java.lang.Object e)
          Append an object to the key.
protected  void append(G generator, short[] a)
          Append a short array.
protected abstract  void appendGraphCycle(G generator, java.lang.Object o)
          Called if a graph cycle is detected.
protected abstract  void appendNull(G generator)
          Called if a null value is found in the object graph
protected  void beginRecursion(G generator, java.lang.Object e)
          Called before each array/Iterable/Map is handled.
protected  void deepHashCode(G generator, java.lang.Iterable<?> a)
          Calls deepHashCode(Object, Object) on each element in the Iterable
protected  void deepHashCode(G generator, java.util.Map.Entry<?,?> e)
          Calls deepHashCode(Object, Object) on both the key and the value.
protected  void deepHashCode(G generator, java.lang.Object element)
          Does instanceof checks to determine the correct deepHashCode(G, java.lang.Object[]) method to call or append(Object, Object) is called if no other recursion is needed or reflectionDeepHashCode(Object, Object) is called if setUseReflection(boolean) is true.
protected  void deepHashCode(G generator, java.lang.Object[] a)
          Calls deepHashCode(Object, Object) on each element in the array.
protected  void endRecursion(G generator, java.lang.Object e)
          Called after each array/Iterable/Map is handled.
protected abstract  T generateKey(G generator)
          Generate the cache key from the generator
 T generateKey(java.lang.Object... data)
          Called to generate the key.
protected abstract  G getGenerator(java.lang.Object... data)
          Create the object used to generate the key.
 ReflectionHelper getReflectionHelper()
           
 boolean isUseReflection()
           
protected  void reflectionDeepHashCode(G generator, java.lang.Object element)
          Calls shouldReflect(Object) to determine if the object needs to be reflected on to generate a good key.
 void setReflectionHelper(ReflectionHelper reflectionHelper)
           
 void setUseReflection(boolean useReflection)
          Determines if reflection should be used on each object that is used in key generation.
protected  boolean shouldReflect(java.lang.Object element)
          Default implementation returns true if the Object doesn't implement hashCode or doesn't implement equals.
 java.lang.String toString()
           
 
Methods inherited from class com.googlecode.ehcache.annotations.key.AbstractCacheKeyGenerator
generateKey, isCheckforCycles, isIncludeMethod, isIncludeParameterTypes, register, setCheckforCycles, setIncludeMethod, setIncludeParameterTypes, unregister
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

AbstractDeepCacheKeyGenerator

public AbstractDeepCacheKeyGenerator()

AbstractDeepCacheKeyGenerator

public AbstractDeepCacheKeyGenerator(boolean includeMethod,
                                     boolean includeParameterTypes)
Method Detail

getReflectionHelper

public ReflectionHelper getReflectionHelper()

setReflectionHelper

public void setReflectionHelper(ReflectionHelper reflectionHelper)
Specified by:
setReflectionHelper in interface ReflectionHelperAware

isUseReflection

public final boolean isUseReflection()

setUseReflection

public final void setUseReflection(boolean useReflection)
Determines if reflection should be used on each object that is used in key generation. If true each object that is added to the key has shouldReflect(Object) called on it. If that returns true reflection is used to recurse on all of the fields of the object.

Parameters:
useReflection - Defaults to false.

generateKey

public final T generateKey(java.lang.Object... data)
Description copied from class: AbstractCacheKeyGenerator
Called to generate the key. Depending on the AbstractCacheKeyGenerator.setIncludeMethod(boolean) and AbstractCacheKeyGenerator.setIncludeParameterTypes(boolean) the the appropriate parts of the Method signature will be included in the data array.

Specified by:
generateKey in interface CacheKeyGenerator<T extends java.io.Serializable>
Specified by:
generateKey in class AbstractCacheKeyGenerator<T extends java.io.Serializable>
Parameters:
data - the objects to use when generating the key
Returns:
The Serializable cache key for the method invocation.

deepHashCode

protected void deepHashCode(G generator,
                            java.lang.Object[] a)
Calls deepHashCode(Object, Object) on each element in the array.

Parameters:
a - will never be null

deepHashCode

protected void deepHashCode(G generator,
                            java.lang.Iterable<?> a)
Calls deepHashCode(Object, Object) on each element in the Iterable

Parameters:
a - will never be null

deepHashCode

protected void deepHashCode(G generator,
                            java.util.Map.Entry<?,?> e)
Calls deepHashCode(Object, Object) on both the key and the value.

Parameters:
e - will never be null

deepHashCode

protected final void deepHashCode(G generator,
                                  java.lang.Object element)
Does instanceof checks to determine the correct deepHashCode(G, java.lang.Object[]) method to call or append(Object, Object) is called if no other recursion is needed or reflectionDeepHashCode(Object, Object) is called if setUseReflection(boolean) is true.


reflectionDeepHashCode

protected final void reflectionDeepHashCode(G generator,
                                            java.lang.Object element)
Calls shouldReflect(Object) to determine if the object needs to be reflected on to generate a good key. If so AccessibleObject.setAccessible(AccessibleObject[], boolean) is used to enable access to private, protected and default fields. Each non-transient, non-static field has deepHashCode(Object, Object) called on it.


shouldReflect

protected boolean shouldReflect(java.lang.Object element)
Default implementation returns true if the Object doesn't implement hashCode or doesn't implement equals.

Parameters:
element - will never be null

getGenerator

protected abstract G getGenerator(java.lang.Object... data)
Create the object used to generate the key. This object is passed into every deepHashCode(G, java.lang.Object[]) and append(G, java.lang.Object) call.


generateKey

protected abstract T generateKey(G generator)
Generate the cache key from the generator


append

protected abstract void append(G generator,
                               java.lang.Object e)
Append an object to the key.


appendGraphCycle

protected abstract void appendGraphCycle(G generator,
                                         java.lang.Object o)
Called if a graph cycle is detected.

Parameters:
o - The object that started the cycle

appendNull

protected abstract void appendNull(G generator)
Called if a null value is found in the object graph


append

protected void append(G generator,
                      boolean[] a)
Append a boolean array. Calls append(Object, Object) on each value in the array. If the implementing class can handle boolean or boolean array primitives directly this should be overridden to avoid auto-boxing each array element.


append

protected void append(G generator,
                      byte[] a)
Append a byte array. Calls append(Object, Object) on each value in the array. If the implementing class can handle byte or byte array primitives directly this should be overridden to avoid auto-boxing each array element.


append

protected void append(G generator,
                      char[] a)
Append a char array. Calls append(Object, Object) on each value in the array. If the implementing class can handle char or char array primitives directly this should be overridden to avoid auto-boxing each array element.


append

protected void append(G generator,
                      double[] a)
Append a double array. Calls append(Object, Object) on each value in the array. If the implementing class can handle double or double array primitives directly this should be overridden to avoid auto-boxing each array element.


append

protected void append(G generator,
                      float[] a)
Append a float array. Calls append(Object, Object) on each value in the array. If the implementing class can handle float or float array primitives directly this should be overridden to avoid auto-boxing each array element.


append

protected void append(G generator,
                      int[] a)
Append a int array. Calls append(Object, Object) on each value in the array. If the implementing class can handle int or int array primitives directly this should be overridden to avoid auto-boxing each array element.


append

protected void append(G generator,
                      long[] a)
Append a long array. Calls append(Object, Object) on each value in the array. If the implementing class can handle long or long array primitives directly this should be overridden to avoid auto-boxing each array element.


append

protected void append(G generator,
                      short[] a)
Append a short array. Calls append(Object, Object) on each value in the array. If the implementing class can handle short or short array primitives directly this should be overridden to avoid auto-boxing each array element.


beginRecursion

protected void beginRecursion(G generator,
                              java.lang.Object e)
Called before each array/Iterable/Map is handled. Useful for sub-classes that want to be aware of the tree structure of the object graph.


endRecursion

protected void endRecursion(G generator,
                            java.lang.Object e)
Called after each array/Iterable/Map is handled. Useful for sub-classes that want to be aware of the tree structure of the object graph.


toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object


Copyright © 2011. All Rights Reserved.