001/*
002 * Copyright 2002-2014 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.jcache.interceptor;
018
019import java.io.Serializable;
020import java.lang.reflect.Method;
021
022import org.springframework.aop.support.StaticMethodMatcherPointcut;
023import org.springframework.util.ObjectUtils;
024
025/**
026 * A Pointcut that matches if the underlying {@link JCacheOperationSource}
027 * has an operation for a given method.
028 *
029 * @author Stephane Nicoll
030 * @since 4.1
031 */
032@SuppressWarnings("serial")
033public abstract class JCacheOperationSourcePointcut
034                extends StaticMethodMatcherPointcut implements Serializable {
035
036        @Override
037        public boolean matches(Method method, Class<?> targetClass) {
038                JCacheOperationSource cas = getCacheOperationSource();
039                return (cas != null && cas.getCacheOperation(method, targetClass) != null);
040        }
041
042        /**
043         * Obtain the underlying {@link JCacheOperationSource} (may be {@code null}).
044         * To be implemented by subclasses.
045         */
046        protected abstract JCacheOperationSource getCacheOperationSource();
047
048        @Override
049        public boolean equals(Object other) {
050                if (this == other) {
051                        return true;
052                }
053                if (!(other instanceof JCacheOperationSourcePointcut)) {
054                        return false;
055                }
056                JCacheOperationSourcePointcut otherPc = (JCacheOperationSourcePointcut) other;
057                return ObjectUtils.nullSafeEquals(getCacheOperationSource(), otherPc.getCacheOperationSource());
058        }
059
060        @Override
061        public int hashCode() {
062                return JCacheOperationSourcePointcut.class.hashCode();
063        }
064
065        @Override
066        public String toString() {
067                return getClass().getName() + ": " + getCacheOperationSource();
068        }
069
070}