001/*
002 * Copyright 2002-2019 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.beans.factory.support;
018
019import java.lang.reflect.Method;
020import java.util.Set;
021import java.util.concurrent.CopyOnWriteArraySet;
022
023import org.springframework.lang.Nullable;
024
025/**
026 * Set of method overrides, determining which, if any, methods on a
027 * managed object the Spring IoC container will override at runtime.
028 *
029 * <p>The currently supported {@link MethodOverride} variants are
030 * {@link LookupOverride} and {@link ReplaceOverride}.
031 *
032 * @author Rod Johnson
033 * @author Juergen Hoeller
034 * @since 1.1
035 * @see MethodOverride
036 */
037public class MethodOverrides {
038
039        private final Set<MethodOverride> overrides = new CopyOnWriteArraySet<>();
040
041
042        /**
043         * Create new MethodOverrides.
044         */
045        public MethodOverrides() {
046        }
047
048        /**
049         * Deep copy constructor.
050         */
051        public MethodOverrides(MethodOverrides other) {
052                addOverrides(other);
053        }
054
055
056        /**
057         * Copy all given method overrides into this object.
058         */
059        public void addOverrides(@Nullable MethodOverrides other) {
060                if (other != null) {
061                        this.overrides.addAll(other.overrides);
062                }
063        }
064
065        /**
066         * Add the given method override.
067         */
068        public void addOverride(MethodOverride override) {
069                this.overrides.add(override);
070        }
071
072        /**
073         * Return all method overrides contained by this object.
074         * @return a Set of MethodOverride objects
075         * @see MethodOverride
076         */
077        public Set<MethodOverride> getOverrides() {
078                return this.overrides;
079        }
080
081        /**
082         * Return whether the set of method overrides is empty.
083         */
084        public boolean isEmpty() {
085                return this.overrides.isEmpty();
086        }
087
088        /**
089         * Return the override for the given method, if any.
090         * @param method method to check for overrides for
091         * @return the method override, or {@code null} if none
092         */
093        @Nullable
094        public MethodOverride getOverride(Method method) {
095                MethodOverride match = null;
096                for (MethodOverride candidate : this.overrides) {
097                        if (candidate.matches(method)) {
098                                match = candidate;
099                        }
100                }
101                return match;
102        }
103
104
105        @Override
106        public boolean equals(@Nullable Object other) {
107                if (this == other) {
108                        return true;
109                }
110                if (!(other instanceof MethodOverrides)) {
111                        return false;
112                }
113                MethodOverrides that = (MethodOverrides) other;
114                return this.overrides.equals(that.overrides);
115        }
116
117        @Override
118        public int hashCode() {
119                return this.overrides.hashCode();
120        }
121
122}