KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > util > DataSetBuilder


1 package hudson.util;
2
3 import org.jfree.data.category.CategoryDataset;
4 import org.jfree.data.category.DefaultCategoryDataset;
5
6 import java.util.ArrayList JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.TreeSet JavaDoc;
9
10 /**
11  * Builds {@link CategoryDataset}.
12  *
13  * <p>
14  * This code works around an issue in {@link DefaultCategoryDataset} where
15  * order of addition changes the way they are drawn.
16  */

17 public final class DataSetBuilder<Row extends Comparable JavaDoc,Column extends Comparable JavaDoc> {
18
19     private List JavaDoc<Number JavaDoc> values = new ArrayList JavaDoc<Number JavaDoc>();
20     private List JavaDoc<Row> rows = new ArrayList JavaDoc<Row>();
21     private List JavaDoc<Column> columns = new ArrayList JavaDoc<Column>();
22
23     public void add( Number JavaDoc value, Row rowKey, Column columnKey ) {
24         values.add(value);
25         rows.add(rowKey);
26         columns.add(columnKey);
27     }
28
29     public CategoryDataset build() {
30         DefaultCategoryDataset ds = new DefaultCategoryDataset();
31
32         TreeSet JavaDoc<Row> rowSet = new TreeSet JavaDoc<Row>(rows);
33         TreeSet JavaDoc<Column> colSet = new TreeSet JavaDoc<Column>(columns);
34
35         Comparable JavaDoc[] _rows = rowSet.toArray(new Comparable JavaDoc[rowSet.size()]);
36         Comparable JavaDoc[] _cols = colSet.toArray(new Comparable JavaDoc[colSet.size()]);
37
38         // insert rows and columns in the right order
39
for (Comparable JavaDoc r : _rows)
40             ds.setValue(null, r, _cols[0]);
41         for (Comparable JavaDoc c : _cols)
42             ds.setValue(null, _rows[0], c);
43
44         for( int i=0; i<values.size(); i++ )
45             ds.addValue( values.get(i), rows.get(i), columns.get(i) );
46         return ds;
47     }
48 }
49
Popular Tags