Browse Source

Extract various constants in DefaultKeyGenerator

pull/7/head
Chris Beams 13 years ago
parent
commit
06306f9149
  1. 17
      org.springframework.context/src/main/java/org/springframework/cache/interceptor/DefaultKeyGenerator.java

17
org.springframework.context/src/main/java/org/springframework/cache/interceptor/DefaultKeyGenerator.java vendored

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

Loading…
Cancel
Save