← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 17955: Added PageRange.getPages

 

------------------------------------------------------------
revno: 17955
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2015-01-13 15:27:02 +0100
message:
  Added PageRange.getPages
modified:
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/PageRange.java
  dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/PageRangeTest.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-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/PageRange.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/PageRange.java	2015-01-13 13:26:18 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/PageRange.java	2015-01-13 14:27:02 +0000
@@ -1,5 +1,8 @@
 package org.hisp.dhis.system.util;
 
+import java.util.ArrayList;
+import java.util.List;
+
 /*
  * Copyright (c) 2004-2014, University of Oslo
  * All rights reserved.
@@ -135,4 +138,24 @@
         
         return toIndex;
     }
+    
+
+    /**
+     * Returns a list of all pages. Each item is an array where index 0 holds the
+     * from index and index 1 holds the to index. Resets the page.
+     */
+    public List<int[]> getPages()
+    {
+        List<int[]> pages = new ArrayList<>();
+        
+        while ( nextPage() )
+        {
+            int[] range = { getFromIndex(), getToIndex() };
+            pages.add( range );
+        }
+        
+        reset();
+        
+        return pages;
+    }
 }

=== modified file 'dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/PageRangeTest.java'
--- dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/PageRangeTest.java	2015-01-13 13:26:18 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/PageRangeTest.java	2015-01-13 14:27:02 +0000
@@ -28,7 +28,10 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import java.util.List;
+
 import org.junit.Test;
+
 import static org.junit.Assert.*;
 
 /**
@@ -75,4 +78,20 @@
         
         assertFalse( range.nextPage() );
     }
+    
+    @Test
+    public void testGetPages()
+    {
+        PageRange range = new PageRange( 12 ).setPageSize( 5 );
+        
+        List<int[]> pages = range.getPages();
+        
+        assertEquals( 3, pages.size() );
+        assertEquals( 0, pages.get( 0 )[0] );
+        assertEquals( 5, pages.get( 0 )[1] );
+        assertEquals( 5, pages.get( 1 )[0] );
+        assertEquals( 10, pages.get( 1 )[1] );
+        assertEquals( 10, pages.get( 2 )[0] );
+        assertEquals( 12, pages.get( 2 )[1] );
+    }
 }