01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: package org.apache.jmeter.control;
18:
19: import java.io.Serializable;
20: import java.util.ArrayList;
21: import java.util.Iterator;
22: import java.util.List;
23:
24: /**
25: * A controller that runs its children each at most once, but in a random order.
26: *
27: * @author Mike Verdone
28: * @version $Revision: 571988 $ updated on $Date: 2007-09-02 15:19:10 +0100 (Sun, 02 Sep 2007) $
29: */
30: public class RandomOrderController extends GenericController implements
31: Serializable {
32: /**
33: * Create a new RandomOrderController.
34: */
35: public RandomOrderController() {
36: }
37:
38: /**
39: * @see GenericController#initialize()
40: */
41: public void initialize() {
42: super .initialize();
43: this .reorder();
44: }
45:
46: /**
47: * @see GenericController#reInitialize()
48: */
49: protected void reInitialize() {
50: super .reInitialize();
51: this .reorder();
52: }
53:
54: /**
55: * Replace the subControllersAndSamplers list with a reordered ArrayList.
56: */
57: private void reorder() {
58: int numElements = this .subControllersAndSamplers.size();
59:
60: // Create a new list containing numElements null elements.
61: List reordered = new ArrayList(this .subControllersAndSamplers
62: .size());
63: for (int i = 0; i < numElements; i++) {
64: reordered.add(null);
65: }
66:
67: // Insert the subControllersAndSamplers into random list positions.
68: for (Iterator i = this .subControllersAndSamplers.iterator(); i
69: .hasNext();) {
70: int idx = (int) Math
71: .floor(Math.random() * reordered.size());
72: while (true) {
73: if (idx == numElements) {
74: idx = 0;
75: }
76: if (reordered.get(idx) == null) {
77: reordered.set(idx, i.next());
78: break;
79: }
80: idx++;
81: }
82: }
83:
84: // Replace subControllersAndSamplers with reordered copy.
85: this.subControllersAndSamplers = reordered;
86: }
87: }
|