File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\util\DataSetBuilder.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.util;
25
26 import org.jfree.data.category.CategoryDataset;
27 import org.jfree.data.category.DefaultCategoryDataset;
28
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.TreeSet;
32
33 /**
34 * Builds {@link CategoryDataset}.
35 *
36 * <p>
37 * This code works around an issue in {@link DefaultCategoryDataset} where
38 * order of addition changes the way they are drawn.
39 *
40 * @param <Row>
41 * Names that identify different graphs drawn in the same chart.
42 * @param <Column>
43 * X-axis.
44 */
45 public final class DataSetBuilder<Row extends Comparable,Column extends Comparable> {
46
47 private List<Number> values = new ArrayList<Number>();
48 private List<Row> rows = new ArrayList<Row>();
49 private List<Column> columns = new ArrayList<Column>();
50
51 public void add( Number value, Row rowKey, Column columnKey ) {
52 values.add(value);
53 rows.add(rowKey);
54 columns.add(columnKey);
55 }
56
57 public CategoryDataset build() {
58 DefaultCategoryDataset ds = new DefaultCategoryDataset();
59
60 TreeSet<Row> rowSet = new TreeSet<Row>(rows);
61 TreeSet<Column> colSet = new TreeSet<Column>(columns);
62
63 Comparable[] _rows = rowSet.toArray(new Comparable[rowSet.size()]);
64 Comparable[] _cols = colSet.toArray(new Comparable[colSet.size()]);
65
66 // insert rows and columns in the right order
67 for (Comparable r : _rows)
68 ds.setValue(null, r, _cols[0]);
69 for (Comparable c : _cols)
70 ds.setValue(null, _rows[0], c);
71
72 for( int i=0; i<values.size(); i++ )
73 ds.addValue( values.get(i), rows.get(i), columns.get(i) );
74 return ds;
75 }
76 }
77