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.core;
018
019import java.lang.reflect.Method;
020
021import org.springframework.util.ObjectUtils;
022
023/**
024 * A common key class for a method against a specific target class,
025 * including {@link #toString()} representation and {@link Comparable}
026 * support (as suggested for custom {@code HashMap} keys as of Java 8).
027 *
028 * @author Juergen Hoeller
029 * @since 4.3
030 */
031public final class MethodClassKey implements Comparable<MethodClassKey> {
032
033        private final Method method;
034
035        private final Class<?> targetClass;
036
037
038        /**
039         * Create a key object for the given method and target class.
040         * @param method the method to wrap (must not be {@code null})
041         * @param targetClass the target class that the method will be invoked
042         * on (may be {@code null} if identical to the declaring class)
043         */
044        public MethodClassKey(Method method, Class<?> targetClass) {
045                this.method = method;
046                this.targetClass = targetClass;
047        }
048
049
050        @Override
051        public boolean equals(Object other) {
052                if (this == other) {
053                        return true;
054                }
055                if (!(other instanceof MethodClassKey)) {
056                        return false;
057                }
058                MethodClassKey otherKey = (MethodClassKey) other;
059                return (this.method.equals(otherKey.method) &&
060                                ObjectUtils.nullSafeEquals(this.targetClass, otherKey.targetClass));
061        }
062
063        @Override
064        public int hashCode() {
065                return this.method.hashCode() + (this.targetClass != null ? this.targetClass.hashCode() * 29 : 0);
066        }
067
068        @Override
069        public String toString() {
070                return this.method + (this.targetClass != null ? " on " + this.targetClass : "");
071        }
072
073        @Override
074        public int compareTo(MethodClassKey other) {
075                int result = this.method.getName().compareTo(other.method.getName());
076                if (result == 0) {
077                        result = this.method.toString().compareTo(other.method.toString());
078                        if (result == 0 && this.targetClass != null && other.targetClass != null) {
079                                result = this.targetClass.getName().compareTo(other.targetClass.getName());
080                        }
081                }
082                return result;
083        }
084
085}