001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.testelement.property;
020:
021: import java.util.ArrayList;
022: import java.util.Collection;
023: import java.util.List;
024:
025: import org.apache.jmeter.testelement.TestElement;
026:
027: public class CollectionProperty extends MultiProperty {
028:
029: private static final long serialVersionUID = 221L; // Remember to change this when the class changes ...
030:
031: private Collection value;
032:
033: transient private Collection savedValue;
034:
035: public CollectionProperty(String name, Collection value) {
036: super (name);
037: this .value = normalizeList(value);
038: }
039:
040: public CollectionProperty() {
041: super ();
042: value = new ArrayList();
043: }
044:
045: public boolean equals(Object o) {
046: if (o instanceof CollectionProperty) {
047: if (value != null) {
048: return value.equals(((JMeterProperty) o)
049: .getObjectValue());
050: }
051: }
052: return false;
053: }
054:
055: public int hashCode() {
056: return (value == null ? 0 : value.hashCode());
057: }
058:
059: public void remove(String prop) {
060: PropertyIterator iter = iterator();
061: while (iter.hasNext()) {
062: if (iter.next().getName().equals(prop)) {
063: iter.remove();
064: }
065: }
066: }
067:
068: public void set(int index, String prop) {
069: if (value instanceof List) {
070: ((List) value).set(index, new StringProperty(prop, prop));
071: }
072: }
073:
074: public void set(int index, JMeterProperty prop) {
075: if (value instanceof List) {
076: ((List) value).set(index, prop);
077: }
078: }
079:
080: public JMeterProperty get(int row) {
081: if (value instanceof List) {
082: return (JMeterProperty) ((List) value).get(row);
083: }
084: return null;
085: }
086:
087: public void remove(int index) {
088: if (value instanceof List) {
089: ((List) value).remove(index);
090: }
091: }
092:
093: public void setObjectValue(Object v) {
094: if (v instanceof Collection) {
095: setCollection((Collection) v);
096: }
097:
098: }
099:
100: public PropertyIterator iterator() {
101: return getIterator(value);
102: }
103:
104: /*
105: * (non-Javadoc)
106: *
107: * @see JMeterProperty#getStringValue()
108: */
109: public String getStringValue() {
110: return value.toString();
111: }
112:
113: /*
114: * (non-Javadoc)
115: *
116: * @see JMeterProperty#getObjectValue()
117: */
118: public Object getObjectValue() {
119: return value;
120: }
121:
122: public int size() {
123: return value.size();
124: }
125:
126: /*
127: * (non-Javadoc)
128: *
129: * @see Object#clone()
130: */
131: public Object clone() {
132: CollectionProperty prop = (CollectionProperty) super .clone();
133: prop.value = cloneCollection();
134: return prop;
135: }
136:
137: private Collection cloneCollection() {
138: try {
139: Collection newCol = (Collection) value.getClass()
140: .newInstance();
141: PropertyIterator iter = iterator();
142: while (iter.hasNext()) {
143: newCol.add(iter.next().clone());
144: }
145: return newCol;
146: } catch (Exception e) {
147: log.error("Couldn't clone collection", e);
148: return value;
149: }
150: }
151:
152: public void setCollection(Collection coll) {
153: value = normalizeList(coll);
154: }
155:
156: public void clear() {
157: value.clear();
158: }
159:
160: /**
161: * Easy way to add properties to the list.
162: *
163: * @param prop
164: */
165: public void addProperty(JMeterProperty prop) {
166: value.add(prop);
167: }
168:
169: public void addItem(Object item) {
170: addProperty(convertObject(item));
171: }
172:
173: /**
174: * Figures out what kind of properties this collection is holding and
175: * returns the class type.
176: *
177: * @see AbstractProperty#getPropertyType()
178: */
179: protected Class getPropertyType() {
180: if (value.size() > 0) {
181: return value.iterator().next().getClass();
182: }
183: return NullProperty.class;
184: }
185:
186: /*
187: * (non-Javadoc)
188: *
189: * @see JMeterProperty#recoverRunningVersion(TestElement)
190: */
191: public void recoverRunningVersion(TestElement owner) {
192: if (savedValue != null) {
193: value = savedValue;
194: }
195: recoverRunningVersionOfSubElements(owner);
196: }
197:
198: /*
199: * (non-Javadoc)
200: *
201: * @see JMeterProperty#setRunningVersion(boolean)
202: */
203: public void setRunningVersion(boolean running) {
204: super.setRunningVersion(running);
205: if (running) {
206: savedValue = value;
207: } else {
208: savedValue = null;
209: }
210: }
211: }
|