|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
CyclicParamsAssigner.java | 100% | 100% | 100% | 100% |
|
1 | /** | |
2 | * JTRunner is free software; you can redistribute it and/or modify it under the | |
3 | * terms of the GNU General Public License as published by the Free Software | |
4 | * Foundation; either version 2, or (at your option) any later version. | |
5 | */ | |
6 | ||
7 | package jtr.assigner.impl; | |
8 | ||
9 | import jtr.runners.IRunnerClean; | |
10 | import jtr.config.RunnerConfig; | |
11 | import jtr.runners.IRunnerParameterized; | |
12 | import jtr.config.ParametersMap; | |
13 | import org.apache.log4j.Logger; | |
14 | ||
15 | /** | |
16 | * This class is a concrete <code>IParamsAssigner</code> implementation | |
17 | * providing a cyclic parameters assignment policy.<br> | |
18 | * This means that should a configured runner have multiple parameters-sets | |
19 | * associated with it, this particular assigner would iterate over all the | |
20 | * available parameters-set and assign them to the runner following the order in | |
21 | * which they compare in the <code>jtr.xml</code> configuration file. Each | |
22 | * assignment will be performed at the beginning of each run.<br> | |
23 | * The number of runs has priority over the number of configured | |
24 | * parameters-sets. | |
25 | * | |
26 | * @author Francesco Russo (frusso@dev.java.net) | |
27 | * @version 4.0 | |
28 | * @since 1.0 | |
29 | */ | |
30 | public class CyclicParamsAssigner extends AbstractParamsAssigner { | |
31 | /** | |
32 | * Default constructor. | |
33 | */ | |
34 | 4 | public CyclicParamsAssigner() { |
35 | } | |
36 | ||
37 | /** | |
38 | * Assing to the provided <code>cRunners</code> the parameters defined in | |
39 | * the first parameter-set obtainable from the given | |
40 | * <code>runnerConfig</code>. | |
41 | * | |
42 | * @param cRunners | |
43 | * IRunnerClean[] Set of <code>IRunnerClean</code> instances | |
44 | * belonging to the same class. | |
45 | * @param runnerConfig | |
46 | * RunnerConfig Configuration for the above runners | |
47 | * @return IRunnerParameterized[] The set of input runners transitioned to | |
48 | * the <code>PARAMETERIZED</code> state | |
49 | */ | |
50 | 4 | public IRunnerParameterized[] assign(IRunnerClean[] cRunners, RunnerConfig runnerConfig) { |
51 | 4 | logger.debug("Assigning parameters to " + cRunners.length + " IRunners"); |
52 | // local memory for the future | |
53 | 4 | this.runnerConfig = runnerConfig; |
54 | 4 | ParametersMap params = (ParametersMap) runnerConfig.getParameterMaps().get(0); |
55 | 4 | for (int i = 0; i < cRunners.length; i++) { |
56 | 30 | performStdAssignment(cRunners[i], runnerConfig, params, this); |
57 | } | |
58 | 4 | return (IRunnerParameterized[]) cRunners; |
59 | } | |
60 | ||
61 | /** | |
62 | * This method always returns <code>true</code>. | |
63 | * | |
64 | * @return boolean | |
65 | */ | |
66 | 21950 | public boolean requiresReinitialization() { |
67 | 21950 | return true; |
68 | } | |
69 | ||
70 | /** | |
71 | * Perform every assignment subsequent to the first one using the | |
72 | * <code>RunnerConfig</code> provided to the <code>assign</code> method. | |
73 | * | |
74 | * @param cRunner | |
75 | * IRunnerClean The runner to be parameterized in its | |
76 | * <code>CLEAN</code> state | |
77 | * @return IRunnerParameterized The runner in its <code>PARAMETERIZED</code> | |
78 | * state | |
79 | */ | |
80 | 15969 | public IRunnerParameterized reAssign(IRunnerClean cRunner) { |
81 | 15968 | if (threadLocal.get() == null) { |
82 | // should a thread be invoking this method for the first time | |
83 | // let's set to 1 the depth in the list of the parameters to be | |
84 | // assigned | |
85 | // to the IRunner represented by the current thread | |
86 | 1500 | threadLocal.set(new Integer(1)); |
87 | } | |
88 | 15966 | Integer depth = (Integer) threadLocal.get(); |
89 | // let's retrieve the parameters of index "depth mod(dim)" | |
90 | 15966 | ParametersMap params = (ParametersMap) runnerConfig.getParameterMaps().get(depth.intValue() % runnerConfig.getParameterMaps().size()); |
91 | // we need to re-read the standard parameters too, since they might be | |
92 | // modified by the | |
93 | // current ParametersMap | |
94 | 15969 | performStdAssignment(cRunner, runnerConfig, params, this); |
95 | // let's increase by one the depth value held by the threadLocal | |
96 | // variable | |
97 | 15969 | threadLocal.set(new Integer(depth.intValue() + 1)); |
98 | 15970 | return (IRunnerParameterized) cRunner; |
99 | } | |
100 | ||
101 | /** | |
102 | * Brings the provided runner back to its first state. | |
103 | * | |
104 | * @param cRunner | |
105 | * IRunnerClean The runner in its <code>CLEAN</code> state | |
106 | * @return IRunnerParameterized The runner in its <code>PARAMETERIZED</code> | |
107 | * state | |
108 | */ | |
109 | 1470 | public IRunnerParameterized backToFirstAssignment(IRunnerClean cRunner) { |
110 | 1470 | logger.debug("Bringing the IRunner back to its first state"); |
111 | 1470 | threadLocal.set(new Integer(0)); |
112 | 1470 | return reAssign(cRunner); |
113 | } | |
114 | ||
115 | private ThreadLocal<Integer> threadLocal = new ThreadLocal<Integer>(); | |
116 | ||
117 | private RunnerConfig runnerConfig; | |
118 | ||
119 | private static Logger logger = Logger.getLogger(CyclicParamsAssigner.class); | |
120 | } |
|