KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > interop > util > ParameterList


1 /**
2  *
3  * Copyright 2004-2005 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  *
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.geronimo.interop.util;
19
20 import java.lang.reflect.Method JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.LinkedList JavaDoc;
23
24
25 public class ParameterList extends LinkedList JavaDoc {
26     public ParameterList() {
27     }
28
29     public ParameterList(Class JavaDoc[] types) {
30         init(types);
31     }
32
33     public ParameterList(Method JavaDoc template) {
34         Class JavaDoc[] types = template.getParameterTypes();
35         init(types);
36     }
37
38     private void init(Class JavaDoc[] types) {
39         int n = types.length;
40         for (int i = 0; i < n; i++) {
41             Class JavaDoc type = types[i];
42             add(type, "p" + (i + 1));
43         }
44     }
45
46     public ParameterList add(String JavaDoc type, String JavaDoc name) {
47         add(new MethodParameter(type, name));
48         return this;
49     }
50
51     public ParameterList add(Class JavaDoc type, String JavaDoc name) {
52         return add(JavaType.getName(type), name);
53     }
54
55     public ParameterList add(String JavaDoc name) {
56         return add("?", name);
57     }
58
59     public MethodParameter find(String JavaDoc parameter) {
60         for (Iterator JavaDoc i = iterator(); i.hasNext();) {
61             MethodParameter mp = (MethodParameter) i.next();
62             if (mp.name.equals(parameter)) {
63                 return mp;
64             }
65         }
66         return null;
67     }
68
69     public MethodParameter getParameter(int index) {
70         return (MethodParameter) get(index);
71     }
72
73     public boolean hasSameTypes(ParameterList that) {
74         if (this.size() != that.size()) {
75             return false;
76         }
77         Iterator JavaDoc i = this.iterator();
78         Iterator JavaDoc j = that.iterator();
79         for (; i.hasNext();) {
80             MethodParameter mp1 = (MethodParameter) i.next();
81             MethodParameter mp2 = (MethodParameter) j.next();
82             if (!mp1.type.equals(mp2.type)) {
83                 return false;
84             }
85         }
86         return true;
87     }
88
89     public String JavaDoc toString() {
90         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("(");
91         int comma = 0;
92         for (Iterator JavaDoc i = iterator(); i.hasNext(); comma++) {
93             MethodParameter mp = (MethodParameter) i.next();
94             if (comma > 0) {
95                 sb.append(", ");
96             }
97             sb.append(mp.name);
98         }
99         sb.append(")");
100         return sb.toString();
101     }
102
103     public String JavaDoc toStringWithNoSpaces() {
104         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("(");
105         int comma = 0;
106         for (Iterator JavaDoc i = iterator(); i.hasNext(); comma++) {
107             MethodParameter mp = (MethodParameter) i.next();
108             if (comma > 0) {
109                 sb.append(",");
110             }
111             sb.append(mp.name);
112         }
113         sb.append(")");
114         return sb.toString();
115     }
116
117     public String JavaDoc toStringWithTypesOnly() {
118         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("(");
119         int comma = 0;
120         for (Iterator JavaDoc i = iterator(); i.hasNext(); comma++) {
121             MethodParameter mp = (MethodParameter) i.next();
122             if (comma > 0) {
123                 sb.append(", ");
124             }
125             sb.append(mp.type);
126         }
127         sb.append(")");
128         return sb.toString();
129     }
130
131     public String JavaDoc toStringWithTypes() {
132         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("(");
133         int comma = 0;
134         for (Iterator JavaDoc i = iterator(); i.hasNext(); comma++) {
135             MethodParameter mp = (MethodParameter) i.next();
136             if (comma > 0) {
137                 sb.append(", ");
138             }
139             sb.append(mp.type);
140             sb.append(' ');
141             sb.append(mp.name);
142         }
143         sb.append(")");
144         return sb.toString();
145     }
146
147     public String JavaDoc toStringWithFinalTypes() {
148         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("(");
149         int comma = 0;
150         for (Iterator JavaDoc i = iterator(); i.hasNext(); comma++) {
151             MethodParameter mp = (MethodParameter) i.next();
152             if (comma > 0) {
153                 sb.append(", ");
154             }
155             sb.append("final ");
156             sb.append(mp.type);
157             sb.append(' ');
158             sb.append(mp.name);
159         }
160         sb.append(")");
161         return sb.toString();
162     }
163
164     public String JavaDoc getClassArray() {
165         if (isEmpty()) {
166             return "org.apache.geronimo.interop.util.ArrayUtil.EMPTY_CLASS_ARRAY";
167         }
168         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("new java.lang.Class[] {");
169         int comma = 0;
170         for (Iterator JavaDoc i = iterator(); i.hasNext(); comma++) {
171             MethodParameter mp = (MethodParameter) i.next();
172             if (comma > 0) {
173                 sb.append(", ");
174             }
175             sb.append(mp.type + ".class");
176         }
177         sb.append("}");
178         return sb.toString();
179     }
180
181     public String JavaDoc getObjectArray() {
182         if (isEmpty()) {
183             return "org.apache.geronimo.interop.util.ArrayUtil.EMPTY_OBJECT_ARRAY";
184         }
185         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("new java.lang.Object[] {");
186         int comma = 0;
187         for (Iterator JavaDoc i = iterator(); i.hasNext(); comma++) {
188             MethodParameter mp = (MethodParameter) i.next();
189             if (comma > 0) {
190                 sb.append(", ");
191             }
192             sb.append(mp.getObject());
193         }
194         sb.append("}");
195         return sb.toString();
196     }
197
198     public String JavaDoc wrapAll() {
199         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
200         int comma = 0;
201         for (Iterator JavaDoc i = iterator(); i.hasNext(); comma++) {
202             MethodParameter mp = (MethodParameter) i.next();
203             if (comma > 0) {
204                 sb.append(", ");
205             }
206             sb.append(mp.getObject());
207         }
208         return sb.toString();
209     }
210 }
211
Popular Tags