File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\matrix\Layouter.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.matrix;
25
26 import java.util.List;
27 import java.util.ArrayList;
28 import java.util.AbstractList;
29 import java.util.Map;
30 import java.util.HashMap;
31
32 /**
33 * Used to assist thegeneration of config table.
34 *
35 * <p>
36 * {@link Axis Axes} are split into four groups.
37 * {@link #x Ones that are displayed as columns},
38 * {@link #y Ones that are displayed as rows},
39 * {@link #z Ones that are listed as bullet items inside table cell},
40 * and those which only have one value, and therefore doesn't show up
41 * in the table.
42 *
43 * <p>
44 * Because of object reuse inside {@link Layouter}, this class is not thread-safe.
45 *
46 * @author Kohsuke Kawaguchi
47 */
48 public abstract class Layouter<T> {
49 public final List<Axis> x,y,z;
50 /**
51 * Axes that only have one value.
52 */
53 private final List<Axis> trivial = new ArrayList<Axis>();
54 /**
55 * Number of data columns and rows.
56 */
57 private int xSize, ySize, zSize;
58
59
60 public Layouter(List<Axis> x, List<Axis> y, List<Axis> z) {
61 this.x = x;
62 this.y = y;
63 this.z = z;
64 init();
65 }
66
67 /**
68 * Automatically split axes to x,y, and z.
69 */
70 public Layouter(AxisList axisList) {
71 x = new ArrayList<Axis>();
72 y = new ArrayList<Axis>();
73 z = new ArrayList<Axis>();
74
75 List<Axis> nonTrivialAxes = new ArrayList<Axis>();
76 for (Axis a : axisList) {
77 if(a.size()>1)
78 nonTrivialAxes.add(a);
79 else
80 trivial.add(a);
81 }
82
83 switch(nonTrivialAxes.size()) {
84 case 0:
85 break;
86 case 1:
87 z.add(nonTrivialAxes.get(0));
88 break;
89 case 2:
90 // use the longer axis in Y
91 Axis a = nonTrivialAxes.get(0);
92 Axis b = nonTrivialAxes.get(1);
93 x.add(a.size() > b.size() ? b : a);
94 y.add(a.size() > b.size() ? a : b);
95 break;
96 default:
97 // for size > 3, use x and y, and try to pack y more
98 for( int i=0; i<nonTrivialAxes.size(); i++ )
99 (i%3==1?x:y).add(nonTrivialAxes.get(i));
100 }
101 init();
102 }
103
104 private void init() {
105 xSize = calc(x,-1);
106 ySize = calc(y,-1);
107 zSize = calc(z,-1);
108 }
109
110 /**
111 * Computes the width of n-th X-axis.
112 */
113 public int width(int n) {
114 return calc(x,n);
115 }
116
117 /**
118 * Computes the repeat count of n-th X-axis.
119 */
120 public int repeatX(int n) {
121 int w = 1;
122 for( n--; n>=0; n-- )
123 w *= x.get(n).size();
124 return w;
125 }
126
127 /**
128 * Computes the width of n-th Y-axis.
129 */
130 public int height(int n) {
131 return calc(y,n);
132 }
133
134 private int calc(List<Axis> l, int n) {
135 int w = 1;
136 for( n++ ; n<l.size(); n++ )
137 w *= l.get(n).size();
138 return w;
139 }
140
141 /**
142 * Gets list of {@link Row}s to be displayed.
143 *
144 * The {@link Row} object is reused, so every value
145 * in collection returns the same object (but with different values.)
146 */
147 public List<Row> getRows() {
148 return new AbstractList<Row>() {
149 final Row row = new Row();
150 public Row get(int index) {
151 row.index = index;
152 return row;
153 }
154
155 public int size() {
156 return ySize;
157 }
158 };
159 }
160
161 /**
162 * Represents a row, which is a collection of {@link Column}s.
163 */
164 public final class Row extends AbstractList<Column> {
165 private int index;
166 final Column col = new Column();
167
168 @Override
169 public Column get(int index) {
170 col.xp = index;
171 col.yp = Row.this.index;
172 return col;
173 }
174
175 @Override
176 public int size() {
177 return xSize;
178 }
179
180 public String drawYHeader(int n) {
181 int base = calc(y,n);
182 if(index/base==(index-1)/base && index!=0) return null; // no need to draw a new value
183
184 Axis axis = y.get(n);
185 return axis.value((index/base)%axis.getValues().size());
186 }
187 }
188
189 protected abstract T getT(Combination c);
190
191 public final class Column extends AbstractList<T> {
192 /**
193 * Cell position.
194 */
195 private int xp,yp;
196
197 private final Map<String,String> m = new HashMap<String,String>();
198
199 public T get(int zp) {
200 m.clear();
201 buildMap(xp,x);
202 buildMap(yp,y);
203 buildMap(zp,z);
204 for (Axis a : trivial)
205 m.put(a.name,a.value(0));
206 return getT(new Combination(m));
207 }
208
209 private void buildMap(int p, List<Axis> axes) {
210 int n = p;
211 for( int i= axes.size()-1; i>=0; i-- ) {
212 Axis a = axes.get(i);
213 m.put(a.name, a.value(n%a.size()));
214 n /= a.size();
215 }
216 }
217
218 public int size() {
219 return zSize;
220 }
221 }
222 }
223