ExceptionTypeFilter and InstanceFilter live in util package itself now

Issue: SPR-9616
This commit is contained in:
Juergen Hoeller 2014-07-09 21:43:19 +02:00
parent 3c726aa6c1
commit bf9ccc8138
10 changed files with 112 additions and 92 deletions

View File

@ -25,7 +25,7 @@ import org.springframework.cache.interceptor.CacheOperationInvoker;
import org.springframework.cache.interceptor.CacheResolver; import org.springframework.cache.interceptor.CacheResolver;
import org.springframework.cache.jcache.model.CacheResultOperation; import org.springframework.cache.jcache.model.CacheResultOperation;
import org.springframework.util.SerializationUtils; import org.springframework.util.SerializationUtils;
import org.springframework.util.filter.ExceptionTypeFilter; import org.springframework.util.ExceptionTypeFilter;
/** /**
* Intercept methods annotated with {@link CacheResult}. * Intercept methods annotated with {@link CacheResult}.
@ -65,10 +65,10 @@ public class CacheResultInterceptor extends AbstractKeyCacheInterceptor<CacheRes
} }
return invocationResult; return invocationResult;
} }
catch (CacheOperationInvoker.ThrowableWrapper t) { catch (CacheOperationInvoker.ThrowableWrapper ex) {
Throwable original = t.getOriginal(); Throwable original = ex.getOriginal();
cacheException(exceptionCache, operation.getExceptionTypeFilter(), cacheKey, original); cacheException(exceptionCache, operation.getExceptionTypeFilter(), cacheKey, original);
throw t; throw ex;
} }
} }
@ -86,13 +86,12 @@ public class CacheResultInterceptor extends AbstractKeyCacheInterceptor<CacheRes
} }
protected void cacheException(Cache exceptionCache, ExceptionTypeFilter filter, protected void cacheException(Cache exceptionCache, ExceptionTypeFilter filter, Object cacheKey, Throwable ex) {
Object cacheKey, Throwable t) {
if (exceptionCache == null) { if (exceptionCache == null) {
return; return;
} }
if (filter.match(t.getClass())) { if (filter.match(ex.getClass())) {
exceptionCache.put(cacheKey, t); exceptionCache.put(cacheKey, ex);
} }
} }
@ -100,8 +99,7 @@ public class CacheResultInterceptor extends AbstractKeyCacheInterceptor<CacheRes
private Cache resolveExceptionCache(CacheOperationInvocationContext<CacheResultOperation> context) { private Cache resolveExceptionCache(CacheOperationInvocationContext<CacheResultOperation> context) {
CacheResolver exceptionCacheResolver = context.getOperation().getExceptionCacheResolver(); CacheResolver exceptionCacheResolver = context.getOperation().getExceptionCacheResolver();
if (exceptionCacheResolver != null) { if (exceptionCacheResolver != null) {
return extractFrom(context.getOperation() return extractFrom(context.getOperation().getExceptionCacheResolver().resolveCaches(context));
.getExceptionCacheResolver().resolveCaches(context));
} }
return null; return null;
} }
@ -121,8 +119,9 @@ public class CacheResultInterceptor extends AbstractKeyCacheInterceptor<CacheRes
* {@code methodName} arguments, followed by stack trace elements of the specified * {@code methodName} arguments, followed by stack trace elements of the specified
* {@code exception} after the common ancestor. * {@code exception} after the common ancestor.
*/ */
private static CacheOperationInvoker.ThrowableWrapper rewriteCallStack(Throwable exception, private static CacheOperationInvoker.ThrowableWrapper rewriteCallStack(
String className, String methodName) { Throwable exception, String className, String methodName) {
Throwable clone = cloneException(exception); Throwable clone = cloneException(exception);
if (clone == null) { if (clone == null) {
return new CacheOperationInvoker.ThrowableWrapper(exception); return new CacheOperationInvoker.ThrowableWrapper(exception);
@ -149,7 +148,7 @@ public class CacheResultInterceptor extends AbstractKeyCacheInterceptor<CacheRes
try { try {
return (T) SerializationUtils.deserialize(SerializationUtils.serialize(exception)); return (T) SerializationUtils.deserialize(SerializationUtils.serialize(exception));
} }
catch (Exception e) { catch (Exception ex) {
return null; // exception parameter cannot be cloned return null; // exception parameter cannot be cloned
} }
} }

View File

@ -16,8 +16,6 @@
package org.springframework.cache.jcache.model; package org.springframework.cache.jcache.model;
import static java.util.Arrays.*;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
@ -25,7 +23,6 @@ import java.util.Collections;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import javax.cache.annotation.CacheInvocationParameter; import javax.cache.annotation.CacheInvocationParameter;
import javax.cache.annotation.CacheKey; import javax.cache.annotation.CacheKey;
import javax.cache.annotation.CacheMethodDetails; import javax.cache.annotation.CacheMethodDetails;
@ -33,7 +30,9 @@ import javax.cache.annotation.CacheValue;
import org.springframework.cache.interceptor.CacheResolver; import org.springframework.cache.interceptor.CacheResolver;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.filter.ExceptionTypeFilter; import org.springframework.util.ExceptionTypeFilter;
import static java.util.Arrays.*;
/** /**
* A base {@link JCacheOperation} implementation. * A base {@link JCacheOperation} implementation.
@ -49,6 +48,7 @@ public abstract class BaseCacheOperation<A extends Annotation> implements JCache
protected final List<CacheParameterDetail> allParameterDetails; protected final List<CacheParameterDetail> allParameterDetails;
/** /**
* Create a new instance. * Create a new instance.
* @param methodDetails the {@link CacheMethodDetails} related to the cached method * @param methodDetails the {@link CacheMethodDetails} related to the cached method
@ -62,30 +62,32 @@ public abstract class BaseCacheOperation<A extends Annotation> implements JCache
this.allParameterDetails = initializeAllParameterDetails(methodDetails.getMethod()); this.allParameterDetails = initializeAllParameterDetails(methodDetails.getMethod());
} }
/** /**
* Return the {@link ExceptionTypeFilter} to use to filter exceptions thrown while * Return the {@link ExceptionTypeFilter} to use to filter exceptions thrown while
* invoking the method. * invoking the method.
*/ */
public abstract ExceptionTypeFilter getExceptionTypeFilter(); public abstract ExceptionTypeFilter getExceptionTypeFilter();
@Override @Override
public Method getMethod() { public Method getMethod() {
return methodDetails.getMethod(); return this.methodDetails.getMethod();
} }
@Override @Override
public Set<Annotation> getAnnotations() { public Set<Annotation> getAnnotations() {
return methodDetails.getAnnotations(); return this.methodDetails.getAnnotations();
} }
@Override @Override
public A getCacheAnnotation() { public A getCacheAnnotation() {
return methodDetails.getCacheAnnotation(); return this.methodDetails.getCacheAnnotation();
} }
@Override @Override
public String getCacheName() { public String getCacheName() {
return methodDetails.getCacheName(); return this.methodDetails.getCacheName();
} }
@Override @Override
@ -95,37 +97,28 @@ public abstract class BaseCacheOperation<A extends Annotation> implements JCache
@Override @Override
public CacheResolver getCacheResolver() { public CacheResolver getCacheResolver() {
return cacheResolver; return this.cacheResolver;
} }
@Override @Override
public CacheInvocationParameter[] getAllParameters(Object... values) { public CacheInvocationParameter[] getAllParameters(Object... values) {
if (allParameterDetails.size() != values.length) { if (this.allParameterDetails.size() != values.length) {
throw new IllegalStateException("Values mismatch, operation has " throw new IllegalStateException("Values mismatch, operation has " +
+ allParameterDetails.size() + " parameter(s) but got " + values.length + " value(s)"); this.allParameterDetails.size() + " parameter(s) but got " + values.length + " value(s)");
} }
List<CacheInvocationParameter> result = new ArrayList<CacheInvocationParameter>(); List<CacheInvocationParameter> result = new ArrayList<CacheInvocationParameter>();
for (int i = 0; i < allParameterDetails.size(); i++) { for (int i = 0; i < this.allParameterDetails.size(); i++) {
result.add(allParameterDetails.get(i).toCacheInvocationParameter(values[i])); result.add(this.allParameterDetails.get(i).toCacheInvocationParameter(values[i]));
} }
return result.toArray(new CacheInvocationParameter[result.size()]); return result.toArray(new CacheInvocationParameter[result.size()]);
} }
protected ExceptionTypeFilter createExceptionTypeFiler(Class<? extends Throwable>[] includes, protected ExceptionTypeFilter createExceptionTypeFilter(
Class<? extends Throwable>[] excludes) { Class<? extends Throwable>[] includes, Class<? extends Throwable>[] excludes) {
return new ExceptionTypeFilter(asList(includes), asList(excludes), true); return new ExceptionTypeFilter(asList(includes), asList(excludes), true);
} }
private static List<CacheParameterDetail> initializeAllParameterDetails(Method method) {
List<CacheParameterDetail> result = new ArrayList<CacheParameterDetail>();
for (int i = 0; i < method.getParameterTypes().length; i++) {
CacheParameterDetail detail = new CacheParameterDetail(method, i);
result.add(detail);
}
return result;
}
@Override @Override
public String toString() { public String toString() {
return getOperationDescription().append("]").toString(); return getOperationDescription().append("]").toString();
@ -144,6 +137,16 @@ public abstract class BaseCacheOperation<A extends Annotation> implements JCache
} }
private static List<CacheParameterDetail> initializeAllParameterDetails(Method method) {
List<CacheParameterDetail> result = new ArrayList<CacheParameterDetail>();
for (int i = 0; i < method.getParameterTypes().length; i++) {
CacheParameterDetail detail = new CacheParameterDetail(method, i);
result.add(detail);
}
return result;
}
protected static class CacheParameterDetail { protected static class CacheParameterDetail {
private final Class<?> rawType; private final Class<?> rawType;
@ -156,7 +159,7 @@ public abstract class BaseCacheOperation<A extends Annotation> implements JCache
private final boolean isValue; private final boolean isValue;
private CacheParameterDetail(Method m, int parameterPosition) { public CacheParameterDetail(Method m, int parameterPosition) {
this.rawType = m.getParameterTypes()[parameterPosition]; this.rawType = m.getParameterTypes()[parameterPosition];
this.annotations = new LinkedHashSet<Annotation>(); this.annotations = new LinkedHashSet<Annotation>();
boolean foundKeyAnnotation = false; boolean foundKeyAnnotation = false;
@ -176,15 +179,15 @@ public abstract class BaseCacheOperation<A extends Annotation> implements JCache
} }
public int getParameterPosition() { public int getParameterPosition() {
return parameterPosition; return this.parameterPosition;
} }
protected boolean isKey() { protected boolean isKey() {
return isKey; return this.isKey;
} }
protected boolean isValue() { protected boolean isValue() {
return isValue; return this.isValue;
} }
public CacheInvocationParameter toCacheInvocationParameter(Object value) { public CacheInvocationParameter toCacheInvocationParameter(Object value) {
@ -192,35 +195,36 @@ public abstract class BaseCacheOperation<A extends Annotation> implements JCache
} }
} }
protected static class CacheInvocationParameterImpl implements CacheInvocationParameter { protected static class CacheInvocationParameterImpl implements CacheInvocationParameter {
private final CacheParameterDetail detail; private final CacheParameterDetail detail;
private final Object value; private final Object value;
private CacheInvocationParameterImpl(CacheParameterDetail detail, Object value) { public CacheInvocationParameterImpl(CacheParameterDetail detail, Object value) {
this.detail = detail; this.detail = detail;
this.value = value; this.value = value;
} }
@Override @Override
public Class<?> getRawType() { public Class<?> getRawType() {
return detail.rawType; return this.detail.rawType;
} }
@Override @Override
public Object getValue() { public Object getValue() {
return value; return this.value;
} }
@Override @Override
public Set<Annotation> getAnnotations() { public Set<Annotation> getAnnotations() {
return detail.annotations; return this.detail.annotations;
} }
@Override @Override
public int getParameterPosition() { public int getParameterPosition() {
return detail.parameterPosition; return this.detail.parameterPosition;
} }
} }

View File

@ -25,7 +25,7 @@ import javax.cache.annotation.CachePut;
import org.springframework.cache.interceptor.CacheResolver; import org.springframework.cache.interceptor.CacheResolver;
import org.springframework.cache.interceptor.KeyGenerator; import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.util.filter.ExceptionTypeFilter; import org.springframework.util.ExceptionTypeFilter;
/** /**
* The {@link JCacheOperation} implementation for a {@link CachePut} operation. * The {@link JCacheOperation} implementation for a {@link CachePut} operation.
@ -40,11 +40,13 @@ public class CachePutOperation extends BaseKeyCacheOperation<CachePut> {
private final CacheParameterDetail valueParameterDetail; private final CacheParameterDetail valueParameterDetail;
public CachePutOperation(CacheMethodDetails<CachePut> methodDetails,
CacheResolver cacheResolver, KeyGenerator keyGenerator) { public CachePutOperation(
CacheMethodDetails<CachePut> methodDetails, CacheResolver cacheResolver, KeyGenerator keyGenerator) {
super(methodDetails, cacheResolver, keyGenerator); super(methodDetails, cacheResolver, keyGenerator);
CachePut ann = methodDetails.getCacheAnnotation(); CachePut ann = methodDetails.getCacheAnnotation();
this.exceptionTypeFilter = createExceptionTypeFiler(ann.cacheFor(), ann.noCacheFor()); this.exceptionTypeFilter = createExceptionTypeFilter(ann.cacheFor(), ann.noCacheFor());
this.valueParameterDetail = initializeValueParameterDetail(methodDetails.getMethod(), allParameterDetails); this.valueParameterDetail = initializeValueParameterDetail(methodDetails.getMethod(), allParameterDetails);
if (valueParameterDetail == null) { if (valueParameterDetail == null) {
throw new IllegalArgumentException("No parameter annotated with @CacheValue was found for " + throw new IllegalArgumentException("No parameter annotated with @CacheValue was found for " +
@ -52,9 +54,10 @@ public class CachePutOperation extends BaseKeyCacheOperation<CachePut> {
} }
} }
@Override @Override
public ExceptionTypeFilter getExceptionTypeFilter() { public ExceptionTypeFilter getExceptionTypeFilter() {
return exceptionTypeFilter; return this.exceptionTypeFilter;
} }
/** /**
@ -74,17 +77,18 @@ public class CachePutOperation extends BaseKeyCacheOperation<CachePut> {
* @return the {@link CacheInvocationParameter} instance for the value parameter * @return the {@link CacheInvocationParameter} instance for the value parameter
*/ */
public CacheInvocationParameter getValueParameter(Object... values) { public CacheInvocationParameter getValueParameter(Object... values) {
int parameterPosition = valueParameterDetail.getParameterPosition(); int parameterPosition = this.valueParameterDetail.getParameterPosition();
if (parameterPosition >= values.length) { if (parameterPosition >= values.length) {
throw new IllegalStateException("Values mismatch, value parameter at position " throw new IllegalStateException("Values mismatch, value parameter at position " +
+ parameterPosition + " cannot be matched against " + values.length + " value(s)"); parameterPosition + " cannot be matched against " + values.length + " value(s)");
} }
return valueParameterDetail.toCacheInvocationParameter(values[parameterPosition]); return this.valueParameterDetail.toCacheInvocationParameter(values[parameterPosition]);
} }
private static CacheParameterDetail initializeValueParameterDetail(Method method, private static CacheParameterDetail initializeValueParameterDetail(
List<CacheParameterDetail> allParameters) { Method method, List<CacheParameterDetail> allParameters) {
CacheParameterDetail result = null; CacheParameterDetail result = null;
for (CacheParameterDetail parameter : allParameters) { for (CacheParameterDetail parameter : allParameters) {
if (parameter.isValue()) { if (parameter.isValue()) {

View File

@ -20,7 +20,7 @@ import javax.cache.annotation.CacheMethodDetails;
import javax.cache.annotation.CacheRemoveAll; import javax.cache.annotation.CacheRemoveAll;
import org.springframework.cache.interceptor.CacheResolver; import org.springframework.cache.interceptor.CacheResolver;
import org.springframework.util.filter.ExceptionTypeFilter; import org.springframework.util.ExceptionTypeFilter;
/** /**
* The {@link JCacheOperation} implementation for a {@link CacheRemoveAll} operation. * The {@link JCacheOperation} implementation for a {@link CacheRemoveAll} operation.
@ -33,15 +33,17 @@ public class CacheRemoveAllOperation extends BaseCacheOperation<CacheRemoveAll>
private final ExceptionTypeFilter exceptionTypeFilter; private final ExceptionTypeFilter exceptionTypeFilter;
public CacheRemoveAllOperation(CacheMethodDetails<CacheRemoveAll> methodDetails, CacheResolver cacheResolver) { public CacheRemoveAllOperation(CacheMethodDetails<CacheRemoveAll> methodDetails, CacheResolver cacheResolver) {
super(methodDetails, cacheResolver); super(methodDetails, cacheResolver);
CacheRemoveAll ann = methodDetails.getCacheAnnotation(); CacheRemoveAll ann = methodDetails.getCacheAnnotation();
this.exceptionTypeFilter = createExceptionTypeFiler(ann.evictFor(), ann.noEvictFor()); this.exceptionTypeFilter = createExceptionTypeFilter(ann.evictFor(), ann.noEvictFor());
} }
@Override @Override
public ExceptionTypeFilter getExceptionTypeFilter() { public ExceptionTypeFilter getExceptionTypeFilter() {
return exceptionTypeFilter; return this.exceptionTypeFilter;
} }
/** /**

View File

@ -21,7 +21,7 @@ import javax.cache.annotation.CacheRemove;
import org.springframework.cache.interceptor.CacheResolver; import org.springframework.cache.interceptor.CacheResolver;
import org.springframework.cache.interceptor.KeyGenerator; import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.util.filter.ExceptionTypeFilter; import org.springframework.util.ExceptionTypeFilter;
/** /**
* The {@link JCacheOperation} implementation for a {@link CacheRemove} operation. * The {@link JCacheOperation} implementation for a {@link CacheRemove} operation.
@ -34,16 +34,19 @@ public class CacheRemoveOperation extends BaseKeyCacheOperation<CacheRemove> {
private final ExceptionTypeFilter exceptionTypeFilter; private final ExceptionTypeFilter exceptionTypeFilter;
public CacheRemoveOperation(CacheMethodDetails<CacheRemove> methodDetails,
CacheResolver cacheResolver, KeyGenerator keyGenerator) { public CacheRemoveOperation(
CacheMethodDetails<CacheRemove> methodDetails, CacheResolver cacheResolver, KeyGenerator keyGenerator) {
super(methodDetails, cacheResolver, keyGenerator); super(methodDetails, cacheResolver, keyGenerator);
CacheRemove ann = methodDetails.getCacheAnnotation(); CacheRemove ann = methodDetails.getCacheAnnotation();
this.exceptionTypeFilter = createExceptionTypeFiler(ann.evictFor(), ann.noEvictFor()); this.exceptionTypeFilter = createExceptionTypeFilter(ann.evictFor(), ann.noEvictFor());
} }
@Override @Override
public ExceptionTypeFilter getExceptionTypeFilter() { public ExceptionTypeFilter getExceptionTypeFilter() {
return exceptionTypeFilter; return this.exceptionTypeFilter;
} }
/** /**

View File

@ -22,7 +22,7 @@ import javax.cache.annotation.CacheResult;
import org.springframework.cache.interceptor.CacheResolver; import org.springframework.cache.interceptor.CacheResolver;
import org.springframework.cache.interceptor.KeyGenerator; import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.util.filter.ExceptionTypeFilter; import org.springframework.util.ExceptionTypeFilter;
/** /**
* The {@link JCacheOperation} implementation for a {@link CacheResult} operation. * The {@link JCacheOperation} implementation for a {@link CacheResult} operation.
@ -39,20 +39,22 @@ public class CacheResultOperation extends BaseKeyCacheOperation<CacheResult> {
private final String exceptionCacheName; private final String exceptionCacheName;
public CacheResultOperation(CacheMethodDetails<CacheResult> methodDetails,
CacheResolver cacheResolver, KeyGenerator keyGenerator, public CacheResultOperation(CacheMethodDetails<CacheResult> methodDetails, CacheResolver cacheResolver,
CacheResolver exceptionCacheResolver) { KeyGenerator keyGenerator, CacheResolver exceptionCacheResolver) {
super(methodDetails, cacheResolver, keyGenerator); super(methodDetails, cacheResolver, keyGenerator);
CacheResult ann = methodDetails.getCacheAnnotation(); CacheResult ann = methodDetails.getCacheAnnotation();
this.exceptionTypeFilter = createExceptionTypeFiler(ann.cachedExceptions(), ann.nonCachedExceptions()); this.exceptionTypeFilter = createExceptionTypeFilter(ann.cachedExceptions(), ann.nonCachedExceptions());
this.exceptionCacheResolver = exceptionCacheResolver; this.exceptionCacheResolver = exceptionCacheResolver;
String exceptionCacheNameCandidate = ann.exceptionCacheName(); String exceptionCacheNameCandidate = ann.exceptionCacheName();
this.exceptionCacheName = StringUtils.hasText(exceptionCacheNameCandidate) ? exceptionCacheNameCandidate : null; this.exceptionCacheName = StringUtils.hasText(exceptionCacheNameCandidate) ? exceptionCacheNameCandidate : null;
} }
@Override @Override
public ExceptionTypeFilter getExceptionTypeFilter() { public ExceptionTypeFilter getExceptionTypeFilter() {
return exceptionTypeFilter; return this.exceptionTypeFilter;
} }
/** /**
@ -69,7 +71,7 @@ public class CacheResultOperation extends BaseKeyCacheOperation<CacheResult> {
* use for matching exceptions thrown by this operation. * use for matching exceptions thrown by this operation.
*/ */
public CacheResolver getExceptionCacheResolver() { public CacheResolver getExceptionCacheResolver() {
return exceptionCacheResolver; return this.exceptionCacheResolver;
} }
/** /**
@ -78,7 +80,7 @@ public class CacheResultOperation extends BaseKeyCacheOperation<CacheResult> {
* @see javax.cache.annotation.CacheResult#exceptionCacheName() * @see javax.cache.annotation.CacheResult#exceptionCacheName()
*/ */
public String getExceptionCacheName() { public String getExceptionCacheName() {
return exceptionCacheName; return this.exceptionCacheName;
} }
} }

View File

@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.util.filter; package org.springframework.util;
import java.util.Collection; import java.util.Collection;
@ -29,6 +29,7 @@ public class ExceptionTypeFilter extends InstanceFilter<Class<? extends Throwabl
public ExceptionTypeFilter(Collection<? extends Class<? extends Throwable>> includes, public ExceptionTypeFilter(Collection<? extends Class<? extends Throwable>> includes,
Collection<? extends Class<? extends Throwable>> excludes, boolean matchIfEmpty) { Collection<? extends Class<? extends Throwable>> excludes, boolean matchIfEmpty) {
super(includes, excludes, matchIfEmpty); super(includes, excludes, matchIfEmpty);
} }

View File

@ -14,13 +14,11 @@
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.util.filter; package org.springframework.util;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import org.springframework.util.Assert;
/** /**
* A simple instance filter that checks if a given instance match based on * A simple instance filter that checks if a given instance match based on
* a collection of includes and excludes element. * a collection of includes and excludes element.
@ -39,6 +37,7 @@ public class InstanceFilter<T> {
private final boolean matchIfEmpty; private final boolean matchIfEmpty;
/** /**
* Create a new instance based on includes/excludes collections. * Create a new instance based on includes/excludes collections.
* <p>A particular element will match if it "matches" the one of the element in the * <p>A particular element will match if it "matches" the one of the element in the
@ -47,7 +46,6 @@ public class InstanceFilter<T> {
* another if it is equals according to {@link Object#equals(Object)} * another if it is equals according to {@link Object#equals(Object)}
* <p>If both collections are empty, {@code matchIfEmpty} defines if * <p>If both collections are empty, {@code matchIfEmpty} defines if
* an element matches or not. * an element matches or not.
*
* @param includes the collection of includes * @param includes the collection of includes
* @param excludes the collection of excludes * @param excludes the collection of excludes
* @param matchIfEmpty the matching result if both the includes and the excludes * @param matchIfEmpty the matching result if both the includes and the excludes
@ -61,20 +59,21 @@ public class InstanceFilter<T> {
this.matchIfEmpty = matchIfEmpty; this.matchIfEmpty = matchIfEmpty;
} }
/** /**
* Determine if the specified {code instance} matches this filter. * Determine if the specified {code instance} matches this filter.
*/ */
public boolean match(T instance) { public boolean match(T instance) {
Assert.notNull(instance, "The instance to match is mandatory."); Assert.notNull(instance, "The instance to match is mandatory");
boolean includesSet = !includes.isEmpty(); boolean includesSet = !this.includes.isEmpty();
boolean excludesSet = !excludes.isEmpty(); boolean excludesSet = !this.excludes.isEmpty();
if (!includesSet && !excludesSet) { if (!includesSet && !excludesSet) {
return matchIfEmpty; return this.matchIfEmpty;
} }
boolean matchIncludes = match(instance, includes); boolean matchIncludes = match(instance, this.includes);
boolean matchExcludes = match(instance, excludes); boolean matchExcludes = match(instance, this.excludes);
if (!includesSet) { if (!includesSet) {
return !matchExcludes; return !matchExcludes;
@ -89,7 +88,6 @@ public class InstanceFilter<T> {
/** /**
* Determine if the specified {@code instance} is equal to the * Determine if the specified {@code instance} is equal to the
* specified {@code candidate}. * specified {@code candidate}.
*
* @param instance the instance to handle * @param instance the instance to handle
* @param candidate a candidate defined by this filter * @param candidate a candidate defined by this filter
* @return {@code true} if the instance matches the candidate * @return {@code true} if the instance matches the candidate
@ -101,7 +99,6 @@ public class InstanceFilter<T> {
/** /**
* Determine if the specified {@code instance} matches one of the candidates. * Determine if the specified {@code instance} matches one of the candidates.
* <p>If the candidates collection is {@code null}, returns {@code false}. * <p>If the candidates collection is {@code null}, returns {@code false}.
*
* @param instance the instance to check * @param instance the instance to check
* @param candidates a list of candidates * @param candidates a list of candidates
* @return {@code true} if the instance match or the candidates collection is null * @return {@code true} if the instance match or the candidates collection is null
@ -117,7 +114,11 @@ public class InstanceFilter<T> {
@Override @Override
public String toString() { public String toString() {
return "includes=" + includes + ", excludes=" + excludes + ", matchIfEmpty=" + matchIfEmpty; StringBuilder sb = new StringBuilder(getClass().getSimpleName());
sb.append(": includes=").append(this.includes);
sb.append(", excludes=").append(this.excludes);
sb.append(", matchIfEmpty=").append(this.matchIfEmpty);
return sb.toString();
} }
} }

View File

@ -14,13 +14,15 @@
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.util.filter; package org.springframework.util;
import static java.util.Arrays.*; import static java.util.Arrays.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import org.junit.Test; import org.junit.Test;
import org.springframework.util.ExceptionTypeFilter;
/** /**
* @author Stephane Nicoll * @author Stephane Nicoll
*/ */

View File

@ -14,13 +14,15 @@
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.util.filter; package org.springframework.util;
import static java.util.Arrays.*; import static java.util.Arrays.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import org.junit.Test; import org.junit.Test;
import org.springframework.util.InstanceFilter;
/** /**
* @author Stephane Nicoll * @author Stephane Nicoll
*/ */