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.interceptor;
018
019import java.lang.reflect.Method;
020
021/**
022 * Simple key generator. Returns the parameter itself if a single non-null value
023 * is given, otherwise returns a {@link SimpleKey} of the parameters.
024 *
025 * <p>Unlike {@link DefaultKeyGenerator}, no collisions will occur with the keys
026 * generated by this class. The returned {@link SimpleKey} object can be safely
027 * used with a {@link org.springframework.cache.concurrent.ConcurrentMapCache},
028 * however, might not be suitable for all {@link org.springframework.cache.Cache}
029 * implementations.
030 *
031 * @author Phillip Webb
032 * @author Juergen Hoeller
033 * @since 4.0
034 * @see SimpleKey
035 * @see DefaultKeyGenerator
036 * @see org.springframework.cache.annotation.CachingConfigurer
037 */
038public class SimpleKeyGenerator implements KeyGenerator {
039
040        @Override
041        public Object generate(Object target, Method method, Object... params) {
042                return generateKey(params);
043        }
044
045        /**
046         * Generate a key based on the specified parameters.
047         */
048        public static Object generateKey(Object... params) {
049                if (params.length == 0) {
050                        return SimpleKey.EMPTY;
051                }
052                if (params.length == 1) {
053                        Object param = params[0];
054                        if (param != null && !param.getClass().isArray()) {
055                                return param;
056                        }
057                }
058                return new SimpleKey(params);
059        }
060
061}