001/*
002 * Copyright 2012-2018 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 *      http://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.boot.autoconfigure.cache;
018
019import java.util.ArrayList;
020import java.util.Collections;
021import java.util.List;
022
023import org.springframework.boot.util.LambdaSafe;
024import org.springframework.cache.CacheManager;
025
026/**
027 * Invokes the available {@link CacheManagerCustomizer} instances in the context for a
028 * given {@link CacheManager}.
029 *
030 * @author Stephane Nicoll
031 * @since 1.5.0
032 */
033public class CacheManagerCustomizers {
034
035        private final List<CacheManagerCustomizer<?>> customizers;
036
037        public CacheManagerCustomizers(
038                        List<? extends CacheManagerCustomizer<?>> customizers) {
039                this.customizers = (customizers != null) ? new ArrayList<>(customizers)
040                                : Collections.emptyList();
041        }
042
043        /**
044         * Customize the specified {@link CacheManager}. Locates all
045         * {@link CacheManagerCustomizer} beans able to handle the specified instance and
046         * invoke {@link CacheManagerCustomizer#customize(CacheManager)} on them.
047         * @param <T> the type of cache manager
048         * @param cacheManager the cache manager to customize
049         * @return the cache manager
050         */
051        @SuppressWarnings("unchecked")
052        public <T extends CacheManager> T customize(T cacheManager) {
053                LambdaSafe.callbacks(CacheManagerCustomizer.class, this.customizers, cacheManager)
054                                .withLogger(CacheManagerCustomizers.class)
055                                .invoke((customizer) -> customizer.customize(cacheManager));
056                return cacheManager;
057        }
058
059}