Skip to content

Commit 54aeb7a

Browse files
committed
Cache key classes implement Comparable and consistently provide a toString representation
Issue: SPR-14017
1 parent a8b5ea1 commit 54aeb7a

File tree

15 files changed

+222
-54
lines changed

15 files changed

+222
-54
lines changed

spring-aop/src/main/java/org/springframework/aop/framework/AdvisedSupport.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -586,7 +586,7 @@ public String toString() {
586586
* Simple wrapper class around a Method. Used as the key when
587587
* caching methods, for efficient equals and hashCode comparisons.
588588
*/
589-
private static class MethodCacheKey {
589+
private static final class MethodCacheKey implements Comparable<MethodCacheKey> {
590590

591591
private final Method method;
592592

@@ -599,17 +599,28 @@ public MethodCacheKey(Method method) {
599599

600600
@Override
601601
public boolean equals(Object other) {
602-
if (other == this) {
603-
return true;
604-
}
605-
MethodCacheKey otherKey = (MethodCacheKey) other;
606-
return (this.method == otherKey.method);
602+
return (this == other || (other instanceof MethodCacheKey &&
603+
this.method == ((MethodCacheKey) other).method));
607604
}
608605

609606
@Override
610607
public int hashCode() {
611608
return this.hashCode;
612609
}
610+
611+
@Override
612+
public String toString() {
613+
return this.method.toString();
614+
}
615+
616+
@Override
617+
public int compareTo(MethodCacheKey other) {
618+
int result = this.method.getName().compareTo(other.method.getName());
619+
if (result == 0) {
620+
result = this.method.toString().compareTo(other.method.toString());
621+
}
622+
return result;
623+
}
613624
}
614625

615626
}

spring-context/src/main/java/org/springframework/cache/annotation/SpringCacheAnnotationParser.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ private <T extends Annotation> Collection<CacheOperation> lazyInit(Collection<Ca
101101
CacheableOperation parseCacheableAnnotation(AnnotatedElement ae, DefaultCacheConfig defaultConfig, Cacheable cacheable) {
102102
CacheableOperation.Builder builder = new CacheableOperation.Builder();
103103

104+
builder.setName(ae.toString());
104105
builder.setCacheNames(cacheable.cacheNames());
105106
builder.setCondition(cacheable.condition());
106107
builder.setUnless(cacheable.unless());
@@ -109,7 +110,6 @@ CacheableOperation parseCacheableAnnotation(AnnotatedElement ae, DefaultCacheCon
109110
builder.setCacheManager(cacheable.cacheManager());
110111
builder.setCacheResolver(cacheable.cacheResolver());
111112
builder.setSync(cacheable.sync());
112-
builder.setName(ae.toString());
113113

114114
defaultConfig.applyDefault(builder);
115115
CacheableOperation op = builder.build();
@@ -121,6 +121,7 @@ CacheableOperation parseCacheableAnnotation(AnnotatedElement ae, DefaultCacheCon
121121
CacheEvictOperation parseEvictAnnotation(AnnotatedElement ae, DefaultCacheConfig defaultConfig, CacheEvict cacheEvict) {
122122
CacheEvictOperation.Builder builder = new CacheEvictOperation.Builder();
123123

124+
builder.setName(ae.toString());
124125
builder.setCacheNames(cacheEvict.cacheNames());
125126
builder.setCondition(cacheEvict.condition());
126127
builder.setKey(cacheEvict.key());
@@ -129,7 +130,6 @@ CacheEvictOperation parseEvictAnnotation(AnnotatedElement ae, DefaultCacheConfig
129130
builder.setCacheResolver(cacheEvict.cacheResolver());
130131
builder.setCacheWide(cacheEvict.allEntries());
131132
builder.setBeforeInvocation(cacheEvict.beforeInvocation());
132-
builder.setName(ae.toString());
133133

134134
defaultConfig.applyDefault(builder);
135135
CacheEvictOperation op = builder.build();
@@ -141,14 +141,14 @@ CacheEvictOperation parseEvictAnnotation(AnnotatedElement ae, DefaultCacheConfig
141141
CacheOperation parsePutAnnotation(AnnotatedElement ae, DefaultCacheConfig defaultConfig, CachePut cachePut) {
142142
CachePutOperation.Builder builder = new CachePutOperation.Builder();
143143

144+
builder.setName(ae.toString());
144145
builder.setCacheNames(cachePut.cacheNames());
145146
builder.setCondition(cachePut.condition());
146147
builder.setUnless(cachePut.unless());
147148
builder.setKey(cachePut.key());
148149
builder.setKeyGenerator(cachePut.keyGenerator());
149150
builder.setCacheManager(cachePut.cacheManager());
150151
builder.setCacheResolver(cachePut.cacheResolver());
151-
builder.setName(ae.toString());
152152

153153
defaultConfig.applyDefault(builder);
154154
CachePutOperation op = builder.build();

spring-context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ public void apply(Object result) {
746746
}
747747

748748

749-
private static class CacheOperationCacheKey {
749+
private static final class CacheOperationCacheKey implements Comparable<CacheOperationCacheKey> {
750750

751751
private final CacheOperation cacheOperation;
752752

@@ -774,6 +774,20 @@ public boolean equals(Object other) {
774774
public int hashCode() {
775775
return (this.cacheOperation.hashCode() * 31 + this.methodCacheKey.hashCode());
776776
}
777+
778+
@Override
779+
public String toString() {
780+
return this.cacheOperation + " on " + this.methodCacheKey;
781+
}
782+
783+
@Override
784+
public int compareTo(CacheOperationCacheKey other) {
785+
int result = this.cacheOperation.getName().compareTo(other.cacheOperation.getName());
786+
if (result == 0) {
787+
result = this.methodCacheKey.compareTo(other.methodCacheKey);
788+
}
789+
return result;
790+
}
777791
}
778792

779793
}

spring-context/src/main/java/org/springframework/cache/interceptor/CacheEvictOperation.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ public class CacheEvictOperation extends CacheOperation {
2929

3030
private final boolean beforeInvocation;
3131

32+
33+
public CacheEvictOperation(CacheEvictOperation.Builder b) {
34+
super(b);
35+
this.cacheWide = b.cacheWide;
36+
this.beforeInvocation = b.beforeInvocation;
37+
}
38+
3239
public boolean isCacheWide() {
3340
return this.cacheWide;
3441
}
@@ -37,12 +44,10 @@ public boolean isBeforeInvocation() {
3744
return this.beforeInvocation;
3845
}
3946

40-
public CacheEvictOperation(CacheEvictOperation.Builder b) {
41-
super(b);
42-
this.cacheWide = b.cacheWide;
43-
this.beforeInvocation = b.beforeInvocation;
44-
}
4547

48+
/**
49+
* @since 4.3
50+
*/
4651
public static class Builder extends CacheOperation.Builder {
4752

4853
private boolean cacheWide = false;
@@ -71,4 +76,5 @@ public CacheEvictOperation build() {
7176
return new CacheEvictOperation(this);
7277
}
7378
}
79+
7480
}

spring-context/src/main/java/org/springframework/cache/interceptor/CacheOperation.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public abstract class CacheOperation implements BasicOperation {
4848

4949
private final String toString;
5050

51+
5152
protected CacheOperation(Builder b) {
5253
this.name = b.name;
5354
this.cacheNames = b.cacheNames;
@@ -59,11 +60,11 @@ protected CacheOperation(Builder b) {
5960
this.toString = b.getOperationDescription().toString();
6061
}
6162

63+
6264
public String getName() {
6365
return this.name;
6466
}
6567

66-
6768
@Override
6869
public Set<String> getCacheNames() {
6970
return this.cacheNames;
@@ -96,7 +97,6 @@ public String getCondition() {
9697

9798
/**
9899
* This implementation compares the {@code toString()} results.
99-
*
100100
* @see #toString()
101101
*/
102102
@Override
@@ -106,7 +106,6 @@ public boolean equals(Object other) {
106106

107107
/**
108108
* This implementation returns {@code toString()}'s hash code.
109-
*
110109
* @see #toString()
111110
*/
112111
@Override
@@ -118,14 +117,17 @@ public int hashCode() {
118117
* Return an identifying description for this cache operation.
119118
* <p>Returned value is produced by calling {@link Builder#getOperationDescription()}
120119
* during object construction. This method is used in {#hashCode} and {#equals}.
121-
*
122120
* @see Builder#getOperationDescription()
123121
*/
124122
@Override
125123
public final String toString() {
126124
return this.toString;
127125
}
128126

127+
128+
/**
129+
* @since 4.3
130+
*/
129131
public abstract static class Builder {
130132

131133
private String name = "";

spring-context/src/main/java/org/springframework/cache/interceptor/CachePutOperation.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public class CachePutOperation extends CacheOperation {
2828

2929
private final String unless;
3030

31+
3132
public CachePutOperation(CachePutOperation.Builder b) {
3233
super(b);
3334
this.unless = b.unless;
@@ -37,6 +38,10 @@ public String getUnless() {
3738
return this.unless;
3839
}
3940

41+
42+
/**
43+
* @since 4.3
44+
*/
4045
public static class Builder extends CacheOperation.Builder {
4146

4247
private String unless;
@@ -58,4 +63,5 @@ public CachePutOperation build() {
5863
return new CachePutOperation(this);
5964
}
6065
}
66+
6167
}

spring-context/src/main/java/org/springframework/cache/interceptor/CacheableOperation.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class CacheableOperation extends CacheOperation {
3030

3131
private boolean sync;
3232

33+
3334
public CacheableOperation(CacheableOperation.Builder b) {
3435
super(b);
3536
this.unless = b.unless;
@@ -76,4 +77,5 @@ public CacheableOperation build() {
7677
return new CacheableOperation(this);
7778
}
7879
}
80+
7981
}

spring-context/src/main/java/org/springframework/context/event/AbstractApplicationEventMulticaster.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -286,7 +286,7 @@ protected boolean supportsEvent(ApplicationListener<?> listener, ResolvableType
286286
/**
287287
* Cache key for ListenerRetrievers, based on event type and source type.
288288
*/
289-
private static class ListenerCacheKey {
289+
private static final class ListenerCacheKey implements Comparable<ListenerCacheKey> {
290290

291291
private final ResolvableType eventType;
292292

@@ -311,6 +311,23 @@ public boolean equals(Object other) {
311311
public int hashCode() {
312312
return (ObjectUtils.nullSafeHashCode(this.eventType) * 29 + ObjectUtils.nullSafeHashCode(this.sourceType));
313313
}
314+
315+
@Override
316+
public String toString() {
317+
return "ListenerCacheKey [eventType = " + this.eventType + ", sourceType = " + this.sourceType.getName() + "]";
318+
}
319+
320+
@Override
321+
public int compareTo(ListenerCacheKey other) {
322+
int result = 0;
323+
if (this.eventType != null) {
324+
result = this.eventType.toString().compareTo(other.eventType.toString());
325+
}
326+
if (result == 0 && this.sourceType != null) {
327+
result = this.sourceType.getName().compareTo(other.sourceType.getName());
328+
}
329+
return result;
330+
}
314331
}
315332

316333

spring-context/src/main/java/org/springframework/context/expression/AnnotatedElementKey.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
* @since 4.2
3131
* @see CachedExpressionEvaluator
3232
*/
33-
public final class AnnotatedElementKey {
33+
public final class AnnotatedElementKey implements Comparable<AnnotatedElementKey> {
3434

3535
private final AnnotatedElement element;
3636

@@ -66,4 +66,18 @@ public int hashCode() {
6666
return this.element.hashCode() + (this.targetClass != null ? this.targetClass.hashCode() * 29 : 0);
6767
}
6868

69+
@Override
70+
public String toString() {
71+
return this.element + (this.targetClass != null ? " on " + this.targetClass : "");
72+
}
73+
74+
@Override
75+
public int compareTo(AnnotatedElementKey other) {
76+
int result = this.element.toString().compareTo(other.element.toString());
77+
if (result == 0 && this.targetClass != null) {
78+
result = this.targetClass.getName().compareTo(other.targetClass.getName());
79+
}
80+
return result;
81+
}
82+
6983
}

spring-context/src/main/java/org/springframework/jmx/access/MBeanClientInterceptor.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -63,6 +63,7 @@
6363
import org.springframework.jmx.support.ObjectNameManager;
6464
import org.springframework.util.ClassUtils;
6565
import org.springframework.util.ReflectionUtils;
66+
import org.springframework.util.StringUtils;
6667

6768
/**
6869
* {@link org.aopalliance.intercept.MethodInterceptor} that routes calls to an
@@ -609,7 +610,7 @@ public void destroy() {
609610
* Simple wrapper class around a method name and its signature.
610611
* Used as the key when caching methods.
611612
*/
612-
private static class MethodCacheKey {
613+
private static final class MethodCacheKey implements Comparable<MethodCacheKey> {
613614

614615
private final String name;
615616

@@ -628,7 +629,7 @@ public MethodCacheKey(String name, Class<?>[] parameterTypes) {
628629

629630
@Override
630631
public boolean equals(Object other) {
631-
if (other == this) {
632+
if (this == other) {
632633
return true;
633634
}
634635
MethodCacheKey otherKey = (MethodCacheKey) other;
@@ -639,6 +640,32 @@ public boolean equals(Object other) {
639640
public int hashCode() {
640641
return this.name.hashCode();
641642
}
643+
644+
@Override
645+
public String toString() {
646+
return this.name + "(" + StringUtils.arrayToCommaDelimitedString(this.parameterTypes) + ")";
647+
}
648+
649+
@Override
650+
public int compareTo(MethodCacheKey other) {
651+
int result = this.name.compareTo(other.name);
652+
if (result != 0) {
653+
return result;
654+
}
655+
if (this.parameterTypes.length < other.parameterTypes.length) {
656+
return -1;
657+
}
658+
if (this.parameterTypes.length > other.parameterTypes.length) {
659+
return 1;
660+
}
661+
for (int i = 0; i < this.parameterTypes.length; i++) {
662+
result = this.parameterTypes[i].getName().compareTo(other.parameterTypes[i].getName());
663+
if (result != 0) {
664+
return result;
665+
}
666+
}
667+
return 0;
668+
}
642669
}
643670

644671
}

0 commit comments

Comments
 (0)