← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 12512: Grid, added method addRows

 

------------------------------------------------------------
revno: 12512
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2013-10-08 19:14:50 +0200
message:
  Grid, added method addRows
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/Grid.java
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/ListGrid.java
  dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/grid/GridTest.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/Grid.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/Grid.java	2013-08-23 15:56:19 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/Grid.java	2013-10-08 17:14:50 +0000
@@ -128,6 +128,13 @@
      * Adds a new row the the grid and moves the cursor accordingly.
      */
     Grid addRow();
+    
+    /**
+     * Adds all rows of the given grid to this grid.
+     * 
+     * @param grid the grid to add to this grid.
+     */
+    Grid addRows( Grid grid );
 
     /**
      * Adds the value to the end of the current row.

=== modified file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/ListGrid.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/ListGrid.java	2013-08-23 16:05:01 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/ListGrid.java	2013-10-08 17:14:50 +0000
@@ -58,7 +58,6 @@
 
 /**
  * @author Lars Helge Overland
- * @version $Id$
  */
 public class ListGrid
     implements Grid
@@ -259,6 +258,20 @@
 
         return this;
     }
+    
+    public Grid addRows( Grid grid )
+    {
+        List<List<Object>> rows = grid.getRows();
+        
+        for ( List<Object> row : rows )
+        {
+            this.grid.add( row );
+            
+            currentRowWriteIndex++;
+        }
+        
+        return this;
+    }
 
     public Grid addValue( Object value )
     {

=== modified file 'dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/grid/GridTest.java'
--- dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/grid/GridTest.java	2013-08-23 16:05:01 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/grid/GridTest.java	2013-10-08 17:14:50 +0000
@@ -50,7 +50,8 @@
  */
 public class GridTest
 {
-    private Grid grid;
+    private Grid gridA;
+    private Grid gridB;
     
     private GridHeader headerA;
     private GridHeader headerB;
@@ -59,35 +60,49 @@
     @Before
     public void setUp()
     {
-        grid = new ListGrid();
+        gridA = new ListGrid();
+        gridB = new ListGrid();
         
         headerA = new GridHeader( "ColA", "colA", String.class.getName(), false, true );
         headerB = new GridHeader( "ColB", "colB", String.class.getName(), false, true );
         headerC = new GridHeader( "ColC", "colC", String.class.getName(), true, false );
         
-        grid.addHeader( headerA );
-        grid.addHeader( headerB );
-        grid.addHeader( headerC );
-        
-        grid.addRow();        
-        grid.addValue( 11 );
-        grid.addValue( 12 );
-        grid.addValue( 13 );
-
-        grid.addRow();        
-        grid.addValue( 21 );
-        grid.addValue( 22 );
-        grid.addValue( 23 );
-
-        grid.addRow();        
-        grid.addValue( 31 );
-        grid.addValue( 32 );
-        grid.addValue( 33 );
-
-        grid.addRow();        
-        grid.addValue( 41 );
-        grid.addValue( 42 );
-        grid.addValue( 43 );
+        gridA.addHeader( headerA );
+        gridA.addHeader( headerB );
+        gridA.addHeader( headerC );
+        
+        gridA.addRow();        
+        gridA.addValue( 11 );
+        gridA.addValue( 12 );
+        gridA.addValue( 13 );
+
+        gridA.addRow();        
+        gridA.addValue( 21 );
+        gridA.addValue( 22 );
+        gridA.addValue( 23 );
+
+        gridA.addRow();        
+        gridA.addValue( 31 );
+        gridA.addValue( 32 );
+        gridA.addValue( 33 );
+
+        gridA.addRow();        
+        gridA.addValue( 41 );
+        gridA.addValue( 42 );
+        gridA.addValue( 43 );
+
+        gridB.addRow();        
+        gridB.addValue( 11 );
+        gridB.addValue( 12 );
+        gridB.addValue( 13 );
+    }
+    
+    @Test
+    public void testAddGrid()
+    {
+        gridA.addRows( gridB );
+        
+        assertEquals( 5, gridA.getHeight() );
     }
     
     @Test
@@ -99,42 +114,42 @@
         metaData.put( 21, "TwentyOne" );
         metaData.put( 22, "TwentyTwo" );
                 
-        assertEquals( 11, grid.getValue( 0, 0 ) );
-        assertEquals( 12, grid.getValue( 0, 1 ) );
-        assertEquals( 21, grid.getValue( 1, 0 ) );
-        assertEquals( 22, grid.getValue( 1, 1 ) );
+        assertEquals( 11, gridA.getValue( 0, 0 ) );
+        assertEquals( 12, gridA.getValue( 0, 1 ) );
+        assertEquals( 21, gridA.getValue( 1, 0 ) );
+        assertEquals( 22, gridA.getValue( 1, 1 ) );
         
-        grid.substituteMetaData( metaData );
+        gridA.substituteMetaData( metaData );
 
-        assertEquals( "Eleven", grid.getValue( 0, 0 ) );
-        assertEquals( "Twelve", grid.getValue( 0, 1 ) );
-        assertEquals( "TwentyOne", grid.getValue( 1, 0 ) );
-        assertEquals( "TwentyTwo", grid.getValue( 1, 1 ) );
+        assertEquals( "Eleven", gridA.getValue( 0, 0 ) );
+        assertEquals( "Twelve", gridA.getValue( 0, 1 ) );
+        assertEquals( "TwentyOne", gridA.getValue( 1, 0 ) );
+        assertEquals( "TwentyTwo", gridA.getValue( 1, 1 ) );
     }
     
     @Test
     public void testGetHeight()
     {
-        assertEquals( 4, grid.getHeight() );
+        assertEquals( 4, gridA.getHeight() );
     }
     
     @Test
     public void testGetWidth()
     {
-        assertEquals( 3, grid.getWidth() );
+        assertEquals( 3, gridA.getWidth() );
     }
         
     @Test
     public void testGetRow()
     {
-        List<Object> rowA = grid.getRow( 0 );
+        List<Object> rowA = gridA.getRow( 0 );
         
         assertTrue( rowA.size() == 3 );
         assertTrue( rowA.contains( 11 ) );
         assertTrue( rowA.contains( 12 ) );
         assertTrue( rowA.contains( 13 ) );
         
-        List<Object> rowB = grid.getRow( 1 );
+        List<Object> rowB = gridA.getRow( 1 );
         
         assertTrue( rowB.size() == 3 );
         assertTrue( rowB.contains( 21 ) );
@@ -145,38 +160,38 @@
     @Test
     public void testGetHeaders()
     {
-        assertEquals( 3, grid.getHeaders().size() );
+        assertEquals( 3, gridA.getHeaders().size() );
     }
     
     @Test
     public void tetsGetVisibleHeaders()
     {
-        assertEquals( 2, grid.getVisibleHeaders().size() );
-        assertTrue( grid.getVisibleHeaders().contains( headerA ) );
-        assertTrue( grid.getVisibleHeaders().contains( headerB ) );
+        assertEquals( 2, gridA.getVisibleHeaders().size() );
+        assertTrue( gridA.getVisibleHeaders().contains( headerA ) );
+        assertTrue( gridA.getVisibleHeaders().contains( headerB ) );
     }
 
     @Test
     public void testGetRows()
     {
-        assertEquals( 4, grid.getRows().size() );
-        assertEquals( 3, grid.getWidth() );
+        assertEquals( 4, gridA.getRows().size() );
+        assertEquals( 3, gridA.getWidth() );
     }
 
     @Test
     public void testGetGetVisibleRows()
     {
-        assertEquals( 4, grid.getVisibleRows().size() );
-        assertEquals( 2, grid.getVisibleRows().get( 0 ).size() );
-        assertEquals( 2, grid.getVisibleRows().get( 1 ).size() );
-        assertEquals( 2, grid.getVisibleRows().get( 2 ).size() );
-        assertEquals( 2, grid.getVisibleRows().get( 3 ).size() );
+        assertEquals( 4, gridA.getVisibleRows().size() );
+        assertEquals( 2, gridA.getVisibleRows().get( 0 ).size() );
+        assertEquals( 2, gridA.getVisibleRows().get( 1 ).size() );
+        assertEquals( 2, gridA.getVisibleRows().get( 2 ).size() );
+        assertEquals( 2, gridA.getVisibleRows().get( 3 ).size() );
     }
     
     @Test
     public void testGetColumn()
     {        
-        List<Object> column1 = grid.getColumn( 1 );
+        List<Object> column1 = gridA.getColumn( 1 );
         
         assertEquals( 4, column1.size() );
         assertTrue( column1.contains( 12 ) );
@@ -184,7 +199,7 @@
         assertTrue( column1.contains( 32 ) );
         assertTrue( column1.contains( 42 ) );
 
-        List<Object> column2 = grid.getColumn( 2 );
+        List<Object> column2 = gridA.getColumn( 2 );
         
         assertEquals( 4, column2.size() );
         assertTrue( column2.contains( 13 ) );
@@ -202,9 +217,9 @@
         columnValues.add( 34 );
         columnValues.add( 44 );
         
-        grid.addColumn( columnValues );
+        gridA.addColumn( columnValues );
         
-        List<Object> column3 = grid.getColumn( 3 );
+        List<Object> column3 = gridA.getColumn( 3 );
         
         assertEquals( 4, column3.size() );
         assertTrue( column3.contains( 14 ) );
@@ -212,7 +227,7 @@
         assertTrue( column3.contains( 34 ) );
         assertTrue( column3.contains( 44 ) );
         
-        List<Object> row2 = grid.getRow( 1 );
+        List<Object> row2 = gridA.getRow( 1 );
         
         assertEquals( 4, row2.size() );
         assertTrue( row2.contains( 21 ) );
@@ -224,70 +239,70 @@
     @Test
     public void testRemoveColumn()
     {
-        assertEquals( 3, grid.getWidth() );
-        
-        grid.removeColumn( 2 );
-        
-        assertEquals( 2, grid.getWidth() );
+        assertEquals( 3, gridA.getWidth() );
+        
+        gridA.removeColumn( 2 );
+        
+        assertEquals( 2, gridA.getWidth() );
     }
     
     @Test
     public void testRemoveColumnByHeader()
     {
-        assertEquals( 3, grid.getWidth() );
-        
-        grid.removeColumn( headerB );
-        
-        assertEquals( 2, grid.getWidth() );
+        assertEquals( 3, gridA.getWidth() );
+        
+        gridA.removeColumn( headerB );
+        
+        assertEquals( 2, gridA.getWidth() );
     }
 
     @Test
     public void testLimit()
     {
-        assertEquals( 4, grid.getRows().size() );
-        
-        grid.limitGrid( 2 );
-        
-        assertEquals( 2, grid.getRows().size() );
-        
-        List<Object> rowA = grid.getRow( 0 );
+        assertEquals( 4, gridA.getRows().size() );
+        
+        gridA.limitGrid( 2 );
+        
+        assertEquals( 2, gridA.getRows().size() );
+        
+        List<Object> rowA = gridA.getRow( 0 );
         assertTrue( rowA.contains( 11 ) );
 
-        List<Object> rowB = grid.getRow( 1 );        
+        List<Object> rowB = gridA.getRow( 1 );        
         assertTrue( rowB.contains( 21 ) );
         
-        grid.limitGrid( 0 );
+        gridA.limitGrid( 0 );
         
-        assertEquals( 2, grid.getRows().size() );
+        assertEquals( 2, gridA.getRows().size() );
     }
     
     @Test
     public void testLimitShortList()
     {
-        assertEquals( 4, grid.getRows().size() );
-        
-        grid.limitGrid( 6 );
-        
-        assertEquals( 4, grid.getRows().size() );
+        assertEquals( 4, gridA.getRows().size() );
+        
+        gridA.limitGrid( 6 );
+        
+        assertEquals( 4, gridA.getRows().size() );
 
-        grid.limitGrid( 4 );
+        gridA.limitGrid( 4 );
         
-        assertEquals( 4, grid.getRows().size() );
+        assertEquals( 4, gridA.getRows().size() );
     }
     
     @Test
     public void testLimits()
     {
-        assertEquals( 4, grid.getRows().size() );
-        
-        grid.limitGrid( 1, 3 );
-        
-        assertEquals( 2, grid.getRows().size() );
+        assertEquals( 4, gridA.getRows().size() );
+        
+        gridA.limitGrid( 1, 3 );
+        
+        assertEquals( 2, gridA.getRows().size() );
 
-        List<Object> rowA = grid.getRow( 0 );
+        List<Object> rowA = gridA.getRow( 0 );
         assertTrue( rowA.contains( 21 ) );
 
-        List<Object> rowB = grid.getRow( 1 );        
+        List<Object> rowB = gridA.getRow( 1 );        
         assertTrue( rowB.contains( 31 ) );        
     }
     
@@ -451,20 +466,20 @@
     @Test
     public void testAddRegressionColumn()
     {
-        grid = new ListGrid();
+        gridA = new ListGrid();
 
-        grid.addRow();        
-        grid.addValue( 10.0 );
-        grid.addRow();        
-        grid.addValue( 50.0 );
-        grid.addRow();        
-        grid.addValue( 20.0 );
-        grid.addRow();        
-        grid.addValue( 60.0 );
-        
-        grid.addRegressionColumn( 0, true );
-        
-        List<Object> column = grid.getColumn( 1 );
+        gridA.addRow();        
+        gridA.addValue( 10.0 );
+        gridA.addRow();        
+        gridA.addValue( 50.0 );
+        gridA.addRow();        
+        gridA.addValue( 20.0 );
+        gridA.addRow();        
+        gridA.addValue( 60.0 );
+        
+        gridA.addRegressionColumn( 0, true );
+        
+        List<Object> column = gridA.getColumn( 1 );
         
         assertTrue( column.size() == 4 );
         assertTrue( column.contains( 17.0 ) );
@@ -476,20 +491,20 @@
     @Test
     public void testAddCumulativeColumn()
     {
-        grid = new ListGrid();
+        gridA = new ListGrid();
 
-        grid.addRow();        
-        grid.addValue( 10.0 );
-        grid.addRow();        
-        grid.addValue( 50.0 );
-        grid.addRow();        
-        grid.addValue( 20.0 );
-        grid.addRow();        
-        grid.addValue( 60.0 );
+        gridA.addRow();        
+        gridA.addValue( 10.0 );
+        gridA.addRow();        
+        gridA.addValue( 50.0 );
+        gridA.addRow();        
+        gridA.addValue( 20.0 );
+        gridA.addRow();        
+        gridA.addValue( 60.0 );
         
-        grid.addCumulativeColumn( 0, true );
+        gridA.addCumulativeColumn( 0, true );
 
-        List<Object> column = grid.getColumn( 1 );
+        List<Object> column = gridA.getColumn( 1 );
         
         assertTrue( column.size() == 4 );
         assertTrue( column.contains( 10.0 ) );
@@ -501,27 +516,27 @@
     @Test
     public void testJRDataSource() throws Exception
     {
-        assertTrue( grid.next() );
-        assertEquals( 11, grid.getFieldValue( new MockJRField( "colA" ) ) );
-        assertEquals( 12, grid.getFieldValue( new MockJRField( "colB" ) ) );
-        assertEquals( 13, grid.getFieldValue( new MockJRField( "colC" ) ) );
-
-        assertTrue( grid.next() );
-        assertEquals( 21, grid.getFieldValue( new MockJRField( "colA" ) ) );
-        assertEquals( 22, grid.getFieldValue( new MockJRField( "colB" ) ) );
-        assertEquals( 23, grid.getFieldValue( new MockJRField( "colC" ) ) );
-
-        assertTrue( grid.next() );
-        assertEquals( 31, grid.getFieldValue( new MockJRField( "colA" ) ) );
-        assertEquals( 32, grid.getFieldValue( new MockJRField( "colB" ) ) );
-        assertEquals( 33, grid.getFieldValue( new MockJRField( "colC" ) ) );
-
-        assertTrue( grid.next() );
-        assertEquals( 41, grid.getFieldValue( new MockJRField( "colA" ) ) );
-        assertEquals( 42, grid.getFieldValue( new MockJRField( "colB" ) ) );
-        assertEquals( 43, grid.getFieldValue( new MockJRField( "colC" ) ) );
+        assertTrue( gridA.next() );
+        assertEquals( 11, gridA.getFieldValue( new MockJRField( "colA" ) ) );
+        assertEquals( 12, gridA.getFieldValue( new MockJRField( "colB" ) ) );
+        assertEquals( 13, gridA.getFieldValue( new MockJRField( "colC" ) ) );
+
+        assertTrue( gridA.next() );
+        assertEquals( 21, gridA.getFieldValue( new MockJRField( "colA" ) ) );
+        assertEquals( 22, gridA.getFieldValue( new MockJRField( "colB" ) ) );
+        assertEquals( 23, gridA.getFieldValue( new MockJRField( "colC" ) ) );
+
+        assertTrue( gridA.next() );
+        assertEquals( 31, gridA.getFieldValue( new MockJRField( "colA" ) ) );
+        assertEquals( 32, gridA.getFieldValue( new MockJRField( "colB" ) ) );
+        assertEquals( 33, gridA.getFieldValue( new MockJRField( "colC" ) ) );
+
+        assertTrue( gridA.next() );
+        assertEquals( 41, gridA.getFieldValue( new MockJRField( "colA" ) ) );
+        assertEquals( 42, gridA.getFieldValue( new MockJRField( "colB" ) ) );
+        assertEquals( 43, gridA.getFieldValue( new MockJRField( "colC" ) ) );
         
-        assertFalse( grid.next() );
+        assertFalse( gridA.next() );
     }
     
     private static List<Object> getList( Object... items )