KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > functions > IntSum


1 // $Header: /home/cvs/jakarta-jmeter/src/functions/org/apache/jmeter/functions/IntSum.java,v 1.6 2004/02/10 00:35:12 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.functions;
20
21 import java.io.Serializable JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.LinkedList JavaDoc;
24 import java.util.List JavaDoc;
25
26 import org.apache.jmeter.engine.util.CompoundVariable;
27 import org.apache.jmeter.samplers.SampleResult;
28 import org.apache.jmeter.samplers.Sampler;
29 import org.apache.jmeter.threads.JMeterVariables;
30 import org.apache.jmeter.util.JMeterUtils;
31
32 /**
33  * Provides an intSum function that adds two or more integer values.
34  *
35  * @author <a HREF="mailto:seade@backstagetech.com.au">Scott Eade</a>
36  * @version $Id: IntSum.java,v 1.6 2004/02/10 00:35:12 sebb Exp $
37  */

38 public class IntSum extends AbstractFunction implements Serializable JavaDoc
39 {
40
41     private static final List JavaDoc desc = new LinkedList JavaDoc();
42     private static final String JavaDoc KEY = "__intSum";
43
44     static {
45         desc.add(JMeterUtils.getResString("intsum_param_1"));
46         desc.add(JMeterUtils.getResString("intsum_param_2"));
47         desc.add(JMeterUtils.getResString("function_name_param"));
48     }
49
50     private Object JavaDoc[] values;
51
52     /**
53      * No-arg constructor.
54      */

55     public IntSum()
56     {
57     }
58
59     /**
60      * Clone this Add object.
61      *
62      * @return A new Add object.
63      */

64     public Object JavaDoc clone()
65     {
66         IntSum newIntSum = new IntSum();
67         return newIntSum;
68     }
69
70     /**
71      * Execute the function.
72      *
73      * @see Function#execute(SampleResult, Sampler)
74      */

75     public synchronized String JavaDoc execute(
76         SampleResult previousResult,
77         Sampler currentSampler)
78         throws InvalidVariableException
79     {
80
81         JMeterVariables vars = getVariables();
82
83         int sum = 0;
84         String JavaDoc varName =
85             ((CompoundVariable) values[values.length - 1]).execute();
86
87         for (int i = 0; i < values.length - 1; i++)
88         {
89             sum += Integer.parseInt(((CompoundVariable) values[i]).execute());
90         }
91
92         String JavaDoc totalString = Integer.toString(sum);
93         vars.put(varName, totalString);
94
95         return totalString;
96
97     }
98
99     /**
100      * Set the parameters for the function.
101      *
102      * @see Function#setParameters(Collection)
103      */

104     public void setParameters(Collection JavaDoc parameters)
105         throws InvalidVariableException
106     {
107         values = parameters.toArray();
108
109         if (values.length < 3)
110         {
111             throw new InvalidVariableException();
112         }
113
114     }
115
116     /**
117      * Get the invocation key for this function.
118      *
119      * @see Function#getReferenceKey()
120      */

121     public String JavaDoc getReferenceKey()
122     {
123         return KEY;
124     }
125
126     /**
127      * Get the description of this function.
128      *
129      * @see Function#getArgumentDesc()
130      */

131     public List JavaDoc getArgumentDesc()
132     {
133         return desc;
134     }
135 }
136
Popular Tags