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;
020import java.util.Arrays;
021
022/**
023 * Default key generator. Returns {@value #NO_PARAM_KEY} if no
024 * parameters are provided, the parameter itself if only one is given or
025 * a hash code computed from all given parameters' hash code values.
026 * Uses the constant value {@value #NULL_PARAM_KEY} for any
027 * {@code null} parameters given.
028 *
029 * <p>NOTE: As this implementation returns only a hash of the parameters
030 * it is possible for key collisions to occur. Since Spring 4.0 the
031 * {@link SimpleKeyGenerator} is used when no explicit key generator
032 * has been defined. This class remains for applications that do not
033 * wish to migrate to the {@link SimpleKeyGenerator}.
034 *
035 * @author Costin Leau
036 * @author Chris Beams
037 * @author Juergen Hoeller
038 * @since 3.1
039 * @deprecated as of Spring 4.0, in favor of {@link SimpleKeyGenerator}
040 * or custom {@link KeyGenerator} implementations based on hash codes
041 */
042@Deprecated
043public class DefaultKeyGenerator implements KeyGenerator {
044
045        public static final int NO_PARAM_KEY = 0;
046
047        public static final int NULL_PARAM_KEY = 53;
048
049
050        @Override
051        public Object generate(Object target, Method method, Object... params) {
052                if (params.length == 0) {
053                        return NO_PARAM_KEY;
054                }
055                if (params.length == 1) {
056                        Object param = params[0];
057                        if (param == null) {
058                                return NULL_PARAM_KEY;
059                        }
060                        if (!param.getClass().isArray()) {
061                                return param;
062                        }
063                }
064                return Arrays.deepHashCode(params);
065        }
066
067}