KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Header: /home/cvs/jakarta-jmeter/src/functions/org/apache/jmeter/functions/Random.java,v 1.4.2.2 2004/06/12 20:26:58 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 a Random function which returns a random integer between
34  * a min (first argument) and a max (seceond argument).
35  *
36  * @author <a HREF="mailto:sjkwadzo@praize.com">Jonathan Kwadzo</a>
37  * @version $Id: Random.java,v 1.4.2.2 2004/06/12 20:26:58 sebb Exp $
38  */

39 public class Random extends AbstractFunction implements Serializable JavaDoc
40 {
41
42     private static final List JavaDoc desc = new LinkedList JavaDoc();
43     private static final String JavaDoc KEY = "__Random";
44
45     static {
46         desc.add(JMeterUtils.getResString("minimum_param"));
47         desc.add(JMeterUtils.getResString("maximum_param"));
48         desc.add(JMeterUtils.getResString("function_name_param"));
49     }
50
51     private transient CompoundVariable varName, minimum, maximum;
52
53     /**
54      * No-arg constructor.
55      */

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

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

76     public synchronized String JavaDoc execute(
77         SampleResult previousResult,
78         Sampler currentSampler)
79         throws InvalidVariableException
80     {
81
82         JMeterVariables vars = getVariables();
83
84         int min = Integer.parseInt(minimum.execute().trim());
85         int max = Integer.parseInt(maximum.execute().trim());
86
87         int rand = (int) Math.round(min + Math.random() * (max - min));
88
89         String JavaDoc randString = Integer.toString(rand);
90         vars.put(varName.execute(), randString);
91
92         return randString;
93
94     }
95
96     /**
97      * Set the parameters for the function.
98      *
99      * @see Function#setParameters(Collection)
100      */

101     public synchronized void setParameters(Collection JavaDoc parameters)
102         throws InvalidVariableException
103     {
104         Object JavaDoc[] values = parameters.toArray();
105
106         if (values.length < 3)
107         {
108             throw new InvalidVariableException();
109         }
110         else
111         {
112             varName = (CompoundVariable) values[2];
113             minimum = (CompoundVariable) values[0];
114             maximum = (CompoundVariable) values[1];
115         }
116
117     }
118
119     /**
120      * Get the invocation key for this function.
121      *
122      * @see Function#getReferenceKey()
123      */

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

134     public List JavaDoc getArgumentDesc()
135     {
136         return desc;
137     }
138
139 }
140
Popular Tags