KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > gbean > GOperationSignature


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.gbean;
19
20 import java.lang.reflect.Method JavaDoc;
21 import java.util.List JavaDoc;
22
23 /**
24  * This is a key class based on a MBean operation name and parameters.
25  *
26  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
27  */

28 public final class GOperationSignature {
29     private final static String JavaDoc[] NO_TYPES = new String JavaDoc[0];
30     private final String JavaDoc name;
31     private final String JavaDoc[] argumentTypes;
32
33     public GOperationSignature(Method JavaDoc method) {
34         name = method.getName();
35         Class JavaDoc[] parameters = method.getParameterTypes();
36         argumentTypes = new String JavaDoc[parameters.length];
37         for (int i = 0; i < parameters.length; i++) {
38             argumentTypes[i] = parameters[i].getName();
39         }
40     }
41
42     public GOperationSignature(String JavaDoc name, String JavaDoc[] argumentTypes) {
43         this.name = name;
44         if (argumentTypes != null) {
45             this.argumentTypes = argumentTypes;
46         } else {
47             this.argumentTypes = NO_TYPES;
48         }
49     }
50
51     public GOperationSignature(String JavaDoc name, List JavaDoc argumentTypes) {
52         this.name = name;
53         if (argumentTypes != null) {
54             this.argumentTypes = new String JavaDoc[argumentTypes.size()];
55             for (int i = 0; i < argumentTypes.size(); i++) {
56                 this.argumentTypes[i] = (String JavaDoc) argumentTypes.get(i);
57             }
58         } else {
59             this.argumentTypes = NO_TYPES;
60         }
61     }
62
63     public boolean equals(Object JavaDoc object) {
64         if (!(object instanceof GOperationSignature)) {
65             return false;
66         }
67
68         // match names
69
GOperationSignature methodKey = (GOperationSignature) object;
70         if (!methodKey.name.equals(name)) {
71             return false;
72         }
73
74         // match arg length
75
int length = methodKey.argumentTypes.length;
76         if (length != argumentTypes.length) {
77             return false;
78         }
79
80         // match each arg
81
for (int i = 0; i < length; i++) {
82             if (!methodKey.argumentTypes[i].equals(argumentTypes[i])) {
83                 return false;
84             }
85         }
86         return true;
87     }
88
89     public int hashCode() {
90         int result = 17;
91         result = 37 * result + name.hashCode();
92         for (int i = 0; i < argumentTypes.length; i++) {
93             result = 37 * result + argumentTypes[i].hashCode();
94         }
95         return result;
96     }
97
98     public String JavaDoc toString() {
99         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(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 }
109
Popular Tags