001/* 002 * Copyright 2002-2019 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.io.Serializable; 020import java.lang.reflect.Method; 021import java.util.ArrayList; 022import java.util.Collection; 023 024import org.springframework.lang.Nullable; 025import org.springframework.util.Assert; 026 027/** 028 * Composite {@link CacheOperationSource} implementation that iterates 029 * over a given array of {@code CacheOperationSource} instances. 030 * 031 * @author Costin Leau 032 * @author Juergen Hoeller 033 * @since 3.1 034 */ 035@SuppressWarnings("serial") 036public class CompositeCacheOperationSource implements CacheOperationSource, Serializable { 037 038 private final CacheOperationSource[] cacheOperationSources; 039 040 041 /** 042 * Create a new CompositeCacheOperationSource for the given sources. 043 * @param cacheOperationSources the CacheOperationSource instances to combine 044 */ 045 public CompositeCacheOperationSource(CacheOperationSource... cacheOperationSources) { 046 Assert.notEmpty(cacheOperationSources, "CacheOperationSource array must not be empty"); 047 this.cacheOperationSources = cacheOperationSources; 048 } 049 050 /** 051 * Return the {@code CacheOperationSource} instances that this 052 * {@code CompositeCacheOperationSource} combines. 053 */ 054 public final CacheOperationSource[] getCacheOperationSources() { 055 return this.cacheOperationSources; 056 } 057 058 059 @Override 060 public boolean isCandidateClass(Class<?> targetClass) { 061 for (CacheOperationSource source : this.cacheOperationSources) { 062 if (source.isCandidateClass(targetClass)) { 063 return true; 064 } 065 } 066 return false; 067 } 068 069 @Override 070 @Nullable 071 public Collection<CacheOperation> getCacheOperations(Method method, @Nullable Class<?> targetClass) { 072 Collection<CacheOperation> ops = null; 073 for (CacheOperationSource source : this.cacheOperationSources) { 074 Collection<CacheOperation> cacheOperations = source.getCacheOperations(method, targetClass); 075 if (cacheOperations != null) { 076 if (ops == null) { 077 ops = new ArrayList<>(); 078 } 079 ops.addAll(cacheOperations); 080 } 081 } 082 return ops; 083 } 084 085}