Skip to content

Commit

Permalink
Cache key classes implement Comparable and consistently provide a toS…
Browse files Browse the repository at this point in the history
…tring representation

Issue: SPR-14017
  • Loading branch information
jhoeller committed Mar 26, 2016
1 parent a8b5ea1 commit 54aeb7a
Show file tree
Hide file tree
Showing 15 changed files with 222 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -586,7 +586,7 @@ public String toString() {
* Simple wrapper class around a Method. Used as the key when
* caching methods, for efficient equals and hashCode comparisons.
*/
private static class MethodCacheKey {
private static final class MethodCacheKey implements Comparable<MethodCacheKey> {

private final Method method;

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

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}
MethodCacheKey otherKey = (MethodCacheKey) other;
return (this.method == otherKey.method);
return (this == other || (other instanceof MethodCacheKey &&
this.method == ((MethodCacheKey) other).method));
}

@Override
public int hashCode() {
return this.hashCode;
}

@Override
public String toString() {
return this.method.toString();
}

@Override
public int compareTo(MethodCacheKey other) {
int result = this.method.getName().compareTo(other.method.getName());
if (result == 0) {
result = this.method.toString().compareTo(other.method.toString());
}
return result;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ private <T extends Annotation> Collection<CacheOperation> lazyInit(Collection<Ca
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());
Expand All @@ -109,7 +110,6 @@ CacheableOperation parseCacheableAnnotation(AnnotatedElement ae, DefaultCacheCon
builder.setCacheManager(cacheable.cacheManager());
builder.setCacheResolver(cacheable.cacheResolver());
builder.setSync(cacheable.sync());
builder.setName(ae.toString());

defaultConfig.applyDefault(builder);
CacheableOperation op = builder.build();
Expand All @@ -121,6 +121,7 @@ CacheableOperation parseCacheableAnnotation(AnnotatedElement ae, DefaultCacheCon
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());
Expand All @@ -129,7 +130,6 @@ CacheEvictOperation parseEvictAnnotation(AnnotatedElement ae, DefaultCacheConfig
builder.setCacheResolver(cacheEvict.cacheResolver());
builder.setCacheWide(cacheEvict.allEntries());
builder.setBeforeInvocation(cacheEvict.beforeInvocation());
builder.setName(ae.toString());

defaultConfig.applyDefault(builder);
CacheEvictOperation op = builder.build();
Expand All @@ -141,14 +141,14 @@ CacheEvictOperation parseEvictAnnotation(AnnotatedElement ae, DefaultCacheConfig
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());
builder.setKey(cachePut.key());
builder.setKeyGenerator(cachePut.keyGenerator());
builder.setCacheManager(cachePut.cacheManager());
builder.setCacheResolver(cachePut.cacheResolver());
builder.setName(ae.toString());

defaultConfig.applyDefault(builder);
CachePutOperation op = builder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ public void apply(Object result) {
}


private static class CacheOperationCacheKey {
private static final class CacheOperationCacheKey implements Comparable<CacheOperationCacheKey> {

private final CacheOperation cacheOperation;

Expand Down Expand Up @@ -774,6 +774,20 @@ public boolean equals(Object other) {
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;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -37,12 +44,10 @@ public boolean isBeforeInvocation() {
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;
Expand Down Expand Up @@ -71,4 +76,5 @@ public CacheEvictOperation build() {
return new CacheEvictOperation(this);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -59,11 +60,11 @@ protected CacheOperation(Builder b) {
this.toString = b.getOperationDescription().toString();
}


public String getName() {
return this.name;
}


@Override
public Set<String> getCacheNames() {
return this.cacheNames;
Expand Down Expand Up @@ -96,7 +97,6 @@ public String getCondition() {

/**
* This implementation compares the {@code toString()} results.
*
* @see #toString()
*/
@Override
Expand All @@ -106,7 +106,6 @@ public boolean equals(Object other) {

/**
* This implementation returns {@code toString()}'s hash code.
*
* @see #toString()
*/
@Override
Expand All @@ -118,14 +117,17 @@ public int hashCode() {
* 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
public final String toString() {
return this.toString;
}


/**
* @since 4.3
*/
public abstract static class Builder {

private String name = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class CachePutOperation extends CacheOperation {

private final String unless;


public CachePutOperation(CachePutOperation.Builder b) {
super(b);
this.unless = b.unless;
Expand All @@ -37,6 +38,10 @@ public String getUnless() {
return this.unless;
}


/**
* @since 4.3
*/
public static class Builder extends CacheOperation.Builder {

private String unless;
Expand All @@ -58,4 +63,5 @@ public CachePutOperation build() {
return new CachePutOperation(this);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class CacheableOperation extends CacheOperation {

private boolean sync;


public CacheableOperation(CacheableOperation.Builder b) {
super(b);
this.unless = b.unless;
Expand Down Expand Up @@ -76,4 +77,5 @@ public CacheableOperation build() {
return new CacheableOperation(this);
}
}

}
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -286,7 +286,7 @@ protected boolean supportsEvent(ApplicationListener<?> listener, ResolvableType
/**
* 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;

Expand All @@ -311,6 +311,23 @@ public boolean equals(Object other) {
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;
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* @since 4.2
* @see CachedExpressionEvaluator
*/
public final class AnnotatedElementKey {
public final class AnnotatedElementKey implements Comparable<AnnotatedElementKey> {

private final AnnotatedElement element;

Expand Down Expand Up @@ -66,4 +66,18 @@ public int hashCode() {
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;
}

}
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -63,6 +63,7 @@
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
Expand Down Expand Up @@ -609,7 +610,7 @@ public void destroy() {
* 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;

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

@Override
public boolean equals(Object other) {
if (other == this) {
if (this == other) {
return true;
}
MethodCacheKey otherKey = (MethodCacheKey) other;
Expand All @@ -639,6 +640,32 @@ public boolean equals(Object other) {
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;
}
}

}
Loading

0 comments on commit 54aeb7a

Please sign in to comment.