Extract various constants in DefaultKeyGenerator

This commit is contained in:
Chris Beams 2011-11-16 04:20:53 +00:00
parent 83d099db98
commit 06306f9149
1 changed files with 11 additions and 6 deletions

View File

@ -21,25 +21,30 @@ import java.lang.reflect.Method;
import org.springframework.cache.interceptor.KeyGenerator; import org.springframework.cache.interceptor.KeyGenerator;
/** /**
* Default key generator. Returns 0 if no param is given, the param itself if * Default key generator. Returns {@value #NO_PARAM_KEY} if no parameters are provided,
* only one is given or a hash code computed from all given params hash code. * the parameter itself if only one is given or a hash code computed from all given
* Uses a constant (53) for <code>null</code> objects. * parameters' hash code values. Uses the constant value {@value #NULL_PARAM_KEY} for any
* {@code null} parameters given.
* *
* @author Costin Leau * @author Costin Leau
* @author Chris Beams
* @since 3.1 * @since 3.1
*/ */
public class DefaultKeyGenerator implements KeyGenerator { public class DefaultKeyGenerator implements KeyGenerator {
public static final int NO_PARAM_KEY = 0;
public static final int NULL_PARAM_KEY = 53;
public Object extract(Object target, Method method, Object... params) { public Object extract(Object target, Method method, Object... params) {
if (params.length == 1) { if (params.length == 1) {
return (params[0] == null ? 53 : params[0]); return (params[0] == null ? NULL_PARAM_KEY : params[0]);
} }
if (params.length == 0) { if (params.length == 0) {
return 0; return NO_PARAM_KEY;
} }
int hashCode = 17; int hashCode = 17;
for (Object object : params) { for (Object object : params) {
hashCode = 31 * hashCode + (object == null ? 53 : object.hashCode()); hashCode = 31 * hashCode + (object == null ? NULL_PARAM_KEY : object.hashCode());
} }
return Integer.valueOf(hashCode); return Integer.valueOf(hashCode);
} }