001/* 002 * Copyright 2002-2017 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.cache.interceptor; 018 019import java.util.Collections; 020import java.util.LinkedHashSet; 021import java.util.Set; 022 023import org.springframework.util.Assert; 024 025/** 026 * Base class for cache operations. 027 * 028 * @author Costin Leau 029 * @author Stephane Nicoll 030 * @author Marcin Kamionowski 031 * @since 3.1 032 */ 033public abstract class CacheOperation implements BasicOperation { 034 035 private final String name; 036 037 private final Set<String> cacheNames; 038 039 private final String key; 040 041 private final String keyGenerator; 042 043 private final String cacheManager; 044 045 private final String cacheResolver; 046 047 private final String condition; 048 049 private final String toString; 050 051 052 /** 053 * @since 4.3 054 */ 055 protected CacheOperation(Builder b) { 056 this.name = b.name; 057 this.cacheNames = b.cacheNames; 058 this.key = b.key; 059 this.keyGenerator = b.keyGenerator; 060 this.cacheManager = b.cacheManager; 061 this.cacheResolver = b.cacheResolver; 062 this.condition = b.condition; 063 this.toString = b.getOperationDescription().toString(); 064 } 065 066 067 public String getName() { 068 return this.name; 069 } 070 071 @Override 072 public Set<String> getCacheNames() { 073 return this.cacheNames; 074 } 075 076 public String getKey() { 077 return this.key; 078 } 079 080 public String getKeyGenerator() { 081 return this.keyGenerator; 082 } 083 084 public String getCacheManager() { 085 return this.cacheManager; 086 } 087 088 public String getCacheResolver() { 089 return this.cacheResolver; 090 } 091 092 public String getCondition() { 093 return this.condition; 094 } 095 096 097 /** 098 * This implementation compares the {@code toString()} results. 099 * @see #toString() 100 */ 101 @Override 102 public boolean equals(Object other) { 103 return (other instanceof CacheOperation && toString().equals(other.toString())); 104 } 105 106 /** 107 * This implementation returns {@code toString()}'s hash code. 108 * @see #toString() 109 */ 110 @Override 111 public int hashCode() { 112 return toString().hashCode(); 113 } 114 115 /** 116 * Return an identifying description for this cache operation. 117 * <p>Returned value is produced by calling {@link Builder#getOperationDescription()} 118 * during object construction. This method is used in {@link #hashCode} and 119 * {@link #equals}. 120 * @see Builder#getOperationDescription() 121 */ 122 @Override 123 public final String toString() { 124 return this.toString; 125 } 126 127 128 /** 129 * @since 4.3 130 */ 131 public abstract static class Builder { 132 133 private String name = ""; 134 135 private Set<String> cacheNames = Collections.emptySet(); 136 137 private String key = ""; 138 139 private String keyGenerator = ""; 140 141 private String cacheManager = ""; 142 143 private String cacheResolver = ""; 144 145 private String condition = ""; 146 147 public void setName(String name) { 148 Assert.hasText(name, "Name must not be empty"); 149 this.name = name; 150 } 151 152 public void setCacheName(String cacheName) { 153 Assert.hasText(cacheName, "Cache name must not be empty"); 154 this.cacheNames = Collections.singleton(cacheName); 155 } 156 157 public void setCacheNames(String... cacheNames) { 158 this.cacheNames = new LinkedHashSet<String>(cacheNames.length); 159 for (String cacheName : cacheNames) { 160 Assert.hasText(cacheName, "Cache name must be non-empty if specified"); 161 this.cacheNames.add(cacheName); 162 } 163 } 164 165 public Set<String> getCacheNames() { 166 return this.cacheNames; 167 } 168 169 public void setKey(String key) { 170 Assert.notNull(key, "Key must not be null"); 171 this.key = key; 172 } 173 174 public String getKey() { 175 return this.key; 176 } 177 178 public String getKeyGenerator() { 179 return this.keyGenerator; 180 } 181 182 public String getCacheManager() { 183 return this.cacheManager; 184 } 185 186 public String getCacheResolver() { 187 return this.cacheResolver; 188 } 189 190 public void setKeyGenerator(String keyGenerator) { 191 Assert.notNull(keyGenerator, "KeyGenerator name must not be null"); 192 this.keyGenerator = keyGenerator; 193 } 194 195 public void setCacheManager(String cacheManager) { 196 Assert.notNull(cacheManager, "CacheManager name must not be null"); 197 this.cacheManager = cacheManager; 198 } 199 200 public void setCacheResolver(String cacheResolver) { 201 Assert.notNull(cacheResolver, "CacheResolver name must not be null"); 202 this.cacheResolver = cacheResolver; 203 } 204 205 public void setCondition(String condition) { 206 Assert.notNull(condition, "Condition must not be null"); 207 this.condition = condition; 208 } 209 210 /** 211 * Return an identifying description for this caching operation. 212 * <p>Available to subclasses, for inclusion in their {@code toString()} result. 213 */ 214 protected StringBuilder getOperationDescription() { 215 StringBuilder result = new StringBuilder(getClass().getSimpleName()); 216 result.append("[").append(this.name); 217 result.append("] caches=").append(this.cacheNames); 218 result.append(" | key='").append(this.key); 219 result.append("' | keyGenerator='").append(this.keyGenerator); 220 result.append("' | cacheManager='").append(this.cacheManager); 221 result.append("' | cacheResolver='").append(this.cacheResolver); 222 result.append("' | condition='").append(this.condition).append("'"); 223 return result; 224 } 225 226 public abstract CacheOperation build(); 227 } 228 229}