KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > config > Arguments


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/config/Arguments.java,v 1.9 2004/02/13 02:21:36 sebb Exp $
2
/*
3  * Copyright 2001-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
19 package org.apache.jmeter.config;
20
21 import java.io.Serializable JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import org.apache.jmeter.testelement.property.CollectionProperty;
28 import org.apache.jmeter.testelement.property.PropertyIterator;
29 import org.apache.jmeter.testelement.property.TestElementProperty;
30
31 // Mark Walsh, 2002-08-03 add method:
32
// addArgument(String name, Object value, Object metadata)
33
// Modify methods:
34
// toString(), addEmptyArgument(), addArgument(String name, Object value)
35

36 /**
37  * A set of Argument objects.
38  *
39  * @author Michael Stover
40  * @author Mark Walsh
41  * @version $Revision: 1.9 $
42  */

43 public class Arguments extends ConfigTestElement implements Serializable JavaDoc
44 {
45     /** The name of the property used to store the arguments. */
46     public static final String JavaDoc ARGUMENTS = "Arguments.arguments";
47
48     /**
49      * Create a new Arguments object with no arguments.
50      */

51     public Arguments()
52     {
53         setProperty(new CollectionProperty(ARGUMENTS, new ArrayList JavaDoc()));
54     }
55
56     /**
57      * Get the arguments.
58      *
59      * @return the arguments
60      */

61     public CollectionProperty getArguments()
62     {
63         return (CollectionProperty) getProperty(ARGUMENTS);
64     }
65
66     /**
67      * Clear the arguments.
68      */

69     public void clear()
70     {
71         super.clear();
72         setProperty(new CollectionProperty(ARGUMENTS, new ArrayList JavaDoc()));
73     }
74
75     /**
76      * Set the list of arguments. Any existing arguments will be lost.
77      *
78      * @param arguments the new arguments
79      */

80     public void setArguments(List JavaDoc arguments)
81     {
82         setProperty(new CollectionProperty(ARGUMENTS, arguments));
83     }
84
85     /**
86      * Get the arguments as a Map. Each argument name is used as the key, and
87      * its value as the value.
88      *
89      * @return a new Map with String keys and values containing the arguments
90      */

91     public Map JavaDoc getArgumentsAsMap()
92     {
93         PropertyIterator iter = getArguments().iterator();
94         Map JavaDoc argMap = new HashMap JavaDoc();
95         while (iter.hasNext())
96         {
97             Argument arg = (Argument) iter.next().getObjectValue();
98             // Because CollectionProperty.mergeIn will not prevent adding two
99
// properties of the same name, we need to select the first value so
100
// that this element's values prevail over defaults provided by configuration
101
// elements:
102
if (! argMap.containsKey(arg.getName())) argMap.put(arg.getName(), arg.getValue());
103         }
104         return argMap;
105     }
106
107     /**
108      * Add a new argument with the given name and value.
109      *
110      * @param name the name of the argument
111      * @param value the value of the argument
112      */

113     public void addArgument(String JavaDoc name, String JavaDoc value)
114     {
115         addArgument(new Argument(name, value, null));
116     }
117
118     /**
119      * Add a new argument.
120      *
121      * @param arg the new argument
122      */

123     public void addArgument(Argument arg)
124     {
125         TestElementProperty newArg =
126             new TestElementProperty(arg.getName(), arg);
127         if (isRunningVersion())
128         {
129             this.setTemporary(newArg);
130         }
131         getArguments().addItem(newArg);
132     }
133
134     /**
135      * Add a new argument with the given name, value, and metadata.
136      *
137      * @param name the name of the argument
138      * @param value the value of the argument
139      * @param metadata the metadata for the argument
140      */

141     public void addArgument(String JavaDoc name, String JavaDoc value, String JavaDoc metadata)
142     {
143         addArgument(new Argument(name, value, metadata));
144     }
145
146     /**
147      * Get a PropertyIterator of the arguments.
148      *
149      * @return an iteration of the arguments
150      */

151     public PropertyIterator iterator()
152     {
153         return getArguments().iterator();
154     }
155
156     /**
157      * Create a string representation of the arguments.
158      *
159      * @return the string representation of the arguments
160      */

161     public String JavaDoc toString()
162     {
163         StringBuffer JavaDoc str = new StringBuffer JavaDoc();
164         PropertyIterator iter = getArguments().iterator();
165         while (iter.hasNext())
166         {
167             Argument arg = (Argument) iter.next().getObjectValue();
168             if (arg.getMetaData() == null)
169             {
170                 str.append(arg.getName() + "=" + arg.getValue());
171             }
172             else
173             {
174                 str.append(arg.getName() + arg.getMetaData() + arg.getValue());
175             }
176             if (iter.hasNext())
177             {
178                 str.append("&");
179             }
180         }
181         return str.toString();
182     }
183
184     /**
185      * Remove the specified argument from the list.
186      *
187      * @param row the index of the argument to remove
188      */

189     public void removeArgument(int row)
190     {
191         if (row < getArguments().size())
192         {
193             getArguments().remove(row);
194         }
195     }
196
197     /**
198      * Remove the specified argument from the list.
199      *
200      * @param arg the argument to remove
201      */

202     public void removeArgument(Argument arg)
203     {
204         PropertyIterator iter = getArguments().iterator();
205         while (iter.hasNext())
206         {
207             Argument item = (Argument) iter.next().getObjectValue();
208             if (arg.equals(item))
209             {
210                 iter.remove();
211             }
212         }
213     }
214
215     /**
216      * Remove the argument with the specified name.
217      *
218      * @param argName the name of the argument to remove
219      */

220     public void removeArgument(String JavaDoc argName)
221     {
222         PropertyIterator iter = getArguments().iterator();
223         while (iter.hasNext())
224         {
225             Argument arg = (Argument) iter.next().getObjectValue();
226             if (arg.getName().equals(argName))
227             {
228                 iter.remove();
229             }
230         }
231     }
232
233     /**
234      * Remove all arguments from the list.
235      */

236     public void removeAllArguments()
237     {
238         getArguments().clear();
239     }
240
241     /**
242      * Add a new empty argument to the list. The new argument will have the
243      * empty string as its name and value, and null metadata.
244      */

245     public void addEmptyArgument()
246     {
247         addArgument(new Argument("", "", null));
248     }
249
250     /**
251      * Get the number of arguments in the list.
252      *
253      * @return the number of arguments
254      */

255     public int getArgumentCount()
256     {
257         return getArguments().size();
258     }
259
260     /**
261      * Get a single argument.
262      *
263      * @param row the index of the argument to return.
264      * @return the argument at the specified index, or null if no argument
265      * exists at that index.
266      */

267     public Argument getArgument(int row)
268     {
269         Argument argument = null;
270
271         if (row < getArguments().size())
272         {
273             argument = (Argument) getArguments().get(row).getObjectValue();
274         }
275
276         return argument;
277     }
278 }
279
Popular Tags