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