summaryrefslogtreecommitdiff
path: root/spring-context/src/main/java/org/springframework/cache/interceptor/CacheOperation.java
diff options
context:
space:
mode:
Diffstat (limited to 'spring-context/src/main/java/org/springframework/cache/interceptor/CacheOperation.java')
-rw-r--r--spring-context/src/main/java/org/springframework/cache/interceptor/CacheOperation.java194
1 files changed, 128 insertions, 66 deletions
diff --git a/spring-context/src/main/java/org/springframework/cache/interceptor/CacheOperation.java b/spring-context/src/main/java/org/springframework/cache/interceptor/CacheOperation.java
index 6264a040..271b59f1 100644
--- a/spring-context/src/main/java/org/springframework/cache/interceptor/CacheOperation.java
+++ b/spring-context/src/main/java/org/springframework/cache/interceptor/CacheOperation.java
@@ -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.
@@ -27,45 +27,45 @@ import org.springframework.util.Assert;
*
* @author Costin Leau
* @author Stephane Nicoll
+ * @author Marcin Kamionowski
* @since 3.1
*/
public abstract class CacheOperation implements BasicOperation {
- private String name = "";
+ private final String name;
- private Set<String> cacheNames = Collections.emptySet();
+ private final Set<String> cacheNames;
- private String key = "";
+ private final String key;
- private String keyGenerator = "";
+ private final String keyGenerator;
- private String cacheManager = "";
+ private final String cacheManager;
- private String cacheResolver = "";
+ private final String cacheResolver;
- private String condition = "";
+ private final String condition;
+ private final String toString;
- public void setName(String name) {
- Assert.hasText(name);
- this.name = name;
- }
- public String getName() {
- return this.name;
+ /**
+ * @since 4.3
+ */
+ protected CacheOperation(Builder b) {
+ this.name = b.name;
+ this.cacheNames = b.cacheNames;
+ this.key = b.key;
+ this.keyGenerator = b.keyGenerator;
+ this.cacheManager = b.cacheManager;
+ this.cacheResolver = b.cacheResolver;
+ this.condition = b.condition;
+ this.toString = b.getOperationDescription().toString();
}
- public void setCacheName(String cacheName) {
- Assert.hasText(cacheName);
- this.cacheNames = Collections.singleton(cacheName);
- }
- public void setCacheNames(String... cacheNames) {
- this.cacheNames = new LinkedHashSet<String>(cacheNames.length);
- for (String cacheName : cacheNames) {
- Assert.hasText(cacheName, "Cache name must be non-null if specified");
- this.cacheNames.add(cacheName);
- }
+ public String getName() {
+ return this.name;
}
@Override
@@ -73,47 +73,22 @@ public abstract class CacheOperation implements BasicOperation {
return this.cacheNames;
}
- public void setKey(String key) {
- Assert.notNull(key);
- this.key = key;
- }
-
public String getKey() {
return this.key;
}
- public void setKeyGenerator(String keyGenerator) {
- Assert.notNull(keyGenerator);
- this.keyGenerator = keyGenerator;
- }
-
public String getKeyGenerator() {
return this.keyGenerator;
}
- public void setCacheManager(String cacheManager) {
- Assert.notNull(cacheManager);
- this.cacheManager = cacheManager;
- }
-
public String getCacheManager() {
return this.cacheManager;
}
- public void setCacheResolver(String cacheResolver) {
- Assert.notNull(cacheManager);
- this.cacheResolver = cacheResolver;
- }
-
public String getCacheResolver() {
return this.cacheResolver;
}
- public void setCondition(String condition) {
- Assert.notNull(condition);
- this.condition = condition;
- }
-
public String getCondition() {
return this.condition;
}
@@ -139,29 +114,116 @@ public abstract class CacheOperation implements BasicOperation {
/**
* Return an identifying description for this cache operation.
- * <p>Has to be overridden in subclasses for correct {@code equals}
- * and {@code hashCode} behavior. Alternatively, {@link #equals}
- * and {@link #hashCode} can be overridden themselves.
+ * <p>Returned value is produced by calling {@link Builder#getOperationDescription()}
+ * during object construction. This method is used in {@link #hashCode} and
+ * {@link #equals}.
+ * @see Builder#getOperationDescription()
*/
@Override
- public String toString() {
- return getOperationDescription().toString();
+ public final String toString() {
+ return this.toString;
}
+
/**
- * Return an identifying description for this caching operation.
- * <p>Available to subclasses, for inclusion in their {@code toString()} result.
+ * @since 4.3
*/
- protected StringBuilder getOperationDescription() {
- StringBuilder result = new StringBuilder(getClass().getSimpleName());
- result.append("[").append(this.name);
- result.append("] caches=").append(this.cacheNames);
- result.append(" | key='").append(this.key);
- result.append("' | keyGenerator='").append(this.keyGenerator);
- result.append("' | cacheManager='").append(this.cacheManager);
- result.append("' | cacheResolver='").append(this.cacheResolver);
- result.append("' | condition='").append(this.condition).append("'");
- return result;
+ public abstract static class Builder {
+
+ private String name = "";
+
+ private Set<String> cacheNames = Collections.emptySet();
+
+ private String key = "";
+
+ private String keyGenerator = "";
+
+ private String cacheManager = "";
+
+ private String cacheResolver = "";
+
+ private String condition = "";
+
+ public void setName(String name) {
+ Assert.hasText(name);
+ this.name = name;
+ }
+
+ public void setCacheName(String cacheName) {
+ Assert.hasText(cacheName);
+ this.cacheNames = Collections.singleton(cacheName);
+ }
+
+ public void setCacheNames(String... cacheNames) {
+ this.cacheNames = new LinkedHashSet<String>(cacheNames.length);
+ for (String cacheName : cacheNames) {
+ Assert.hasText(cacheName, "Cache name must be non-null if specified");
+ this.cacheNames.add(cacheName);
+ }
+ }
+
+ public Set<String> getCacheNames() {
+ return this.cacheNames;
+ }
+
+ public void setKey(String key) {
+ Assert.notNull(key);
+ this.key = key;
+ }
+
+ public String getKey() {
+ return this.key;
+ }
+
+ public String getKeyGenerator() {
+ return this.keyGenerator;
+ }
+
+ public String getCacheManager() {
+ return this.cacheManager;
+ }
+
+ public String getCacheResolver() {
+ return this.cacheResolver;
+ }
+
+ public void setKeyGenerator(String keyGenerator) {
+ Assert.notNull(keyGenerator);
+ this.keyGenerator = keyGenerator;
+ }
+
+ public void setCacheManager(String cacheManager) {
+ Assert.notNull(cacheManager);
+ this.cacheManager = cacheManager;
+ }
+
+ public void setCacheResolver(String cacheResolver) {
+ Assert.notNull(this.cacheManager);
+ this.cacheResolver = cacheResolver;
+ }
+
+ public void setCondition(String condition) {
+ Assert.notNull(condition);
+ this.condition = condition;
+ }
+
+ /**
+ * Return an identifying description for this caching operation.
+ * <p>Available to subclasses, for inclusion in their {@code toString()} result.
+ */
+ protected StringBuilder getOperationDescription() {
+ StringBuilder result = new StringBuilder(getClass().getSimpleName());
+ result.append("[").append(this.name);
+ result.append("] caches=").append(this.cacheNames);
+ result.append(" | key='").append(this.key);
+ result.append("' | keyGenerator='").append(this.keyGenerator);
+ result.append("' | cacheManager='").append(this.cacheManager);
+ result.append("' | cacheResolver='").append(this.cacheResolver);
+ result.append("' | condition='").append(this.condition).append("'");
+ return result;
+ }
+
+ public abstract CacheOperation build();
}
}