View Javadoc

1   /**
2    *
3    * Copyright 2003-2004 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  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;
21  import java.util.List;
22  
23  /**
24   * This is a key class based on a MBean operation name and parameters.
25   *
26   * @version $Rev: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $
27   */
28  public final class GOperationSignature {
29      private final static String[] NO_TYPES = new String[0];
30      private final String name;
31      private final String[] argumentTypes;
32  
33      public GOperationSignature(Method method) {
34          name = method.getName();
35          Class[] parameters = method.getParameterTypes();
36          argumentTypes = new String[parameters.length];
37          for (int i = 0; i < parameters.length; i++) {
38              argumentTypes[i] = parameters[i].getName();
39          }
40      }
41  
42      public GOperationSignature(String name, String[] 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 name, List argumentTypes) {
52          this.name = name;
53          if (argumentTypes != null) {
54              this.argumentTypes = new String[argumentTypes.size()];
55              for (int i = 0; i < argumentTypes.size(); i++) {
56                  this.argumentTypes[i] = (String) argumentTypes.get(i);
57              }
58          } else {
59              this.argumentTypes = NO_TYPES;
60          }
61      }
62  
63      public boolean equals(Object 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 toString() {
99          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 }