KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/testelement/property/AbstractProperty.java,v 1.19 2004/02/27 11:45:54 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 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/testelement/property/AbstractProperty.java,v 1.19 2004/02/27 11:45:54 sebb Exp $
20
/*
21  * Copyright 2003-2004 The Apache Software Foundation.
22  *
23  * Licensed under the Apache License, Version 2.0 (the "License");
24  * you may not use this file except in compliance with the License.
25  * You may obtain a copy of the License at
26  *
27  * http://www.apache.org/licenses/LICENSE-2.0
28  *
29  * Unless required by applicable law or agreed to in writing, software
30  * distributed under the License is distributed on an "AS IS" BASIS,
31  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
32  * See the License for the specific language governing permissions and
33  * limitations under the License.
34  *
35 */

36
37  package org.apache.jmeter.testelement.property;
38
39 import java.util.Collection JavaDoc;
40 import java.util.Iterator JavaDoc;
41 import java.util.Map JavaDoc;
42
43 import org.apache.jmeter.testelement.TestElement;
44 import org.apache.jorphan.logging.LoggingManager;
45 import org.apache.log.Logger;
46
47 /**
48  * @version $Revision: 1.19 $
49  */

50 public abstract class AbstractProperty implements JMeterProperty
51 {
52     protected static final Logger log = LoggingManager.getLoggerForClass();
53     private String JavaDoc name;
54     private boolean runningVersion = false;
55
56     public AbstractProperty(String JavaDoc name)
57     {
58         if (name == null)
59             throw new IllegalArgumentException JavaDoc("Name cannot be null");
60         this.name = name;
61     }
62
63     public AbstractProperty()
64     {
65         this("");
66     }
67
68     protected boolean isEqualType(JMeterProperty prop)
69     {
70         if (this.getClass().equals(prop.getClass()))
71         {
72             return true;
73         }
74         else
75         {
76             return false;
77         }
78     }
79
80     /* (non-Javadoc)
81      * @see JMeterProperty#isRunningVersion()
82      */

83     public boolean isRunningVersion()
84     {
85         return runningVersion;
86     }
87
88     /* (non-Javadoc)
89      * @see JMeterProperty#getName()
90      */

91     public String JavaDoc getName()
92     {
93         return name;
94     }
95
96     public void setName(String JavaDoc name)
97     {
98         if (name == null)
99             throw new IllegalArgumentException JavaDoc("Name cannot be null");
100         this.name = name;
101     }
102
103     /* (non-Javadoc)
104      * @see JMeterProperty#setRunningVersion(boolean)
105      */

106     public void setRunningVersion(boolean runningVersion)
107     {
108         this.runningVersion = runningVersion;
109     }
110     
111     protected PropertyIterator getIterator(Collection JavaDoc values)
112     {
113         return new PropertyIteratorImpl(values);
114     }
115
116     /* (non-Javadoc)
117      * @see Object#clone()
118      */

119     public Object JavaDoc clone()
120     {
121         try
122         {
123             AbstractProperty prop =
124                 (AbstractProperty) this.getClass().newInstance();
125             prop.name = name;
126             prop.runningVersion = runningVersion;
127             return prop;
128         }
129         catch (InstantiationException JavaDoc e)
130         {
131             return null;
132         }
133         catch (IllegalAccessException JavaDoc e)
134         {
135             return null;
136         }
137     }
138
139     /**
140      * Returns 0 if string is invalid or null.
141      * @see JMeterProperty#getIntValue()
142      */

143     public int getIntValue()
144     {
145         String JavaDoc val = getStringValue();
146         if (val == null)
147         {
148             return 0;
149         }
150         try
151         {
152             return Integer.parseInt(val);
153         }
154         catch (NumberFormatException JavaDoc e)
155         {
156             return 0;
157         }
158     }
159
160     /**
161      * Returns 0 if string is invalid or null.
162      * @see JMeterProperty#getLongValue()
163      */

164     public long getLongValue()
165     {
166         String JavaDoc val = getStringValue();
167         if (val == null)
168         {
169             return 0;
170         }
171         try
172         {
173             return Long.parseLong(val);
174         }
175         catch (NumberFormatException JavaDoc e)
176         {
177             return 0;
178         }
179     }
180
181     /**
182      * Returns 0 if string is invalid or null.
183      * @see JMeterProperty#getDoubleValue()
184      */

185     public double getDoubleValue()
186     {
187         String JavaDoc val = getStringValue();
188         if (val == null)
189         {
190             return 0;
191         }
192         try
193         {
194             return Double.parseDouble(val);
195         }
196         catch (NumberFormatException JavaDoc e)
197         {
198             log.error("Tried to parse a non-number string to an integer", e);
199             return 0;
200         }
201     }
202
203     /**
204      * Returns 0 if string is invalid or null.
205      * @see JMeterProperty#getFloatValue()
206      */

207     public float getFloatValue()
208     {
209         String JavaDoc val = getStringValue();
210         if (val == null)
211         {
212             return 0;
213         }
214         try
215         {
216             return Float.parseFloat(val);
217         }
218         catch (NumberFormatException JavaDoc e)
219         {
220             log.error("Tried to parse a non-number string to an integer", e);
221             return 0;
222         }
223     }
224
225     /**
226      * Returns false if string is invalid or null.
227      * @see JMeterProperty#getBooleanValue()
228      */

229     public boolean getBooleanValue()
230     {
231         String JavaDoc val = getStringValue();
232         if (val == null)
233         {
234             return false;
235         }
236         return Boolean.valueOf(val).booleanValue();
237     }
238
239     /**
240      * Determines if the two objects are equal by comparing names and values
241      *
242      * @return true if names are equal and values are equal (or both null)
243      */

244     public boolean equals(Object JavaDoc o)
245     {
246         if (!(o instanceof JMeterProperty)) return false;
247         if (this == o) return true;
248         JMeterProperty jpo = (JMeterProperty) o;
249         if (!name.equals(jpo.getName())) return false;
250         String JavaDoc s1 = getStringValue();
251         String JavaDoc s2 = jpo.getStringValue();
252         return s1 == null ? s2 == null : s1.equals(s2);
253     }
254     
255     public int hashCode()
256     {
257         int result = 17;
258         result = result * 37 + name.hashCode();// name cannot be null
259
String JavaDoc s = getStringValue();
260         result = result * 37 + (s == null ? 0 : s.hashCode());
261         return result;
262     }
263
264     /**
265      * Compares two JMeterProperty object values.
266      * N.B. Does not compare names
267      *
268      * @param arg0 JMeterProperty to compare against
269      * @return 0 if equal values or both values null;
270      * -1 otherwise
271      * @see Comparable#compareTo(Object)
272      */

273     public int compareTo(Object JavaDoc arg0)
274     {
275         if (arg0 instanceof JMeterProperty)
276         {
277             // We don't expect the string values to ever be null. But (as in
278
// bug 19499) sometimes they are. So have null compare less than
279
// any other value. Log a warning so we can try to find the root
280
// cause of the null value.
281
String JavaDoc val = getStringValue();
282             String JavaDoc val2 = ((JMeterProperty)arg0).getStringValue();
283             if (val == null)
284             {
285                 log.warn(
286                     "Warning: Unexpected null value for property: " + name);
287                 
288                 if (val2 == null)
289                 {
290                     // Two null values -- return equal
291
return 0;
292                 }
293                 else
294                 {
295                     return -1;
296                 }
297             }
298             return val.compareTo(val2);
299         }
300         else
301         {
302             return -1;
303         }
304     }
305
306     /**
307      * Get the property type for this property. Used to convert raw values into
308      * JMeterProperties.
309      */

310     protected Class JavaDoc getPropertyType()
311     {
312         return getClass();
313     }
314
315     protected JMeterProperty getBlankProperty()
316     {
317         try
318         {
319             JMeterProperty prop =
320                 (JMeterProperty) getPropertyType().newInstance();
321             if (prop instanceof NullProperty)
322             {
323                 return new StringProperty();
324             }
325             return prop;
326         }
327         catch (Exception JavaDoc e)
328         {
329             return new StringProperty();
330         }
331     }
332
333     protected Collection JavaDoc normalizeList(Collection JavaDoc coll)
334     {
335         Iterator JavaDoc iter = coll.iterator();
336         Collection JavaDoc newColl = null;
337         while (iter.hasNext())
338         {
339             Object JavaDoc item = iter.next();
340             if (newColl == null)
341             {
342                 try
343                 {
344                     newColl = (Collection JavaDoc) coll.getClass().newInstance();
345                 }
346                 catch (Exception JavaDoc e)
347                 {
348                     log.error("Bad collection", e);
349                 }
350             }
351             newColl.add(convertObject(item));
352         }
353         if (newColl != null)
354         {
355             return newColl;
356         }
357         else
358         {
359             return coll;
360         }
361     }
362
363     /**
364      * Given a Map, it converts the Map into a collection of JMeterProperty
365      * objects, appropriate for a MapProperty object.
366      */

367     protected Map JavaDoc normalizeMap(Map JavaDoc coll)
368     {
369         Iterator JavaDoc iter = coll.keySet().iterator();
370         Map JavaDoc newColl = null;
371         while (iter.hasNext())
372         {
373             Object JavaDoc item = iter.next();
374             Object JavaDoc prop = coll.get(item);
375             if (newColl == null)
376             {
377                 try
378                 {
379                     newColl = (Map JavaDoc) coll.getClass().newInstance();
380                 }
381                 catch (Exception JavaDoc e)
382                 {
383                     log.error("Bad collection", e);
384                 }
385             }
386             newColl.put(item, convertObject(prop));
387         }
388         if (newColl != null)
389         {
390             return newColl;
391         }
392         else
393         {
394             return coll;
395         }
396     }
397
398     protected JMeterProperty convertObject(Object JavaDoc item)
399     {
400         if (item instanceof JMeterProperty)
401         {
402             return (JMeterProperty) item;
403         }
404         else if (item instanceof TestElement)
405         {
406             return new TestElementProperty(
407                 ((TestElement) item).getPropertyAsString(TestElement.NAME),
408                 (TestElement) item);
409         }
410         else if (item instanceof Collection JavaDoc)
411         {
412             return new CollectionProperty(
413                 "" + item.hashCode(),
414                 (Collection JavaDoc) item);
415         }
416         else if (item instanceof Map JavaDoc)
417         {
418             return new MapProperty("" + item.hashCode(), (Map JavaDoc) item);
419         }
420         else
421         {
422             JMeterProperty prop = getBlankProperty();
423             prop.setName(item.toString());
424             prop.setObjectValue(item);
425             return prop;
426         }
427     }
428
429     /**
430      * Provides the string representation of the property.
431      *
432      * @return the string value
433      */

434     public String JavaDoc toString()
435     {
436         // N.B. Other classes rely on this returning just the string.
437
return getStringValue();
438     }
439
440     /* (non-Javadoc)
441      * @see org.apache.jmeter.testelement.property.JMeterProperty#mergeIn(org.apache.jmeter.testelement.property.JMeterProperty)
442      */

443     public void mergeIn(JMeterProperty prop)
444     {
445     }
446
447 }
448
Popular Tags