1
package org.hisp.dhis.system.grid;
4
* Copyright (c) 2004-2007, University of Oslo
7
* Redistribution and use in source and binary forms, with or without
8
* modification, are permitted provided that the following conditions are met:
9
* * Redistributions of source code must retain the above copyright notice, this
10
* list of conditions and the following disclaimer.
11
* * Redistributions in binary form must reproduce the above copyright notice,
12
* this list of conditions and the following disclaimer in the documentation
13
* and/or other materials provided with the distribution.
14
* * Neither the name of the HISP project nor the names of its contributors may
15
* be used to endorse or promote products derived from this software without
16
* specific prior written permission.
18
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
22
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
import static org.hisp.dhis.system.util.MathUtils.getRounded;
32
import java.util.ArrayList;
33
import java.util.List;
35
import org.apache.commons.math.stat.regression.SimpleRegression;
38
* @author Lars Helge Overland
44
* A two dimensional List which simulates a grid where the first list
45
* represents rows and the second represents columns.
47
private final List<List<String>> grid;
50
* Indicating the current row in the grid.
52
private int currentRowIndex = -1;
55
* Default constructor.
59
grid = new ArrayList<List<String>>();
62
// ---------------------------------------------------------------------
64
// ---------------------------------------------------------------------
67
* Returns the current height / number of rows in the grid.
69
public int getHeight()
71
return ( grid != null && grid.size() > 0 ) ? grid.size() : 0;
75
* Returns the current width / number of columns in the grid.
81
return ( grid != null && grid.size() > 0 ) ? grid.get( 0 ).size() : 0;
85
* Adds a new row the the grid and moves the cursor accordingly.
89
grid.add( new ArrayList<String>() );
95
* Adds the value to the end of the current row.
97
* @param value the value to add.
99
public void addValue( String value )
101
grid.get( currentRowIndex ).add( value );
105
* Returns the row with the given index.
107
* @param rowIndex the index of the row.
109
public List<String> getRow( int rowIndex )
111
return grid.get( rowIndex );
117
public List<List<String>> getRows()
123
* Returns the column with the given index.
125
* @param columnIndex the index of the column.
127
public List<String> getColumn( int columnIndex )
129
List<String> column = new ArrayList<String>();
131
for ( List<String> row : grid )
133
column.add( row.get( columnIndex ) );
140
* Adds a new column at the end of the grid.
142
* @param columnValues the column values to add.
143
* @throws IllegalStateException if the columnValues has different length
144
* than the rows in grid, or if the grid rows are not of the same length.
146
public void addColumn( List<String> columnValues )
153
if ( grid.size() != columnValues.size() )
155
throw new IllegalStateException( "Column values are not equal to number of rows" );
158
for ( int i = 0; i < grid.size(); i++ )
160
grid.get( rowIndex++ ).add( columnValues.get( columnIndex++ ) );
165
* Column must hold numeric data.
167
* @param columnIndex the index of the base column.
169
public void addRegressionColumn( int columnIndex )
173
SimpleRegression regression = new SimpleRegression();
175
List<String> column = getColumn( columnIndex );
179
for ( String value : column )
183
if ( Double.parseDouble( value ) != 0.0 ) // 0 omitted from regression
185
regression.addData( index, Double.parseDouble( value ) );
189
List<String> regressionColumn = new ArrayList<String>();
193
for ( int i = 0; i < column.size(); i++ )
195
final double predicted = regression.predict( index++ );
197
if ( !Double.isNaN( predicted ) ) // Enough values must exist for regression
199
regressionColumn.add( String.valueOf( getRounded( predicted, 1 ) ) );
203
addColumn( regressionColumn );
206
// ---------------------------------------------------------------------
207
// Supportive methods
208
// ---------------------------------------------------------------------
211
* Verifies that all grid rows are of the same length.
213
private void verifyGridState()
215
Integer rowLength = null;
217
for ( List<String> row : grid )
219
if ( rowLength != null && rowLength != row.size() )
221
throw new IllegalStateException( "Grid rows do not have the same number of cells" );
224
rowLength = row.size();
228
// ---------------------------------------------------------------------
230
// ---------------------------------------------------------------------
233
public String toString()
235
StringBuffer buffer = new StringBuffer( "[" );
237
for ( List<String> row : grid )
239
buffer.append( row );
242
return buffer.append( "]" ).toString();