Cache key classes implement Comparable and consistently provide a toString representation
Issue: SPR-14017
This commit is contained in:
@@ -101,6 +101,7 @@ public class SpringCacheAnnotationParser implements CacheAnnotationParser, Seria
|
||||
CacheableOperation parseCacheableAnnotation(AnnotatedElement ae, DefaultCacheConfig defaultConfig, Cacheable cacheable) {
|
||||
CacheableOperation.Builder builder = new CacheableOperation.Builder();
|
||||
|
||||
builder.setName(ae.toString());
|
||||
builder.setCacheNames(cacheable.cacheNames());
|
||||
builder.setCondition(cacheable.condition());
|
||||
builder.setUnless(cacheable.unless());
|
||||
@@ -109,7 +110,6 @@ public class SpringCacheAnnotationParser implements CacheAnnotationParser, Seria
|
||||
builder.setCacheManager(cacheable.cacheManager());
|
||||
builder.setCacheResolver(cacheable.cacheResolver());
|
||||
builder.setSync(cacheable.sync());
|
||||
builder.setName(ae.toString());
|
||||
|
||||
defaultConfig.applyDefault(builder);
|
||||
CacheableOperation op = builder.build();
|
||||
@@ -121,6 +121,7 @@ public class SpringCacheAnnotationParser implements CacheAnnotationParser, Seria
|
||||
CacheEvictOperation parseEvictAnnotation(AnnotatedElement ae, DefaultCacheConfig defaultConfig, CacheEvict cacheEvict) {
|
||||
CacheEvictOperation.Builder builder = new CacheEvictOperation.Builder();
|
||||
|
||||
builder.setName(ae.toString());
|
||||
builder.setCacheNames(cacheEvict.cacheNames());
|
||||
builder.setCondition(cacheEvict.condition());
|
||||
builder.setKey(cacheEvict.key());
|
||||
@@ -129,7 +130,6 @@ public class SpringCacheAnnotationParser implements CacheAnnotationParser, Seria
|
||||
builder.setCacheResolver(cacheEvict.cacheResolver());
|
||||
builder.setCacheWide(cacheEvict.allEntries());
|
||||
builder.setBeforeInvocation(cacheEvict.beforeInvocation());
|
||||
builder.setName(ae.toString());
|
||||
|
||||
defaultConfig.applyDefault(builder);
|
||||
CacheEvictOperation op = builder.build();
|
||||
@@ -141,6 +141,7 @@ public class SpringCacheAnnotationParser implements CacheAnnotationParser, Seria
|
||||
CacheOperation parsePutAnnotation(AnnotatedElement ae, DefaultCacheConfig defaultConfig, CachePut cachePut) {
|
||||
CachePutOperation.Builder builder = new CachePutOperation.Builder();
|
||||
|
||||
builder.setName(ae.toString());
|
||||
builder.setCacheNames(cachePut.cacheNames());
|
||||
builder.setCondition(cachePut.condition());
|
||||
builder.setUnless(cachePut.unless());
|
||||
@@ -148,7 +149,6 @@ public class SpringCacheAnnotationParser implements CacheAnnotationParser, Seria
|
||||
builder.setKeyGenerator(cachePut.keyGenerator());
|
||||
builder.setCacheManager(cachePut.cacheManager());
|
||||
builder.setCacheResolver(cachePut.cacheResolver());
|
||||
builder.setName(ae.toString());
|
||||
|
||||
defaultConfig.applyDefault(builder);
|
||||
CachePutOperation op = builder.build();
|
||||
|
||||
@@ -746,7 +746,7 @@ public abstract class CacheAspectSupport extends AbstractCacheInvoker
|
||||
}
|
||||
|
||||
|
||||
private static class CacheOperationCacheKey {
|
||||
private static final class CacheOperationCacheKey implements Comparable<CacheOperationCacheKey> {
|
||||
|
||||
private final CacheOperation cacheOperation;
|
||||
|
||||
@@ -774,6 +774,20 @@ public abstract class CacheAspectSupport extends AbstractCacheInvoker
|
||||
public int hashCode() {
|
||||
return (this.cacheOperation.hashCode() * 31 + this.methodCacheKey.hashCode());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.cacheOperation + " on " + this.methodCacheKey;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(CacheOperationCacheKey other) {
|
||||
int result = this.cacheOperation.getName().compareTo(other.cacheOperation.getName());
|
||||
if (result == 0) {
|
||||
result = this.methodCacheKey.compareTo(other.methodCacheKey);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -29,6 +29,13 @@ public class CacheEvictOperation extends CacheOperation {
|
||||
|
||||
private final boolean beforeInvocation;
|
||||
|
||||
|
||||
public CacheEvictOperation(CacheEvictOperation.Builder b) {
|
||||
super(b);
|
||||
this.cacheWide = b.cacheWide;
|
||||
this.beforeInvocation = b.beforeInvocation;
|
||||
}
|
||||
|
||||
public boolean isCacheWide() {
|
||||
return this.cacheWide;
|
||||
}
|
||||
@@ -37,12 +44,10 @@ public class CacheEvictOperation extends CacheOperation {
|
||||
return this.beforeInvocation;
|
||||
}
|
||||
|
||||
public CacheEvictOperation(CacheEvictOperation.Builder b) {
|
||||
super(b);
|
||||
this.cacheWide = b.cacheWide;
|
||||
this.beforeInvocation = b.beforeInvocation;
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 4.3
|
||||
*/
|
||||
public static class Builder extends CacheOperation.Builder {
|
||||
|
||||
private boolean cacheWide = false;
|
||||
@@ -71,4 +76,5 @@ public class CacheEvictOperation extends CacheOperation {
|
||||
return new CacheEvictOperation(this);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -48,6 +48,7 @@ public abstract class CacheOperation implements BasicOperation {
|
||||
|
||||
private final String toString;
|
||||
|
||||
|
||||
protected CacheOperation(Builder b) {
|
||||
this.name = b.name;
|
||||
this.cacheNames = b.cacheNames;
|
||||
@@ -59,11 +60,11 @@ public abstract class CacheOperation implements BasicOperation {
|
||||
this.toString = b.getOperationDescription().toString();
|
||||
}
|
||||
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Set<String> getCacheNames() {
|
||||
return this.cacheNames;
|
||||
@@ -96,7 +97,6 @@ public abstract class CacheOperation implements BasicOperation {
|
||||
|
||||
/**
|
||||
* This implementation compares the {@code toString()} results.
|
||||
*
|
||||
* @see #toString()
|
||||
*/
|
||||
@Override
|
||||
@@ -106,7 +106,6 @@ public abstract class CacheOperation implements BasicOperation {
|
||||
|
||||
/**
|
||||
* This implementation returns {@code toString()}'s hash code.
|
||||
*
|
||||
* @see #toString()
|
||||
*/
|
||||
@Override
|
||||
@@ -118,7 +117,6 @@ public abstract class CacheOperation implements BasicOperation {
|
||||
* Return an identifying description for this cache operation.
|
||||
* <p>Returned value is produced by calling {@link Builder#getOperationDescription()}
|
||||
* during object construction. This method is used in {#hashCode} and {#equals}.
|
||||
*
|
||||
* @see Builder#getOperationDescription()
|
||||
*/
|
||||
@Override
|
||||
@@ -126,6 +124,10 @@ public abstract class CacheOperation implements BasicOperation {
|
||||
return this.toString;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @since 4.3
|
||||
*/
|
||||
public abstract static class Builder {
|
||||
|
||||
private String name = "";
|
||||
|
||||
@@ -28,6 +28,7 @@ public class CachePutOperation extends CacheOperation {
|
||||
|
||||
private final String unless;
|
||||
|
||||
|
||||
public CachePutOperation(CachePutOperation.Builder b) {
|
||||
super(b);
|
||||
this.unless = b.unless;
|
||||
@@ -37,6 +38,10 @@ public class CachePutOperation extends CacheOperation {
|
||||
return this.unless;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @since 4.3
|
||||
*/
|
||||
public static class Builder extends CacheOperation.Builder {
|
||||
|
||||
private String unless;
|
||||
@@ -58,4 +63,5 @@ public class CachePutOperation extends CacheOperation {
|
||||
return new CachePutOperation(this);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -30,6 +30,7 @@ public class CacheableOperation extends CacheOperation {
|
||||
|
||||
private boolean sync;
|
||||
|
||||
|
||||
public CacheableOperation(CacheableOperation.Builder b) {
|
||||
super(b);
|
||||
this.unless = b.unless;
|
||||
@@ -76,4 +77,5 @@ public class CacheableOperation extends CacheOperation {
|
||||
return new CacheableOperation(this);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -286,7 +286,7 @@ public abstract class AbstractApplicationEventMulticaster
|
||||
/**
|
||||
* Cache key for ListenerRetrievers, based on event type and source type.
|
||||
*/
|
||||
private static class ListenerCacheKey {
|
||||
private static final class ListenerCacheKey implements Comparable<ListenerCacheKey> {
|
||||
|
||||
private final ResolvableType eventType;
|
||||
|
||||
@@ -311,6 +311,23 @@ public abstract class AbstractApplicationEventMulticaster
|
||||
public int hashCode() {
|
||||
return (ObjectUtils.nullSafeHashCode(this.eventType) * 29 + ObjectUtils.nullSafeHashCode(this.sourceType));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ListenerCacheKey [eventType = " + this.eventType + ", sourceType = " + this.sourceType.getName() + "]";
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(ListenerCacheKey other) {
|
||||
int result = 0;
|
||||
if (this.eventType != null) {
|
||||
result = this.eventType.toString().compareTo(other.eventType.toString());
|
||||
}
|
||||
if (result == 0 && this.sourceType != null) {
|
||||
result = this.sourceType.getName().compareTo(other.sourceType.getName());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ import org.springframework.util.ObjectUtils;
|
||||
* @since 4.2
|
||||
* @see CachedExpressionEvaluator
|
||||
*/
|
||||
public final class AnnotatedElementKey {
|
||||
public final class AnnotatedElementKey implements Comparable<AnnotatedElementKey> {
|
||||
|
||||
private final AnnotatedElement element;
|
||||
|
||||
@@ -66,4 +66,18 @@ public final class AnnotatedElementKey {
|
||||
return this.element.hashCode() + (this.targetClass != null ? this.targetClass.hashCode() * 29 : 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.element + (this.targetClass != null ? " on " + this.targetClass : "");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(AnnotatedElementKey other) {
|
||||
int result = this.element.toString().compareTo(other.element.toString());
|
||||
if (result == 0 && this.targetClass != null) {
|
||||
result = this.targetClass.getName().compareTo(other.targetClass.getName());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -63,6 +63,7 @@ import org.springframework.jmx.support.JmxUtils;
|
||||
import org.springframework.jmx.support.ObjectNameManager;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* {@link org.aopalliance.intercept.MethodInterceptor} that routes calls to an
|
||||
@@ -609,7 +610,7 @@ public class MBeanClientInterceptor
|
||||
* Simple wrapper class around a method name and its signature.
|
||||
* Used as the key when caching methods.
|
||||
*/
|
||||
private static class MethodCacheKey {
|
||||
private static final class MethodCacheKey implements Comparable<MethodCacheKey> {
|
||||
|
||||
private final String name;
|
||||
|
||||
@@ -628,7 +629,7 @@ public class MBeanClientInterceptor
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
if (other == this) {
|
||||
if (this == other) {
|
||||
return true;
|
||||
}
|
||||
MethodCacheKey otherKey = (MethodCacheKey) other;
|
||||
@@ -639,6 +640,32 @@ public class MBeanClientInterceptor
|
||||
public int hashCode() {
|
||||
return this.name.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.name + "(" + StringUtils.arrayToCommaDelimitedString(this.parameterTypes) + ")";
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(MethodCacheKey other) {
|
||||
int result = this.name.compareTo(other.name);
|
||||
if (result != 0) {
|
||||
return result;
|
||||
}
|
||||
if (this.parameterTypes.length < other.parameterTypes.length) {
|
||||
return -1;
|
||||
}
|
||||
if (this.parameterTypes.length > other.parameterTypes.length) {
|
||||
return 1;
|
||||
}
|
||||
for (int i = 0; i < this.parameterTypes.length; i++) {
|
||||
result = this.parameterTypes[i].getName().compareTo(other.parameterTypes[i].getName());
|
||||
if (result != 0) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user