001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    
018    package org.apache.geronimo.gbean;
019    
020    import java.lang.reflect.Method;
021    import java.util.List;
022    
023    /**
024     * This is a key class based on a MBean operation name and parameters.
025     *
026     * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
027     */
028    public final class GOperationSignature {
029        private final static String[] NO_TYPES = new String[0];
030        private final String name;
031        private final String[] argumentTypes;
032    
033        public GOperationSignature(Method method) {
034            name = method.getName();
035            Class[] parameters = method.getParameterTypes();
036            argumentTypes = new String[parameters.length];
037            for (int i = 0; i < parameters.length; i++) {
038                argumentTypes[i] = parameters[i].getName();
039            }
040        }
041    
042        public GOperationSignature(String name, String[] argumentTypes) {
043            this.name = name;
044            if (argumentTypes != null) {
045                this.argumentTypes = argumentTypes;
046            } else {
047                this.argumentTypes = NO_TYPES;
048            }
049        }
050    
051        public GOperationSignature(String name, List argumentTypes) {
052            this.name = name;
053            if (argumentTypes != null) {
054                this.argumentTypes = new String[argumentTypes.size()];
055                for (int i = 0; i < argumentTypes.size(); i++) {
056                    this.argumentTypes[i] = (String) argumentTypes.get(i);
057                }
058            } else {
059                this.argumentTypes = NO_TYPES;
060            }
061        }
062    
063        public boolean equals(Object object) {
064            if (!(object instanceof GOperationSignature)) {
065                return false;
066            }
067    
068            // match names
069            GOperationSignature methodKey = (GOperationSignature) object;
070            if (!methodKey.name.equals(name)) {
071                return false;
072            }
073    
074            // match arg length
075            int length = methodKey.argumentTypes.length;
076            if (length != argumentTypes.length) {
077                return false;
078            }
079    
080            // match each arg
081            for (int i = 0; i < length; i++) {
082                if (!methodKey.argumentTypes[i].equals(argumentTypes[i])) {
083                    return false;
084                }
085            }
086            return true;
087        }
088    
089        public int hashCode() {
090            int result = 17;
091            result = 37 * result + name.hashCode();
092            for (int i = 0; i < argumentTypes.length; i++) {
093                result = 37 * result + argumentTypes[i].hashCode();
094            }
095            return result;
096        }
097    
098        public String toString() {
099            StringBuffer buffer = new StringBuffer(name).append("(");
100            for (int i = 0; i < argumentTypes.length; i++) {
101                if (i > 0) {
102                    buffer.append(", ");
103                }
104                buffer.append(argumentTypes[i]);
105            }
106            return buffer.append(")").toString();
107        }
108    }