001/*
002 * Copyright 2002-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 *      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.util.Arrays;
021
022import org.springframework.util.Assert;
023import org.springframework.util.StringUtils;
024
025/**
026 * A simple key as returned from the {@link SimpleKeyGenerator}.
027 *
028 * @author Phillip Webb
029 * @since 4.0
030 * @see SimpleKeyGenerator
031 */
032@SuppressWarnings("serial")
033public class SimpleKey implements Serializable {
034
035        /**
036         * An empty key.
037         */
038        public static final SimpleKey EMPTY = new SimpleKey();
039
040
041        private final Object[] params;
042
043        private final int hashCode;
044
045
046        /**
047         * Create a new {@link SimpleKey} instance.
048         * @param elements the elements of the key
049         */
050        public SimpleKey(Object... elements) {
051                Assert.notNull(elements, "Elements must not be null");
052                this.params = new Object[elements.length];
053                System.arraycopy(elements, 0, this.params, 0, elements.length);
054                this.hashCode = Arrays.deepHashCode(this.params);
055        }
056
057
058        @Override
059        public boolean equals(Object other) {
060                return (this == other ||
061                                (other instanceof SimpleKey && Arrays.deepEquals(this.params, ((SimpleKey) other).params)));
062        }
063
064        @Override
065        public final int hashCode() {
066                return this.hashCode;
067        }
068
069        @Override
070        public String toString() {
071                return getClass().getSimpleName() + " [" + StringUtils.arrayToCommaDelimitedString(this.params) + "]";
072        }
073
074}