1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.plugin.charts.source

File DefaultDataSource.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart0.png
83% of files have more coverage

Code metrics

20
50
20
1
255
144
36
0.72
2.5
20
1.8

Classes

Class Line # Actions
DefaultDataSource 28 50 0% 36 90
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package com.xpn.xwiki.plugin.charts.source;
21   
22    import com.xpn.xwiki.plugin.charts.exceptions.ColumnIndexOutOfBoundsException;
23    import com.xpn.xwiki.plugin.charts.exceptions.DataSourceException;
24    import com.xpn.xwiki.plugin.charts.exceptions.NoHeaderColumnException;
25    import com.xpn.xwiki.plugin.charts.exceptions.NoHeaderRowException;
26    import com.xpn.xwiki.plugin.charts.exceptions.RowIndexOutOfBoundsException;
27   
 
28    public class DefaultDataSource implements DataSource
29    {
30    protected Number[][] data;
31   
32    protected String[] headerRow;
33   
34    protected String[] headerColumn;
35   
36    /**
37    * This no-arg constructor creates an empty data source with no headers.
38    */
 
39  0 toggle public DefaultDataSource()
40    {
41  0 data = new Number[0][0];
42    }
43   
44    /**
45    * This constructor creates a data source with no headers
46    *
47    * @param data A matrix containing the values of the data source
48    */
 
49  0 toggle public DefaultDataSource(Number[][] data)
50    {
51  0 this.data = data;
52    }
53   
54    /**
55    * This constructor creates a data source with the given data and headers
56    *
57    * @param data A matrix containing the values of the data source
58    * @param headerRow The header row with headerRow.length == data[x].length, for x=0,data.length-1
59    * @param headerColumn The header column with headerColumn.length == data.length
60    * @throws IllegalArgumentException Thrown when the conditions above are not satisfied
61    */
 
62  0 toggle public DefaultDataSource(Number[][] data, String[] headerRow, String[] headerColumn)
63    {
64  0 if (headerColumn != null && headerColumn.length != data.length) {
65  0 throw new IllegalArgumentException("headerColumn.length != data.length");
66    }
67  0 for (int i = 0; i < data.length; i++) {
68  0 if (headerRow != null && headerRow.length != data[i].length) {
69  0 throw new IllegalArgumentException("headerRow.length != data[" + i + "].length");
70  0 } else if (headerRow == null && i > 0 && data[i].length != data[i - 1].length) {
71  0 throw new IllegalArgumentException("data[" + i + "].length != data[" + (i - 1) + "].length");
72    }
73    }
74  0 this.data = data;
75  0 this.headerColumn = headerColumn;
76  0 this.headerRow = headerRow;
77    }
78   
79    /**
80    * The number of rows of this data source
81    */
 
82  0 toggle @Override
83    public int getRowCount()
84    {
85  0 return data.length;
86    }
87   
88    /**
89    * The number of columns of this data source
90    */
 
91  0 toggle @Override
92    public int getColumnCount()
93    {
94  0 if (data.length > 0) {
95  0 return data[0].length;
96    } else {
97  0 return 0;
98    }
99    }
100   
101    /**
102    * @return The value of a single cell
103    * @throws RowIndexOutOfBoundsException
104    * @throws ColumnIndexOutOfBoundsException
105    */
 
106  0 toggle @Override
107    public Number getCell(int rowIndex, int colIndex) throws DataSourceException
108    {
109  0 checkRowIndex(rowIndex);
110  0 checkColumnIndex(colIndex);
111  0 return data[rowIndex][colIndex];
112    }
113   
114    /**
115    * Sets the value of a single cell
116    *
117    * @throws RowIndexOutOfBoundsException
118    * @throws ColumnIndexOutOfBoundsException
119    */
 
120  0 toggle public void setCell(int rowIndex, int colIndex, Number content) throws DataSourceException
121    {
122  0 checkRowIndex(rowIndex);
123  0 checkColumnIndex(colIndex);
124  0 data[rowIndex][colIndex] = content;
125    }
126   
127    /**
128    * @return A whole row
129    * @throws RowIndexOutOfBoundsException
130    */
 
131  0 toggle @Override
132    public Number[] getRow(int rowIndex) throws DataSourceException
133    {
134  0 checkRowIndex(rowIndex);
135  0 return data[rowIndex];
136    }
137   
138    /**
139    * @return A whole column
140    * @throws ColumnIndexOutOfBoundsException
141    */
 
142  0 toggle @Override
143    public Number[] getColumn(int colIndex) throws DataSourceException
144    {
145  0 checkColumnIndex(colIndex);
146  0 Number[] column = new Number[getRowCount()];
147  0 for (int i = 0; i < getRowCount(); i++) {
148  0 column[i] = data[i][colIndex];
149    }
150  0 return column;
151    }
152   
153    /**
154    * @return A matrix containing the all data source values
155    */
 
156  0 toggle @Override
157    public Number[][] getAllCells() throws DataSourceException
158    {
159  0 return data;
160    }
161   
162    /**
163    * @return true when this data source has a header row
164    */
 
165  0 toggle @Override
166    public boolean hasHeaderRow() throws DataSourceException
167    {
168  0 return headerRow != null;
169    }
170   
171    /**
172    * @return true when this data source has a header column
173    */
 
174  0 toggle @Override
175    public boolean hasHeaderColumn() throws DataSourceException
176    {
177  0 return headerColumn != null;
178    }
179   
180    /**
181    * @return the value in the header row, given by columnIndex
182    * @throws NoHeaderRowException
183    * @throws ColumnIndexOutOfBoundsException
184    */
 
185  0 toggle @Override
186    public String getHeaderRowValue(int columnIndex) throws DataSourceException
187    {
188  0 checkHeaderRow();
189  0 checkColumnIndex(columnIndex);
190  0 return headerRow[columnIndex];
191    }
192   
193    /**
194    * @return The whole header row
195    * @throws NoHeaderRowException
196    */
 
197  0 toggle @Override
198    public String[] getHeaderRow() throws DataSourceException
199    {
200  0 checkHeaderRow();
201  0 return headerRow;
202    }
203   
204    /**
205    * @return the value in the header column, given by rowIndex
206    * @throws NoHeaderColumnException
207    * @throws RowIndexOutOfBoundsException
208    */
 
209  0 toggle @Override
210    public String getHeaderColumnValue(int rowIndex) throws DataSourceException
211    {
212  0 checkHeaderColumn();
213  0 checkRowIndex(rowIndex);
214  0 return headerColumn[rowIndex];
215    }
216   
217    /**
218    * @return The whole header column
219    * @throws NoHeaderColumnException
220    */
 
221  0 toggle @Override
222    public String[] getHeaderColumn() throws DataSourceException
223    {
224  0 checkHeaderColumn();
225  0 return headerColumn;
226    }
227   
 
228  0 toggle private void checkRowIndex(int rowIndex) throws RowIndexOutOfBoundsException
229    {
230  0 if (rowIndex < 0 || rowIndex >= getRowCount()) {
231  0 throw new RowIndexOutOfBoundsException("Invalid row index: " + rowIndex);
232    }
233    }
234   
 
235  0 toggle private void checkColumnIndex(int columnIndex) throws ColumnIndexOutOfBoundsException
236    {
237  0 if (columnIndex < 0 || columnIndex >= getColumnCount()) {
238  0 throw new ColumnIndexOutOfBoundsException("Invalid column index: " + columnIndex);
239    }
240    }
241   
 
242  0 toggle private void checkHeaderRow() throws DataSourceException
243    {
244  0 if (!hasHeaderRow()) {
245  0 throw new NoHeaderRowException();
246    }
247    }
248   
 
249  0 toggle private void checkHeaderColumn() throws DataSourceException
250    {
251  0 if (!hasHeaderColumn()) {
252  0 throw new NoHeaderColumnException();
253    }
254    }
255    }