~mortenoh/+junk/dhis2-detailed-import-export

« back to all changes in this revision

Viewing changes to dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/Grid.java

  • Committer: larshelge at gmail
  • Date: 2009-03-03 16:46:36 UTC
  • Revision ID: larshelge@gmail.com-20090303164636-2sjlrquo7ib1gf7r
Initial check-in

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.hisp.dhis.system.grid;
 
2
 
 
3
/*
 
4
 * Copyright (c) 2004-2007, University of Oslo
 
5
 * All rights reserved.
 
6
 *
 
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.
 
17
 *
 
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.
 
28
 */
 
29
 
 
30
import static org.hisp.dhis.system.util.MathUtils.getRounded;
 
31
 
 
32
import java.util.ArrayList;
 
33
import java.util.List;
 
34
 
 
35
import org.apache.commons.math.stat.regression.SimpleRegression;
 
36
 
 
37
/**
 
38
 * @author Lars Helge Overland
 
39
 * @version $Id$
 
40
 */
 
41
public class Grid
 
42
{
 
43
    /**
 
44
     * A two dimensional List which simulates a grid where the first list
 
45
     * represents rows and the second represents columns.
 
46
     */
 
47
    private final List<List<String>> grid;
 
48
    
 
49
    /**
 
50
     * Indicating the current row in the grid.
 
51
     */
 
52
    private int currentRowIndex = -1;
 
53
    
 
54
    /**
 
55
     * Default constructor.
 
56
     */
 
57
    public Grid()
 
58
    {
 
59
        grid = new ArrayList<List<String>>();
 
60
    }
 
61
 
 
62
    // ---------------------------------------------------------------------
 
63
    // Public methods
 
64
    // ---------------------------------------------------------------------
 
65
 
 
66
    /**
 
67
     * Returns the current height / number of rows in the grid.
 
68
     */
 
69
    public int getHeight()
 
70
    {        
 
71
        return ( grid != null && grid.size() > 0 ) ? grid.size() : 0;
 
72
    }
 
73
    
 
74
    /**
 
75
     * Returns the current width / number of columns in the grid.
 
76
     */
 
77
    public int getWidth()
 
78
    {
 
79
        verifyGridState();
 
80
        
 
81
        return ( grid != null && grid.size() > 0 ) ? grid.get( 0 ).size() : 0;
 
82
    }
 
83
    
 
84
    /**
 
85
     * Adds a new row the the grid and moves the cursor accordingly.
 
86
     */
 
87
    public void nextRow()
 
88
    {
 
89
        grid.add( new ArrayList<String>() );
 
90
        
 
91
        currentRowIndex++;
 
92
    }
 
93
    
 
94
    /**
 
95
     * Adds the value to the end of the current row.
 
96
     * 
 
97
     * @param value the value to add.
 
98
     */
 
99
    public void addValue( String value )
 
100
    {
 
101
        grid.get( currentRowIndex ).add( value );
 
102
    }
 
103
    
 
104
    /**
 
105
     * Returns the row with the given index.
 
106
     * 
 
107
     * @param rowIndex the index of the row.
 
108
     */
 
109
    public List<String> getRow( int rowIndex )
 
110
    {
 
111
        return grid.get( rowIndex );
 
112
    }
 
113
    
 
114
    /**
 
115
     * Returns all rows.
 
116
     */
 
117
    public List<List<String>> getRows()
 
118
    {
 
119
        return grid;
 
120
    }
 
121
    
 
122
    /**
 
123
     * Returns the column with the given index.
 
124
     * 
 
125
     * @param columnIndex the index of the column.
 
126
     */
 
127
    public List<String> getColumn( int columnIndex )
 
128
    {
 
129
        List<String> column = new ArrayList<String>();
 
130
        
 
131
        for ( List<String> row : grid )
 
132
        {
 
133
            column.add( row.get( columnIndex ) );
 
134
        }
 
135
        
 
136
        return column;
 
137
    }
 
138
    
 
139
    /**
 
140
     * Adds a new column at the end of the grid.
 
141
     * 
 
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.
 
145
     */
 
146
    public void addColumn( List<String> columnValues )
 
147
    {
 
148
        verifyGridState();
 
149
        
 
150
        int rowIndex = 0;
 
151
        int columnIndex = 0;
 
152
        
 
153
        if ( grid.size() != columnValues.size() )
 
154
        {
 
155
            throw new IllegalStateException( "Column values are not equal to number of rows" );
 
156
        }
 
157
        
 
158
        for ( int i = 0; i < grid.size(); i++ )
 
159
        {
 
160
            grid.get( rowIndex++ ).add( columnValues.get( columnIndex++ ) );
 
161
        }
 
162
    }
 
163
    
 
164
    /**
 
165
     * Column must hold numeric data.
 
166
     * 
 
167
     * @param columnIndex the index of the base column.
 
168
     */
 
169
    public void addRegressionColumn( int columnIndex )
 
170
    {
 
171
        verifyGridState();
 
172
        
 
173
        SimpleRegression regression = new SimpleRegression();
 
174
        
 
175
        List<String> column = getColumn( columnIndex );
 
176
        
 
177
        int index = 0;
 
178
        
 
179
        for ( String value : column )
 
180
        {
 
181
            index++;
 
182
            
 
183
            if ( Double.parseDouble( value ) != 0.0 ) // 0 omitted from regression
 
184
            {
 
185
                regression.addData( index, Double.parseDouble( value ) );
 
186
            }
 
187
        }
 
188
        
 
189
        List<String> regressionColumn = new ArrayList<String>();
 
190
        
 
191
        index = 0;
 
192
        
 
193
        for ( int i = 0; i < column.size(); i++ )
 
194
        {
 
195
            final double predicted = regression.predict( index++ );
 
196
            
 
197
            if ( !Double.isNaN( predicted ) ) // Enough values must exist for regression
 
198
            {
 
199
                regressionColumn.add( String.valueOf( getRounded( predicted, 1 ) ) );
 
200
            }
 
201
        }
 
202
 
 
203
        addColumn( regressionColumn );
 
204
    }
 
205
 
 
206
    // ---------------------------------------------------------------------
 
207
    // Supportive methods
 
208
    // ---------------------------------------------------------------------
 
209
 
 
210
    /**
 
211
     * Verifies that all grid rows are of the same length.
 
212
     */
 
213
    private void verifyGridState()
 
214
    {
 
215
        Integer rowLength = null;    
 
216
    
 
217
        for ( List<String> row : grid )
 
218
        {
 
219
            if ( rowLength != null && rowLength != row.size() )
 
220
            {
 
221
                throw new IllegalStateException( "Grid rows do not have the same number of cells" );
 
222
            }
 
223
            
 
224
            rowLength = row.size();
 
225
        }
 
226
    }
 
227
 
 
228
    // ---------------------------------------------------------------------
 
229
    // toString
 
230
    // ---------------------------------------------------------------------
 
231
 
 
232
    @Override
 
233
    public String toString()
 
234
    {
 
235
        StringBuffer buffer = new StringBuffer( "[" );
 
236
        
 
237
        for ( List<String> row : grid )
 
238
        {
 
239
            buffer.append( row );
 
240
        }
 
241
        
 
242
        return buffer.append( "]" ).toString();
 
243
    }
 
244
}