KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/testelement/property/MapProperty.java,v 1.14 2004/02/13 02:40:53 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.Map JavaDoc;
22
23 import org.apache.jmeter.testelement.TestElement;
24
25 /**
26  * @version $Revision: 1.14 $
27  */

28 public class MapProperty extends MultiProperty
29 {
30     Map JavaDoc value;
31     Map JavaDoc savedValue = null;
32
33     public MapProperty(String JavaDoc name, Map JavaDoc value)
34     {
35         super(name);
36         log.info("map = " + value);
37         this.value = normalizeMap(value);
38         log.info("normalized map = " + this.value);
39     }
40
41     public MapProperty()
42     {
43         super();
44     }
45
46     public boolean equals(Object JavaDoc o)
47     {
48         if (o instanceof MapProperty)
49         {
50             if (value != null)
51             {
52                 return value.equals(((JMeterProperty) o).getObjectValue());
53             }
54         }
55         return false;
56     }
57
58     public void setObjectValue(Object JavaDoc v)
59     {
60         if (v instanceof Map JavaDoc)
61         {
62             setMap((Map JavaDoc) v);
63         }
64     }
65
66     public void addProperty(JMeterProperty prop)
67     {
68         addProperty(prop.getName(), prop);
69     }
70
71     public JMeterProperty get(String JavaDoc key)
72     {
73         return (JMeterProperty) value.get(key);
74     }
75
76     /**
77      * Figures out what kind of properties this collection is holding and
78      * returns the class type.
79      * @see AbstractProperty#getPropertyType()
80      */

81     protected Class JavaDoc getPropertyType()
82     {
83         if (value.size() > 0)
84         {
85             return valueIterator().next().getClass();
86         }
87         else
88         {
89             return NullProperty.class;
90         }
91     }
92
93     /**
94      * @see JMeterProperty#getStringValue()
95      */

96     public String JavaDoc getStringValue()
97     {
98         return value.toString();
99     }
100
101     /**
102      * @see JMeterProperty#getObjectValue()
103      */

104     public Object JavaDoc getObjectValue()
105     {
106         return value;
107     }
108
109     /**
110      * @see java.lang.Object#clone()
111      */

112     public Object JavaDoc clone()
113     {
114         MapProperty prop = (MapProperty) super.clone();
115         prop.value = cloneMap();
116         return prop;
117     }
118
119     private Map JavaDoc cloneMap()
120     {
121         try
122         {
123             Map JavaDoc newCol = (Map JavaDoc) value.getClass().newInstance();
124             PropertyIterator iter = valueIterator();
125             while (iter.hasNext())
126             {
127                 JMeterProperty item = iter.next();
128                 newCol.put(item.getName(), item.clone());
129             }
130             return newCol;
131         }
132         catch (Exception JavaDoc e)
133         {
134             log.error("Couldn't clone map", e);
135             return value;
136         }
137     }
138
139     public PropertyIterator valueIterator()
140     {
141         return getIterator(value.values());
142     }
143
144     public void addProperty(String JavaDoc name, JMeterProperty prop)
145     {
146         if (!value.containsKey(name))
147         {
148             value.put(name, prop);
149         }
150     }
151
152     public void setMap(Map JavaDoc newMap)
153     {
154         value = normalizeMap(newMap);
155     }
156
157     /**
158      * @see JMeterProperty#recoverRunningVersion(TestElement)
159      */

160     public void recoverRunningVersion(TestElement owner)
161     {
162         if (savedValue != null)
163         {
164             value = savedValue;
165         }
166         recoverRunningVersionOfSubElements(owner);
167     }
168
169     public void clear()
170     {
171         value.clear();
172     }
173
174     /* (non-Javadoc)
175      * @see MultiProperty#iterator()
176      */

177     public PropertyIterator iterator()
178     {
179         return valueIterator();
180     }
181
182     /* (non-Javadoc)
183      * @see JMeterProperty#setRunningVersion(boolean)
184      */

185     public void setRunningVersion(boolean running)
186     {
187         super.setRunningVersion(running);
188         if (running)
189         {
190             savedValue = value;
191         }
192         else
193         {
194             savedValue = null;
195         }
196     }
197 }
198
Popular Tags