1 /*
2 * Copyright (c) 2004, RV Test Team
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * Neither the name of the "RV Test Team" nor the names of its contributors may
16 * be used to endorse or promote products derived from this software without
17 * specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29 * THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 */
32 /*
33 * Copyright (c) 2002, Reuters
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions are met:
38 *
39 * Redistributions of source code must retain the above copyright notice, this
40 * list of conditions and the following disclaimer.
41 * Redistributions in binary form must reproduce the above copyright notice,
42 * this list of conditions and the following disclaimer in the documentation
43 * and/or other materials provided with the distribution.
44 * Neither the name of the Reuters nor the names of its contributors may be
45 * used to endorse or promote products derived from this software without
46 * specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
49 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
52 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
53 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
54 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
55 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
56 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
57 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
58 * THE POSSIBILITY OF SUCH DAMAGE.
59 *
60 */
61 package com.reuters.msgtest.load.value;
62
63 import com.reuters.msgtest.load.LoadTest;
64
65
66 /***
67 * Selects values for an integer range based on a selection
68 * criteria (sequential, random, etc).
69 *
70 * @author Cavit Aydin
71 * @version @VSS_FLAG_VERSION@
72 */
73 public class IntRangeValueGenerator implements ValueGenerator {
74 private int min = 0;
75 private int max = Integer.MAX_VALUE;
76 private int currentIndex = 0;
77 private String currentValue = "0";
78 private String selectionCriteria = ValueGenerator.SEQUENTIAL;
79
80 /***
81 * Constructs a IntRangeValueGenerator from a Valueset.
82 *
83 * @param valueSet the valueset object constructed from the
84 * xml config file.
85 */
86 public IntRangeValueGenerator(ValueSet valueSet) {
87 this(((valueSet.getMin() > 0) ? valueSet.getMin() : 0),
88 ((valueSet.getMax() > 0) ? valueSet.getMax() : Integer.MAX_VALUE),
89 valueSet.getSelection());
90 }
91
92 /***
93 * Construct a DelimListValueGenerator using the parameters.
94 *
95 * @param min the min range value
96 * @param max the max range value
97 * @param selection the selection criteria from the range.
98 */
99 public IntRangeValueGenerator(int min, int max, String selection) {
100 this.min = min;
101 this.max = max;
102 selectionCriteria = selection;
103 currentValue = String.valueOf(this.min);
104 }
105
106 /***
107 * @see ValueGenerator#getCurrentValue
108 */
109 public String getCurrentValue() {
110 return currentValue;
111 }
112
113 /***
114 * @see ValueGenerator#getCurrentValue
115 */
116 public String getCurrentValue(String pSubstr) {
117 return currentValue;
118 }
119
120 /***
121 * Computes the size of the range. It also checks
122 * for integer overflows (which happens when max
123 * is equal to Integer.MAX_VALUE). In this case
124 * Integer.MAX_VALUE is returned as the range size.
125 *
126 * @param pMin (non-negative) min value
127 * @param pMax (non-negative) max value
128 * @return the size of the interval
129 */
130 private int getSize(int pMin, int pMax) {
131 int size = Integer.MAX_VALUE;
132
133 if (pMax < size) {
134 size = pMax - pMin + 1; // we assume min_ is positive
135 }
136
137 return size;
138 }
139
140 /***
141 * @see ValueGenerator#changeValue
142 */
143 public boolean changeValue() {
144 int size = getSize(min, max);
145
146 if (selectionCriteria.equals(ValueGenerator.RANDOM)) {
147 int randomIndex = LoadTest.RANDOM.nextInt(size);
148 currentValue = String.valueOf(min + randomIndex);
149 } else {
150 // sequential
151 boolean endLoop = (currentIndex == (size - 1));
152
153 currentIndex = ++currentIndex % size;
154 currentValue = String.valueOf(min + currentIndex);
155
156 return endLoop;
157 }
158
159 return false;
160 }
161 }