001/*
002 * Copyright 2002-2017 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.context.expression;
018
019import java.lang.reflect.AnnotatedElement;
020
021import org.springframework.util.Assert;
022import org.springframework.util.ObjectUtils;
023
024/**
025 * Represent an {@link AnnotatedElement} on a particular {@link Class}
026 * and is suitable as a key.
027 *
028 * @author Costin Leau
029 * @author Stephane Nicoll
030 * @since 4.2
031 * @see CachedExpressionEvaluator
032 */
033public final class AnnotatedElementKey implements Comparable<AnnotatedElementKey> {
034
035        private final AnnotatedElement element;
036
037        private final Class<?> targetClass;
038
039
040        /**
041         * Create a new instance with the specified {@link AnnotatedElement} and
042         * optional target {@link Class}.
043         */
044        public AnnotatedElementKey(AnnotatedElement element, Class<?> targetClass) {
045                Assert.notNull(element, "AnnotatedElement must not be null");
046                this.element = element;
047                this.targetClass = targetClass;
048        }
049
050
051        @Override
052        public boolean equals(Object other) {
053                if (this == other) {
054                        return true;
055                }
056                if (!(other instanceof AnnotatedElementKey)) {
057                        return false;
058                }
059                AnnotatedElementKey otherKey = (AnnotatedElementKey) other;
060                return (this.element.equals(otherKey.element) &&
061                                ObjectUtils.nullSafeEquals(this.targetClass, otherKey.targetClass));
062        }
063
064        @Override
065        public int hashCode() {
066                return this.element.hashCode() + (this.targetClass != null ? this.targetClass.hashCode() * 29 : 0);
067        }
068
069        @Override
070        public String toString() {
071                return this.element + (this.targetClass != null ? " on " + this.targetClass : "");
072        }
073
074        @Override
075        public int compareTo(AnnotatedElementKey other) {
076                int result = this.element.toString().compareTo(other.element.toString());
077                if (result == 0 && this.targetClass != null) {
078                        if (other.targetClass == null) {
079                                return 1;
080                        }
081                        result = this.targetClass.getName().compareTo(other.targetClass.getName());
082                }
083                return result;
084        }
085
086}