KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > testelement > property > CollectionProperty


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/testelement/property/CollectionProperty.java,v 1.14 2004/02/21 21:22:35 sebb Exp $
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
19 package org.apache.jmeter.testelement.property;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.apache.jmeter.junit.JMeterTestCase;
26 import org.apache.jmeter.testelement.TestElement;
27
28 /**
29  * @version $Revision: 1.14 $
30  */

31 public class CollectionProperty extends MultiProperty
32 {
33     protected Collection JavaDoc value;
34     private Collection JavaDoc savedValue;
35
36     public CollectionProperty(String JavaDoc name, Collection JavaDoc value)
37     {
38         super(name);
39         this.value = normalizeList(value);
40     }
41
42     public CollectionProperty()
43     {
44         super();
45         value = new ArrayList JavaDoc();
46     }
47
48     public boolean equals(Object JavaDoc o)
49     {
50         if (o instanceof CollectionProperty)
51         {
52             if (value != null)
53             {
54                 return value.equals(((JMeterProperty) o).getObjectValue());
55             }
56         }
57         return false;
58     }
59
60     public int hashCode()
61     {
62         return (value == null ? 0 : value.hashCode());
63     }
64     
65     public void remove(String JavaDoc prop)
66     {
67         PropertyIterator iter = iterator();
68         while (iter.hasNext())
69         {
70             if (iter.next().getName().equals(prop))
71             {
72                 iter.remove();
73             }
74         }
75     }
76
77     public void set(int index, String JavaDoc prop)
78     {
79         if (value instanceof List JavaDoc)
80         {
81             ((List JavaDoc) value).set(index, new StringProperty(prop, prop));
82         }
83     }
84
85     public void set(int index, JMeterProperty prop)
86     {
87         if (value instanceof List JavaDoc)
88         {
89             ((List JavaDoc) value).set(index, prop);
90         }
91     }
92
93     public JMeterProperty get(int row)
94     {
95         if (value instanceof List JavaDoc)
96         {
97             return (JMeterProperty) ((List JavaDoc) value).get(row);
98         }
99         else
100         {
101             return null;
102         }
103     }
104
105     public void remove(int index)
106     {
107         if (value instanceof List JavaDoc)
108         {
109             ((List JavaDoc) value).remove(index);
110         }
111     }
112
113     public void setObjectValue(Object JavaDoc v)
114     {
115         if (v instanceof Collection JavaDoc)
116         {
117             setCollection((Collection JavaDoc) v);
118         }
119
120     }
121
122     public PropertyIterator iterator()
123     {
124         return getIterator(value);
125     }
126
127     /* (non-Javadoc)
128      * @see JMeterProperty#getStringValue()
129      */

130     public String JavaDoc getStringValue()
131     {
132         return value.toString();
133     }
134
135     /* (non-Javadoc)
136      * @see JMeterProperty#getObjectValue()
137      */

138     public Object JavaDoc getObjectValue()
139     {
140         return value;
141     }
142
143     public int size()
144     {
145         return value.size();
146     }
147
148     /* (non-Javadoc)
149      * @see Object#clone()
150      */

151     public Object JavaDoc clone()
152     {
153         CollectionProperty prop = (CollectionProperty) super.clone();
154         prop.value = cloneCollection();
155         return prop;
156     }
157
158     private Collection JavaDoc cloneCollection()
159     {
160         try
161         {
162             Collection JavaDoc newCol = (Collection JavaDoc) value.getClass().newInstance();
163             PropertyIterator iter = iterator();
164             while (iter.hasNext())
165             {
166                 newCol.add(iter.next().clone());
167             }
168             return newCol;
169         }
170         catch (Exception JavaDoc e)
171         {
172             log.error("Couldn't clone collection", e);
173             return value;
174         }
175     }
176
177     public void setCollection(Collection JavaDoc coll)
178     {
179         value = normalizeList(coll);
180     }
181
182     public void clear()
183     {
184         value.clear();
185     }
186
187     /**
188      * Easy way to add properties to the list.
189      * @param prop
190      */

191     public void addProperty(JMeterProperty prop)
192     {
193         value.add(prop);
194     }
195
196     public void addItem(Object JavaDoc item)
197     {
198         addProperty(convertObject(item));
199     }
200
201     /**
202      * Figures out what kind of properties this collection is holding and
203      * returns the class type.
204      * @see AbstractProperty#getPropertyType()
205      */

206     protected Class JavaDoc getPropertyType()
207     {
208         if (value.size() > 0)
209         {
210             return value.iterator().next().getClass();
211         }
212         else
213         {
214             return NullProperty.class;
215         }
216     }
217
218     /* (non-Javadoc)
219      * @see JMeterProperty#recoverRunningVersion(TestElement)
220      */

221     public void recoverRunningVersion(TestElement owner)
222     {
223         if (savedValue != null)
224         {
225             value = savedValue;
226         }
227         recoverRunningVersionOfSubElements(owner);
228     }
229     
230     public static class Test extends JMeterTestCase
231     {
232         public Test(String JavaDoc name)
233         {
234             super(name);
235         }
236         
237         public void testAddingProperties() throws Exception JavaDoc
238         {
239             CollectionProperty coll = new CollectionProperty();
240             coll.addItem("joe");
241             coll.addProperty(new FunctionProperty());
242             assertEquals("joe",coll.get(0).getName());
243             assertEquals(
244                 "org.apache.jmeter.testelement.property.FunctionProperty",
245                 coll.get(1).getClass().getName());
246         }
247     }
248
249     /* (non-Javadoc)
250      * @see JMeterProperty#setRunningVersion(boolean)
251      */

252     public void setRunningVersion(boolean running)
253     {
254         super.setRunningVersion(running);
255         if(running)
256         {
257             savedValue = value;
258         }
259         else
260         {
261             savedValue = null;
262         }
263     }
264 }
265
Popular Tags